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.
This commit is contained in:
Anders Melchiorsen 2017-04-17 02:40:22 +02:00 committed by Paulus Schoutsen
parent 103377bdb0
commit d4b05a6a85

View File

@ -82,9 +82,8 @@ class LIFXManager(object):
"""Callback for newly detected bulb.""" """Callback for newly detected bulb."""
if device.mac_addr in self.entities: if device.mac_addr in self.entities:
entity = self.entities[device.mac_addr] entity = self.entities[device.mac_addr]
_LOGGER.debug("%s register AGAIN", entity.ipaddr)
entity.available = True
entity.device = device entity.device = device
_LOGGER.debug("%s register AGAIN", entity.who)
self.hass.async_add_job(entity.async_update_ha_state()) self.hass.async_add_job(entity.async_update_ha_state())
else: else:
_LOGGER.debug("%s register NEW", device.ip_addr) _LOGGER.debug("%s register NEW", device.ip_addr)
@ -94,7 +93,7 @@ class LIFXManager(object):
def ready(self, device, msg): def ready(self, device, msg):
"""Callback that adds the device once all data is retrieved.""" """Callback that adds the device once all data is retrieved."""
entity = LIFXLight(device) entity = LIFXLight(device)
_LOGGER.debug("%s register READY", entity.ipaddr) _LOGGER.debug("%s register READY", entity.who)
self.entities[device.mac_addr] = entity self.entities[device.mac_addr] = entity
self.async_add_devices([entity]) self.async_add_devices([entity])
@ -103,8 +102,8 @@ class LIFXManager(object):
"""Callback for disappearing bulb.""" """Callback for disappearing bulb."""
if device.mac_addr in self.entities: if device.mac_addr in self.entities:
entity = self.entities[device.mac_addr] entity = self.entities[device.mac_addr]
_LOGGER.debug("%s unregister", entity.ipaddr) _LOGGER.debug("%s unregister", entity.who)
entity.available = False entity.device = None
entity.updated_event.set() entity.updated_event.set()
self.hass.async_add_job(entity.async_update_ha_state()) self.hass.async_add_job(entity.async_update_ha_state())
@ -129,29 +128,27 @@ class LIFXLight(Light):
self.updated_event = asyncio.Event() self.updated_event = asyncio.Event()
self.blocker = None self.blocker = None
self.postponed_update = None self.postponed_update = None
self._available = True self._name = device.label
self.set_power(device.power_level) self.set_power(device.power_level)
self.set_color(*device.color) self.set_color(*device.color)
@property @property
def available(self): def available(self):
"""Return the availability of the device.""" """Return the availability of the device."""
return self._available return self.device is not None
@available.setter
def available(self, value):
"""Set the availability of the device."""
self._available = value
@property @property
def name(self): def name(self):
"""Return the name of the device.""" """Return the name of the device."""
return self.device.label return self._name
@property @property
def ipaddr(self): def who(self):
"""Return the IP address of the device.""" """Return a string identifying the device."""
return self.device.ip_addr[0] if self.device:
return self.device.ip_addr[0]
else:
return "(%s)" % self.name
@property @property
def rgb_color(self): def rgb_color(self):
@ -247,7 +244,7 @@ class LIFXLight(Light):
hsbk = [hue, saturation, brightness, kelvin] hsbk = [hue, saturation, brightness, kelvin]
_LOGGER.debug("turn_on: %s (%d) %d %d %d %d %d", _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 self._power == 0:
if changed_color: if changed_color:
@ -282,12 +279,13 @@ class LIFXLight(Light):
"""Callback that gets current power/color status.""" """Callback that gets current power/color status."""
self.set_power(device.power_level) self.set_power(device.power_level)
self.set_color(*device.color) self.set_color(*device.color)
self._name = device.label
self.updated_event.set() self.updated_event.set()
@asyncio.coroutine @asyncio.coroutine
def async_update(self): def async_update(self):
"""Update bulb status (if it is available).""" """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: if self.available and self.blocker is None:
self.updated_event.clear() self.updated_event.clear()
self.device.get_color(self.got_color) self.device.get_color(self.got_color)