mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Move LIFX to aiolifx for driving the bulbs (#6584)
* Move LIFX to aiolifx for driving the bulbs * Fix whitespace * Fix more whitespace * Fix lint * Define _available in init * Add @callback decorators * Use hass.async_add_job * Rename class
This commit is contained in:
parent
95b1e257bb
commit
9ef084d903
@ -6,6 +6,9 @@ https://home-assistant.io/components/light.lifx/
|
|||||||
"""
|
"""
|
||||||
import colorsys
|
import colorsys
|
||||||
import logging
|
import logging
|
||||||
|
import asyncio
|
||||||
|
from functools import partial
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -13,117 +16,90 @@ from homeassistant.components.light import (
|
|||||||
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
|
||||||
SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR,
|
SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR,
|
||||||
SUPPORT_TRANSITION, Light, PLATFORM_SCHEMA)
|
SUPPORT_TRANSITION, Light, PLATFORM_SCHEMA)
|
||||||
from homeassistant.helpers.event import track_time_change
|
|
||||||
from homeassistant.util.color import (
|
from homeassistant.util.color import (
|
||||||
color_temperature_mired_to_kelvin, color_temperature_kelvin_to_mired)
|
color_temperature_mired_to_kelvin, color_temperature_kelvin_to_mired)
|
||||||
|
from homeassistant import util
|
||||||
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
REQUIREMENTS = ['liffylights==0.9.4']
|
REQUIREMENTS = ['aiolifx==0.4.1.post1']
|
||||||
|
|
||||||
BYTE_MAX = 255
|
UDP_BROADCAST_PORT = 56700
|
||||||
|
|
||||||
|
# Delay (in ms) expected for changes to take effect in the physical bulb
|
||||||
|
BULB_LATENCY = 500
|
||||||
|
|
||||||
CONF_BROADCAST = 'broadcast'
|
|
||||||
CONF_SERVER = 'server'
|
CONF_SERVER = 'server'
|
||||||
|
|
||||||
|
BYTE_MAX = 255
|
||||||
SHORT_MAX = 65535
|
SHORT_MAX = 65535
|
||||||
|
|
||||||
TEMP_MAX = 9000
|
|
||||||
TEMP_MAX_HASS = 500
|
|
||||||
TEMP_MIN = 2500
|
|
||||||
TEMP_MIN_HASS = 154
|
|
||||||
|
|
||||||
SUPPORT_LIFX = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR |
|
SUPPORT_LIFX = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR |
|
||||||
SUPPORT_TRANSITION)
|
SUPPORT_TRANSITION)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_SERVER, default=None): cv.string,
|
vol.Optional(CONF_SERVER, default='0.0.0.0'): cv.string,
|
||||||
vol.Optional(CONF_BROADCAST, default=None): cv.string,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
@asyncio.coroutine
|
||||||
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Setup the LIFX platform."""
|
"""Setup the LIFX platform."""
|
||||||
|
import aiolifx
|
||||||
|
|
||||||
server_addr = config.get(CONF_SERVER)
|
server_addr = config.get(CONF_SERVER)
|
||||||
broadcast_addr = config.get(CONF_BROADCAST)
|
|
||||||
|
|
||||||
lifx_library = LIFX(add_devices, server_addr, broadcast_addr)
|
lifx_manager = LIFXManager(hass, async_add_devices)
|
||||||
|
|
||||||
# Register our poll service
|
coro = hass.loop.create_datagram_endpoint(
|
||||||
track_time_change(hass, lifx_library.poll, second=[10, 40])
|
partial(aiolifx.LifxDiscovery, hass.loop, lifx_manager),
|
||||||
|
local_addr=(server_addr, UDP_BROADCAST_PORT))
|
||||||
|
|
||||||
lifx_library.probe()
|
hass.async_add_job(coro)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class LIFX(object):
|
class LIFXManager(object):
|
||||||
"""Representation of a LIFX light."""
|
"""Representation of all known LIFX entities."""
|
||||||
|
|
||||||
def __init__(self, add_devices_callback, server_addr=None,
|
def __init__(self, hass, async_add_devices):
|
||||||
broadcast_addr=None):
|
|
||||||
"""Initialize the light."""
|
"""Initialize the light."""
|
||||||
import liffylights
|
self.entities = {}
|
||||||
|
self.hass = hass
|
||||||
|
self.async_add_devices = async_add_devices
|
||||||
|
|
||||||
self._devices = []
|
@callback
|
||||||
|
def register(self, device):
|
||||||
self._add_devices_callback = add_devices_callback
|
"""Callback for newly detected bulb."""
|
||||||
|
if device.mac_addr in self.entities:
|
||||||
self._liffylights = liffylights.LiffyLights(
|
entity = self.entities[device.mac_addr]
|
||||||
self.on_device, self.on_power, self.on_color, server_addr,
|
_LOGGER.debug("%s register AGAIN", entity.ipaddr)
|
||||||
broadcast_addr)
|
entity.available = True
|
||||||
|
self.hass.async_add_job(entity.async_update_ha_state())
|
||||||
def find_bulb(self, ipaddr):
|
|
||||||
"""Search for bulbs."""
|
|
||||||
bulb = None
|
|
||||||
for device in self._devices:
|
|
||||||
if device.ipaddr == ipaddr:
|
|
||||||
bulb = device
|
|
||||||
break
|
|
||||||
return bulb
|
|
||||||
|
|
||||||
def on_device(self, ipaddr, name, power, hue, sat, bri, kel):
|
|
||||||
"""Initialize the light."""
|
|
||||||
bulb = self.find_bulb(ipaddr)
|
|
||||||
|
|
||||||
if bulb is None:
|
|
||||||
_LOGGER.debug("new bulb %s %s %d %d %d %d %d",
|
|
||||||
ipaddr, name, power, hue, sat, bri, kel)
|
|
||||||
bulb = LIFXLight(
|
|
||||||
self._liffylights, ipaddr, name, power, hue, sat, bri, kel)
|
|
||||||
self._devices.append(bulb)
|
|
||||||
self._add_devices_callback([bulb])
|
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug("update bulb %s %s %d %d %d %d %d",
|
_LOGGER.debug("%s register NEW", device.ip_addr)
|
||||||
ipaddr, name, power, hue, sat, bri, kel)
|
device.get_color(self.ready)
|
||||||
bulb.set_power(power)
|
|
||||||
bulb.set_color(hue, sat, bri, kel)
|
|
||||||
bulb.schedule_update_ha_state()
|
|
||||||
|
|
||||||
def on_color(self, ipaddr, hue, sat, bri, kel):
|
@callback
|
||||||
"""Initialize the light."""
|
def ready(self, device, msg):
|
||||||
bulb = self.find_bulb(ipaddr)
|
"""Callback that adds the device once all data is retrieved."""
|
||||||
|
entity = LIFXLight(device)
|
||||||
|
_LOGGER.debug("%s register READY", entity.ipaddr)
|
||||||
|
self.entities[device.mac_addr] = entity
|
||||||
|
self.hass.async_add_job(self.async_add_devices, [entity])
|
||||||
|
|
||||||
if bulb is not None:
|
@callback
|
||||||
bulb.set_color(hue, sat, bri, kel)
|
def unregister(self, device):
|
||||||
bulb.schedule_update_ha_state()
|
"""Callback for disappearing bulb."""
|
||||||
|
entity = self.entities[device.mac_addr]
|
||||||
def on_power(self, ipaddr, power):
|
_LOGGER.debug("%s unregister", entity.ipaddr)
|
||||||
"""Initialize the light."""
|
entity.available = False
|
||||||
bulb = self.find_bulb(ipaddr)
|
entity.updated_event.set()
|
||||||
|
self.hass.async_add_job(entity.async_update_ha_state())
|
||||||
if bulb is not None:
|
|
||||||
bulb.set_power(power)
|
|
||||||
bulb.schedule_update_ha_state()
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
|
||||||
def poll(self, now):
|
|
||||||
"""Polling for the light."""
|
|
||||||
self.probe()
|
|
||||||
|
|
||||||
def probe(self, address=None):
|
|
||||||
"""Probe the light."""
|
|
||||||
self._liffylights.probe(address)
|
|
||||||
|
|
||||||
|
|
||||||
def convert_rgb_to_hsv(rgb):
|
def convert_rgb_to_hsv(rgb):
|
||||||
@ -140,31 +116,35 @@ def convert_rgb_to_hsv(rgb):
|
|||||||
class LIFXLight(Light):
|
class LIFXLight(Light):
|
||||||
"""Representation of a LIFX light."""
|
"""Representation of a LIFX light."""
|
||||||
|
|
||||||
def __init__(self, liffy, ipaddr, name, power, hue, saturation, brightness,
|
def __init__(self, device):
|
||||||
kelvin):
|
|
||||||
"""Initialize the light."""
|
"""Initialize the light."""
|
||||||
_LOGGER.debug("LIFXLight: %s %s", ipaddr, name)
|
self.device = device
|
||||||
|
self.updated_event = asyncio.Event()
|
||||||
self._liffylights = liffy
|
self.blocker = None
|
||||||
self._ip = ipaddr
|
self.postponed_update = None
|
||||||
self.set_name(name)
|
self._available = True
|
||||||
self.set_power(power)
|
self.set_power(device.power_level)
|
||||||
self.set_color(hue, saturation, brightness, kelvin)
|
self.set_color(*device.color)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def available(self):
|
||||||
"""No polling needed for LIFX light."""
|
"""Return the availability of the device."""
|
||||||
return False
|
return self._available
|
||||||
|
|
||||||
|
@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._name
|
return self.device.label
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ipaddr(self):
|
def ipaddr(self):
|
||||||
"""Return the IP address of the device."""
|
"""Return the IP address of the device."""
|
||||||
return self._ip
|
return self.device.ip_addr[0]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def rgb_color(self):
|
def rgb_color(self):
|
||||||
@ -199,16 +179,47 @@ class LIFXLight(Light):
|
|||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
return SUPPORT_LIFX
|
return SUPPORT_LIFX
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
@callback
|
||||||
|
def update_after_transition(self, now):
|
||||||
|
"""Request new status after completion of the last transition."""
|
||||||
|
self.postponed_update = None
|
||||||
|
self.hass.async_add_job(self.async_update_ha_state(force_refresh=True))
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def unblock_updates(self, now):
|
||||||
|
"""Allow async_update after the new state has settled on the bulb."""
|
||||||
|
self.blocker = None
|
||||||
|
self.hass.async_add_job(self.async_update_ha_state(force_refresh=True))
|
||||||
|
|
||||||
|
def update_later(self, when):
|
||||||
|
"""Block immediate update requests and schedule one for later."""
|
||||||
|
if self.blocker:
|
||||||
|
self.blocker()
|
||||||
|
self.blocker = async_track_point_in_utc_time(
|
||||||
|
self.hass, self.unblock_updates,
|
||||||
|
util.dt.utcnow() + timedelta(milliseconds=BULB_LATENCY))
|
||||||
|
|
||||||
|
if self.postponed_update:
|
||||||
|
self.postponed_update()
|
||||||
|
if when > BULB_LATENCY:
|
||||||
|
self.postponed_update = async_track_point_in_utc_time(
|
||||||
|
self.hass, self.update_after_transition,
|
||||||
|
util.dt.utcnow() + timedelta(milliseconds=when+BULB_LATENCY))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
fade = int(kwargs[ATTR_TRANSITION] * 1000)
|
fade = int(kwargs[ATTR_TRANSITION] * 1000)
|
||||||
else:
|
else:
|
||||||
fade = 0
|
fade = 0
|
||||||
|
|
||||||
|
changed_color = False
|
||||||
|
|
||||||
if ATTR_RGB_COLOR in kwargs:
|
if ATTR_RGB_COLOR in kwargs:
|
||||||
hue, saturation, brightness = \
|
hue, saturation, brightness = \
|
||||||
convert_rgb_to_hsv(kwargs[ATTR_RGB_COLOR])
|
convert_rgb_to_hsv(kwargs[ATTR_RGB_COLOR])
|
||||||
|
changed_color = True
|
||||||
else:
|
else:
|
||||||
hue = self._hue
|
hue = self._hue
|
||||||
saturation = self._sat
|
saturation = self._sat
|
||||||
@ -216,40 +227,64 @@ class LIFXLight(Light):
|
|||||||
|
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
brightness = kwargs[ATTR_BRIGHTNESS] * (BYTE_MAX + 1)
|
brightness = kwargs[ATTR_BRIGHTNESS] * (BYTE_MAX + 1)
|
||||||
|
changed_color = True
|
||||||
else:
|
else:
|
||||||
brightness = self._bri
|
brightness = self._bri
|
||||||
|
|
||||||
if ATTR_COLOR_TEMP in kwargs:
|
if ATTR_COLOR_TEMP in kwargs:
|
||||||
kelvin = int(color_temperature_mired_to_kelvin(
|
kelvin = int(color_temperature_mired_to_kelvin(
|
||||||
kwargs[ATTR_COLOR_TEMP]))
|
kwargs[ATTR_COLOR_TEMP]))
|
||||||
|
changed_color = True
|
||||||
else:
|
else:
|
||||||
kelvin = self._kel
|
kelvin = self._kel
|
||||||
|
|
||||||
|
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._ip, self._power,
|
self.ipaddr, self._power, fade, *hsbk)
|
||||||
hue, saturation, brightness, kelvin, fade)
|
|
||||||
|
|
||||||
if self._power == 0:
|
if self._power == 0:
|
||||||
self._liffylights.set_color(self._ip, hue, saturation,
|
if changed_color:
|
||||||
brightness, kelvin, 0)
|
self.device.set_color(hsbk, None, 0)
|
||||||
self._liffylights.set_power(self._ip, 65535, fade)
|
self.device.set_power(True, None, fade)
|
||||||
else:
|
else:
|
||||||
self._liffylights.set_color(self._ip, hue, saturation,
|
self.device.set_power(True, None, 0) # racing for power status
|
||||||
brightness, kelvin, fade)
|
if changed_color:
|
||||||
|
self.device.set_color(hsbk, None, fade)
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
self.update_later(0)
|
||||||
|
if fade < BULB_LATENCY:
|
||||||
|
self.set_power(1)
|
||||||
|
self.set_color(*hsbk)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
if ATTR_TRANSITION in kwargs:
|
if ATTR_TRANSITION in kwargs:
|
||||||
fade = int(kwargs[ATTR_TRANSITION] * 1000)
|
fade = int(kwargs[ATTR_TRANSITION] * 1000)
|
||||||
else:
|
else:
|
||||||
fade = 0
|
fade = 0
|
||||||
|
|
||||||
_LOGGER.debug("turn_off: %s %d", self._ip, fade)
|
self.device.set_power(False, None, fade)
|
||||||
self._liffylights.set_power(self._ip, 0, fade)
|
|
||||||
|
|
||||||
def set_name(self, name):
|
self.update_later(fade)
|
||||||
"""Set name of the light."""
|
if fade < BULB_LATENCY:
|
||||||
self._name = name
|
self.set_power(0)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def got_color(self, device, msg):
|
||||||
|
"""Callback that gets current power/color status."""
|
||||||
|
self.set_power(device.power_level)
|
||||||
|
self.set_color(*device.color)
|
||||||
|
self.updated_event.set()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_update(self):
|
||||||
|
"""Update bulb status (if it is available)."""
|
||||||
|
_LOGGER.debug("%s async_update", self.ipaddr)
|
||||||
|
if self.available and self.blocker is None:
|
||||||
|
self.updated_event.clear()
|
||||||
|
self.device.get_color(self.got_color)
|
||||||
|
yield from self.updated_event.wait()
|
||||||
|
|
||||||
def set_power(self, power):
|
def set_power(self, power):
|
||||||
"""Set power state value."""
|
"""Set power state value."""
|
||||||
|
@ -40,6 +40,9 @@ aiodns==1.1.1
|
|||||||
# homeassistant.components.http
|
# homeassistant.components.http
|
||||||
aiohttp_cors==0.5.0
|
aiohttp_cors==0.5.0
|
||||||
|
|
||||||
|
# homeassistant.components.light.lifx
|
||||||
|
aiolifx==0.4.1.post1
|
||||||
|
|
||||||
# homeassistant.components.camera.amcrest
|
# homeassistant.components.camera.amcrest
|
||||||
# homeassistant.components.sensor.amcrest
|
# homeassistant.components.sensor.amcrest
|
||||||
amcrest==1.1.4
|
amcrest==1.1.4
|
||||||
@ -334,9 +337,6 @@ libnacl==1.5.0
|
|||||||
# homeassistant.components.media_player.soundtouch
|
# homeassistant.components.media_player.soundtouch
|
||||||
libsoundtouch==0.1.0
|
libsoundtouch==0.1.0
|
||||||
|
|
||||||
# homeassistant.components.light.lifx
|
|
||||||
liffylights==0.9.4
|
|
||||||
|
|
||||||
# homeassistant.components.light.limitlessled
|
# homeassistant.components.light.limitlessled
|
||||||
limitlessled==1.0.5
|
limitlessled==1.0.5
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user