2019-02-13 20:21:14 +00:00
|
|
|
"""Support for IKEA Tradfri."""
|
2018-09-19 19:21:43 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-13 20:56:01 +00:00
|
|
|
from pytradfri import Gateway, RequestError
|
|
|
|
from pytradfri.api.aiocoap_api import APIFactory
|
2019-12-01 05:23:39 +00:00
|
|
|
import voluptuous as vol
|
2018-09-19 19:21:43 +00:00
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2018-10-05 07:59:34 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2019-10-24 19:31:58 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-12-01 05:23:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-09-19 19:21:43 +00:00
|
|
|
from homeassistant.util.json import load_json
|
2019-12-01 05:23:39 +00:00
|
|
|
|
2019-11-16 09:22:07 +00:00
|
|
|
from . import config_flow # noqa: F401
|
2018-09-21 12:47:52 +00:00
|
|
|
from .const import (
|
2019-10-09 19:56:16 +00:00
|
|
|
ATTR_TRADFRI_GATEWAY,
|
|
|
|
ATTR_TRADFRI_GATEWAY_MODEL,
|
2019-12-01 05:23:39 +00:00
|
|
|
ATTR_TRADFRI_MANUFACTURER,
|
|
|
|
CONF_ALLOW_TRADFRI_GROUPS,
|
|
|
|
CONF_GATEWAY_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
2019-12-01 05:23:39 +00:00
|
|
|
CONF_IDENTITY,
|
|
|
|
CONF_IMPORT_GROUPS,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_KEY,
|
2019-12-01 05:23:39 +00:00
|
|
|
CONFIG_FILE,
|
|
|
|
DEFAULT_ALLOW_TRADFRI_GROUPS,
|
|
|
|
DOMAIN,
|
|
|
|
KEY_API,
|
|
|
|
KEY_GATEWAY,
|
|
|
|
TRADFRI_DEVICE_TYPES,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-19 19:21:43 +00:00
|
|
|
|
2019-02-13 20:21:14 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_ALLOW_TRADFRI_GROUPS, default=DEFAULT_ALLOW_TRADFRI_GROUPS
|
|
|
|
): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-09-19 19:21:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the Tradfri component."""
|
|
|
|
conf = config.get(DOMAIN)
|
|
|
|
|
|
|
|
if conf is None:
|
|
|
|
return True
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
configured_hosts = [
|
|
|
|
entry.data["host"] for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
]
|
2018-09-26 16:03:25 +00:00
|
|
|
|
|
|
|
legacy_hosts = await hass.async_add_executor_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
load_json, hass.config.path(CONFIG_FILE)
|
|
|
|
)
|
2018-09-19 19:21:43 +00:00
|
|
|
|
2018-09-26 16:03:25 +00:00
|
|
|
for host, info in legacy_hosts.items():
|
|
|
|
if host in configured_hosts:
|
|
|
|
continue
|
|
|
|
|
2018-09-19 19:21:43 +00:00
|
|
|
info[CONF_HOST] = host
|
|
|
|
info[CONF_IMPORT_GROUPS] = conf[CONF_ALLOW_TRADFRI_GROUPS]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=info
|
|
|
|
)
|
|
|
|
)
|
2018-09-19 19:21:43 +00:00
|
|
|
|
|
|
|
host = conf.get(CONF_HOST)
|
2018-10-11 08:37:34 +00:00
|
|
|
import_groups = conf[CONF_ALLOW_TRADFRI_GROUPS]
|
2018-09-19 19:21:43 +00:00
|
|
|
|
2018-09-26 16:03:25 +00:00
|
|
|
if host is None or host in configured_hosts or host in legacy_hosts:
|
2018-09-19 19:21:43 +00:00
|
|
|
return True
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={CONF_HOST: host, CONF_IMPORT_GROUPS: import_groups},
|
|
|
|
)
|
|
|
|
)
|
2018-09-19 19:21:43 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Create a gateway."""
|
|
|
|
# host, identity, key, allow_tradfri_groups
|
|
|
|
|
|
|
|
factory = APIFactory(
|
|
|
|
entry.data[CONF_HOST],
|
|
|
|
psk_id=entry.data[CONF_IDENTITY],
|
|
|
|
psk=entry.data[CONF_KEY],
|
2019-07-31 19:25:30 +00:00
|
|
|
loop=hass.loop,
|
2018-09-19 19:21:43 +00:00
|
|
|
)
|
2018-10-05 07:59:34 +00:00
|
|
|
|
|
|
|
async def on_hass_stop(event):
|
|
|
|
"""Close connection when hass stops."""
|
|
|
|
await factory.shutdown()
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
|
|
|
|
|
2018-09-19 19:21:43 +00:00
|
|
|
api = factory.request
|
|
|
|
gateway = Gateway()
|
|
|
|
|
|
|
|
try:
|
2018-09-21 12:47:52 +00:00
|
|
|
gateway_info = await api(gateway.get_gateway_info())
|
2018-09-19 19:21:43 +00:00
|
|
|
except RequestError:
|
2019-10-24 19:31:58 +00:00
|
|
|
await factory.shutdown()
|
|
|
|
raise ConfigEntryNotReady
|
2018-09-19 19:21:43 +00:00
|
|
|
|
|
|
|
hass.data.setdefault(KEY_API, {})[entry.entry_id] = api
|
|
|
|
hass.data.setdefault(KEY_GATEWAY, {})[entry.entry_id] = gateway
|
|
|
|
|
2018-09-21 12:47:52 +00:00
|
|
|
dev_reg = await hass.helpers.device_registry.async_get_registry()
|
|
|
|
dev_reg.async_get_or_create(
|
|
|
|
config_entry_id=entry.entry_id,
|
|
|
|
connections=set(),
|
2019-07-31 19:25:30 +00:00
|
|
|
identifiers={(DOMAIN, entry.data[CONF_GATEWAY_ID])},
|
2019-10-09 19:56:16 +00:00
|
|
|
manufacturer=ATTR_TRADFRI_MANUFACTURER,
|
|
|
|
name=ATTR_TRADFRI_GATEWAY,
|
2018-09-21 12:47:52 +00:00
|
|
|
# They just have 1 gateway model. Type is not exposed yet.
|
2019-10-09 19:56:16 +00:00
|
|
|
model=ATTR_TRADFRI_GATEWAY_MODEL,
|
2018-09-21 12:47:52 +00:00
|
|
|
sw_version=gateway_info.firmware_version,
|
|
|
|
)
|
|
|
|
|
2019-10-09 19:56:16 +00:00
|
|
|
for device in TRADFRI_DEVICE_TYPES:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, device)
|
|
|
|
)
|
2018-09-19 19:21:43 +00:00
|
|
|
|
|
|
|
return True
|