2018-10-07 21:14:53 +00:00
|
|
|
"""Component to embed LIFX."""
|
|
|
|
import voluptuous as vol
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.helpers import config_entry_flow
|
|
|
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
DOMAIN = 'lifx'
|
2018-12-07 06:06:35 +00:00
|
|
|
REQUIREMENTS = ['aiolifx==0.6.7']
|
2018-10-07 21:14:53 +00:00
|
|
|
|
|
|
|
CONF_SERVER = 'server'
|
|
|
|
CONF_BROADCAST = 'broadcast'
|
|
|
|
|
2018-10-17 08:50:13 +00:00
|
|
|
INTERFACE_SCHEMA = vol.Schema({
|
|
|
|
vol.Optional(CONF_SERVER): cv.string,
|
|
|
|
vol.Optional(CONF_BROADCAST): cv.string,
|
|
|
|
})
|
|
|
|
|
2018-10-07 21:14:53 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: {
|
2018-10-17 08:50:13 +00:00
|
|
|
LIGHT_DOMAIN:
|
|
|
|
vol.Schema(vol.All(cv.ensure_list, [INTERFACE_SCHEMA])),
|
2018-10-07 21:14:53 +00:00
|
|
|
}
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2018-11-18 18:51:17 +00:00
|
|
|
DATA_LIFX_MANAGER = 'lifx_manager'
|
|
|
|
|
2018-10-07 21:14:53 +00:00
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the LIFX component."""
|
|
|
|
conf = config.get(DOMAIN)
|
|
|
|
|
|
|
|
hass.data[DOMAIN] = conf or {}
|
|
|
|
|
|
|
|
if conf is not None:
|
|
|
|
hass.async_create_task(hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={'source': config_entries.SOURCE_IMPORT}))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Set up LIFX from a config entry."""
|
|
|
|
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
|
|
|
|
entry, LIGHT_DOMAIN))
|
2018-11-18 18:51:17 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
hass.data.pop(DATA_LIFX_MANAGER).cleanup()
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, LIGHT_DOMAIN)
|
|
|
|
|
2018-10-07 21:14:53 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_has_devices(hass):
|
|
|
|
"""Return if there are devices that can be discovered."""
|
|
|
|
import aiolifx
|
|
|
|
|
2018-10-17 08:50:13 +00:00
|
|
|
lifx_ip_addresses = await aiolifx.LifxScan(hass.loop).scan()
|
|
|
|
return len(lifx_ip_addresses) > 0
|
2018-10-07 21:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
config_entry_flow.register_discovery_flow(
|
|
|
|
DOMAIN, 'LIFX', _async_has_devices, config_entries.CONN_CLASS_LOCAL_POLL)
|