2019-12-10 00:00:04 -05:00
|
|
|
"""Support for Zigbee Home Automation devices."""
|
2019-12-09 18:54:54 +01:00
|
|
|
|
2020-02-21 18:06:57 -05:00
|
|
|
import asyncio
|
2017-04-24 22:24:57 -07:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
from homeassistant import config_entries, const as ha_const
|
2018-12-04 05:38:57 -05:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-12-01 10:31:49 +01:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
2017-04-24 22:24:57 -07:00
|
|
|
|
2019-01-11 14:34:29 -05:00
|
|
|
from . import api
|
2019-02-06 13:33:21 -05:00
|
|
|
from .core import ZHAGateway
|
|
|
|
from .core.const import (
|
2019-07-31 12:25:30 -07:00
|
|
|
COMPONENTS,
|
|
|
|
CONF_BAUDRATE,
|
|
|
|
CONF_DATABASE,
|
|
|
|
CONF_DEVICE_CONFIG,
|
2019-08-02 10:37:21 -04:00
|
|
|
CONF_ENABLE_QUIRKS,
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_RADIO_TYPE,
|
|
|
|
CONF_USB_PATH,
|
|
|
|
DATA_ZHA,
|
|
|
|
DATA_ZHA_CONFIG,
|
|
|
|
DATA_ZHA_DISPATCHERS,
|
|
|
|
DATA_ZHA_GATEWAY,
|
2020-02-21 18:06:57 -05:00
|
|
|
DATA_ZHA_PLATFORM_LOADED,
|
2019-07-31 12:25:30 -07:00
|
|
|
DEFAULT_BAUDRATE,
|
|
|
|
DEFAULT_RADIO_TYPE,
|
|
|
|
DOMAIN,
|
|
|
|
RadioType,
|
|
|
|
)
|
2018-11-27 21:21:25 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEVICE_CONFIG_SCHEMA_ENTRY = vol.Schema({vol.Optional(ha_const.CONF_TYPE): cv.string})
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_RADIO_TYPE, default=DEFAULT_RADIO_TYPE): cv.enum(
|
|
|
|
RadioType
|
|
|
|
),
|
|
|
|
CONF_USB_PATH: cv.string,
|
|
|
|
vol.Optional(CONF_BAUDRATE, default=DEFAULT_BAUDRATE): cv.positive_int,
|
|
|
|
vol.Optional(CONF_DATABASE): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_CONFIG, default={}): vol.Schema(
|
|
|
|
{cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}
|
|
|
|
),
|
2019-08-02 06:05:23 -04:00
|
|
|
vol.Optional(CONF_ENABLE_QUIRKS, default=True): cv.boolean,
|
2019-07-31 12:25:30 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2017-04-24 22:24:57 -07:00
|
|
|
|
2018-10-24 14:12:30 +02:00
|
|
|
# Zigbee definitions
|
2019-07-31 12:25:30 -07:00
|
|
|
CENTICELSIUS = "C-100"
|
2017-04-24 22:24:57 -07:00
|
|
|
|
|
|
|
# Internal definitions
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-03-12 21:57:13 +01:00
|
|
|
async def async_setup(hass, config):
|
2018-11-27 21:21:25 +01:00
|
|
|
"""Set up ZHA from config."""
|
|
|
|
hass.data[DATA_ZHA] = {}
|
|
|
|
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_CONFIG] = conf
|
|
|
|
|
|
|
|
if not hass.config_entries.async_entries(DOMAIN):
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={
|
|
|
|
CONF_USB_PATH: conf[CONF_USB_PATH],
|
|
|
|
CONF_RADIO_TYPE: conf.get(CONF_RADIO_TYPE).value,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2018-11-27 21:21:25 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up ZHA.
|
2017-04-24 22:24:57 -07:00
|
|
|
|
|
|
|
Will automatically load components to support devices found on the network.
|
|
|
|
"""
|
2019-02-06 13:33:21 -05:00
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
hass.data[DATA_ZHA] = hass.data.get(DATA_ZHA, {})
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS] = []
|
2020-02-21 18:06:57 -05:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_PLATFORM_LOADED] = asyncio.Event()
|
|
|
|
platforms = []
|
|
|
|
for component in COMPONENTS:
|
|
|
|
platforms.append(
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, component)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _platforms_loaded():
|
|
|
|
await asyncio.gather(*platforms)
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_PLATFORM_LOADED].set()
|
|
|
|
|
|
|
|
hass.async_create_task(_platforms_loaded())
|
|
|
|
|
2018-11-27 21:21:25 +01:00
|
|
|
config = hass.data[DATA_ZHA].get(DATA_ZHA_CONFIG, {})
|
|
|
|
|
2019-08-02 06:05:23 -04:00
|
|
|
if config.get(CONF_ENABLE_QUIRKS, True):
|
2019-01-04 14:00:26 -05:00
|
|
|
# needs to be done here so that the ZHA module is finished loading
|
|
|
|
# before zhaquirks is imported
|
2019-12-09 18:54:54 +01:00
|
|
|
import zhaquirks # noqa: F401 pylint: disable=unused-import, import-outside-toplevel, import-error
|
2019-01-04 14:00:26 -05:00
|
|
|
|
2019-07-16 18:16:49 -04:00
|
|
|
zha_gateway = ZHAGateway(hass, config, config_entry)
|
|
|
|
await zha_gateway.async_initialize()
|
2017-04-24 22:24:57 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
2018-12-01 10:31:49 +01:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
2019-07-31 12:25:30 -07:00
|
|
|
connections={(CONNECTION_ZIGBEE, str(zha_gateway.application_controller.ieee))},
|
|
|
|
identifiers={(DOMAIN, str(zha_gateway.application_controller.ieee))},
|
2018-12-01 10:31:49 +01:00
|
|
|
name="Zigbee Coordinator",
|
|
|
|
manufacturer="ZHA",
|
2019-03-14 10:20:25 -04:00
|
|
|
model=zha_gateway.radio_description,
|
2018-12-01 10:31:49 +01:00
|
|
|
)
|
|
|
|
|
2019-03-14 10:20:25 -04:00
|
|
|
api.async_load_api(hass)
|
2018-02-11 20:34:19 -08:00
|
|
|
|
2019-03-09 23:09:09 -05:00
|
|
|
async def async_zha_shutdown(event):
|
|
|
|
"""Handle shutdown tasks."""
|
2019-04-03 09:40:48 -04:00
|
|
|
await hass.data[DATA_ZHA][DATA_ZHA_GATEWAY].shutdown()
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.data[DATA_ZHA][DATA_ZHA_GATEWAY].async_update_device_storage()
|
2018-11-27 21:21:25 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.bus.async_listen_once(ha_const.EVENT_HOMEASSISTANT_STOP, async_zha_shutdown)
|
2020-02-21 18:06:57 -05:00
|
|
|
hass.async_create_task(zha_gateway.async_load_devices())
|
2018-11-27 21:21:25 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload ZHA config entry."""
|
2019-04-03 09:40:48 -04:00
|
|
|
await hass.data[DATA_ZHA][DATA_ZHA_GATEWAY].shutdown()
|
|
|
|
|
2019-01-11 14:34:29 -05:00
|
|
|
api.async_unload_api(hass)
|
2018-11-27 21:21:25 +01:00
|
|
|
|
|
|
|
dispatchers = hass.data[DATA_ZHA].get(DATA_ZHA_DISPATCHERS, [])
|
|
|
|
for unsub_dispatcher in dispatchers:
|
|
|
|
unsub_dispatcher()
|
|
|
|
|
|
|
|
for component in COMPONENTS:
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, component)
|
2018-11-27 21:21:25 +01:00
|
|
|
|
2017-04-24 22:24:57 -07:00
|
|
|
return True
|