From d4b05a6a852678b4291f400da7a33cdd49b639bd Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Mon, 17 Apr 2017 02:40:22 +0200 Subject: [PATCH] Fix LIFX lights with disappearing names (#7119) * Cache the name of LIFX lights After #7031 the LIFX device will change during an unregister/register transition. This has the user-visible effect of the new device missing a friendly name until the next poll. We now cache the name internally and it will then transfer to the new device when it registers. * Allow LIFX logging even without an available device This will allow us to set the device to None when it unregisters. * Calculate LIFX availability from the existence of a device This has become possible because the device is no longer needed to provide the name of the light when it is unavailable. We just have to forget the device when it unregisters. --- homeassistant/components/light/lifx.py | 34 ++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/light/lifx.py b/homeassistant/components/light/lifx.py index da09601c1bb..945c163435b 100644 --- a/homeassistant/components/light/lifx.py +++ b/homeassistant/components/light/lifx.py @@ -82,9 +82,8 @@ class LIFXManager(object): """Callback for newly detected bulb.""" if device.mac_addr in self.entities: entity = self.entities[device.mac_addr] - _LOGGER.debug("%s register AGAIN", entity.ipaddr) - entity.available = True entity.device = device + _LOGGER.debug("%s register AGAIN", entity.who) self.hass.async_add_job(entity.async_update_ha_state()) else: _LOGGER.debug("%s register NEW", device.ip_addr) @@ -94,7 +93,7 @@ class LIFXManager(object): def ready(self, device, msg): """Callback that adds the device once all data is retrieved.""" entity = LIFXLight(device) - _LOGGER.debug("%s register READY", entity.ipaddr) + _LOGGER.debug("%s register READY", entity.who) self.entities[device.mac_addr] = entity self.async_add_devices([entity]) @@ -103,8 +102,8 @@ class LIFXManager(object): """Callback for disappearing bulb.""" if device.mac_addr in self.entities: entity = self.entities[device.mac_addr] - _LOGGER.debug("%s unregister", entity.ipaddr) - entity.available = False + _LOGGER.debug("%s unregister", entity.who) + entity.device = None entity.updated_event.set() self.hass.async_add_job(entity.async_update_ha_state()) @@ -129,29 +128,27 @@ class LIFXLight(Light): self.updated_event = asyncio.Event() self.blocker = None self.postponed_update = None - self._available = True + self._name = device.label self.set_power(device.power_level) self.set_color(*device.color) @property def available(self): """Return the availability of the device.""" - return self._available - - @available.setter - def available(self, value): - """Set the availability of the device.""" - self._available = value + return self.device is not None @property def name(self): """Return the name of the device.""" - return self.device.label + return self._name @property - def ipaddr(self): - """Return the IP address of the device.""" - return self.device.ip_addr[0] + def who(self): + """Return a string identifying the device.""" + if self.device: + return self.device.ip_addr[0] + else: + return "(%s)" % self.name @property def rgb_color(self): @@ -247,7 +244,7 @@ class LIFXLight(Light): hsbk = [hue, saturation, brightness, kelvin] _LOGGER.debug("turn_on: %s (%d) %d %d %d %d %d", - self.ipaddr, self._power, fade, *hsbk) + self.who, self._power, fade, *hsbk) if self._power == 0: if changed_color: @@ -282,12 +279,13 @@ class LIFXLight(Light): """Callback that gets current power/color status.""" self.set_power(device.power_level) self.set_color(*device.color) + self._name = device.label self.updated_event.set() @asyncio.coroutine def async_update(self): """Update bulb status (if it is available).""" - _LOGGER.debug("%s async_update", self.ipaddr) + _LOGGER.debug("%s async_update", self.who) if self.available and self.blocker is None: self.updated_event.clear() self.device.get_color(self.got_color)