Hi,
I’m basic on programming and first time dealing with python language. Following some integration examples I got my old automation system to work with Home Assistant.
The problem is when I turn on a light from Home Assistant the, the light effectively goes on immediately but the icon takes some seconds until change it’s state. I think this is related with “DataUpdateCoordinator”, but not sure.
What I would like to have is the status changed immediately on “turn_on” and “turn_off” commands and let the device status follow the normal update later, changing the status if necessary.
I think it should be related with this excerpt of my code:
class MDLight(Light):
""" Provides a Mordomus light. """
def __init__(self, coordinator, name, device_id, is_dimmable, mordomusconnect):
self._name = name
self._coordinator = coordinator
self._device_id = device_id
self._state = None
self._brightness = 255
self._is_dimmable = is_dimmable
self._mdconnect = mordomusconnect
@property
def supported_features(self):
"""Flag supported features."""
features = 0
if self._is_dimmable:
features += SUPPORT_BRIGHTNESS
return features
@property
def should_poll(self):
""" No polling needed for a Mordomus light. """
return False
@property
def unique_id(self):
""" Returns the unique_id of the Mordomus light. """
return 'mordomus.' + str(self._device_id).lower()
@property
def name(self):
""" Returns the name of the Mordomus light. """
return self._name
@property
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
@property
def is_on(self):
""" True if the Mordomus light is on. """
return self._coordinator.data[self._device_id]['state']
@property
def available(self):
"""Return if entity is available."""
return self._coordinator.last_update_success
async def async_turn_on(self, **kwargs):
""" Turn the Mordomus light on. """
brightness_value = 255
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
brightness_value = calculate_brightness(self._brightness)
url = "http://192.168.1.99:8888/control?device&cod=%s&value=%d" % (self._device_id, brightness_value)
_LOGGER.info(url)
_LOGGER.info("Turn_ON %s" % (await self._mdconnect.send_command(url)))
#self.async_write_ha_state()
#await self._coordinator.async_request_refresh()
#self.async_schedule_update_ha_state()
#self._state = self._coordinator.data[self._device_id]['state']
_LOGGER.info(self.is_on)
async def async_turn_off(self, **kwargs):
""" Turn the Mordomus light off. """
url = "http://192.168.1.99:8888/control?device&cod=%s&value=0" % (self._device_id)
_LOGGER.info(url)
_LOGGER.info("Turn_OFF %s" % (await self._mdconnect.send_command(url)))
#self.async_write_ha_state()
#await self._coordinator.async_request_refresh()
#self._state = self._coordinator.data[self._device_id]['state']
#self.async_schedule_update_ha_state()
_LOGGER.info(self.is_on)
async def async_added_to_hass(self):
"""When entity is added to hass."""
self._coordinator.async_add_listener(self.async_write_ha_state)
async def async_will_remove_from_hass(self):
"""When entity will be removed from hass."""
self._coordinator.async_remove_listener(self.async_write_ha_state)
async def async_update(self):
"""Update the entity."""
#self._state = self._coordinator.data[self._device_id]['state']
I appreciate any suggestion.
Thanks.