Add config entry for ZHA (#18352)
* Add support for zha config entries * Add support for zha config entries * Fix node_config retrieval * Dynamically load discovered entities * Restore device config support * Refactor loading of entities * Remove device registry support * Send discovery_info directly * Clean up discovery_info in hass.data * Update tests * Clean up rebase * Simplify config flow * Address comments * Fix config path and zigpy check timeout * Remove device entities when unloading config entry
This commit is contained in:
1 parent
43676fcaf4
commit
052d305243
14 files changed
+567
-135
No files matched your search
@@ -8,6 +8,10 @@ import logging
|
||||
from homeassistant.components import light
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.components.zha.const import (
|
||||
ZHA_DISCOVERY_NEW, DATA_ZHA, DATA_ZHA_DISPATCHERS
|
||||
)
|
||||
import homeassistant.util.color as color_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -24,27 +28,54 @@ UNSUPPORTED_ATTRIBUTE = 0x86
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the Zigbee Home Automation lights."""
|
||||
discovery_info = helpers.get_discovery_info(hass, discovery_info)
|
||||
if discovery_info is None:
|
||||
return
|
||||
"""Old way of setting up Zigbee Home Automation lights."""
|
||||
pass
|
||||
|
||||
endpoint = discovery_info['endpoint']
|
||||
if hasattr(endpoint, 'light_color'):
|
||||
caps = await helpers.safe_read(
|
||||
endpoint.light_color, ['color_capabilities'])
|
||||
discovery_info['color_capabilities'] = caps.get('color_capabilities')
|
||||
if discovery_info['color_capabilities'] is None:
|
||||
# ZCL Version 4 devices don't support the color_capabilities
|
||||
# attribute. In this version XY support is mandatory, but we need
|
||||
# to probe to determine if the device supports color temperature.
|
||||
discovery_info['color_capabilities'] = CAPABILITIES_COLOR_XY
|
||||
result = await helpers.safe_read(
|
||||
endpoint.light_color, ['color_temperature'])
|
||||
if result.get('color_temperature') is not UNSUPPORTED_ATTRIBUTE:
|
||||
discovery_info['color_capabilities'] |= CAPABILITIES_COLOR_TEMP
|
||||
|
||||
async_add_entities([Light(**discovery_info)], update_before_add=True)
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up the Zigbee Home Automation light from config entry."""
|
||||
async def async_discover(discovery_info):
|
||||
await _async_setup_entities(hass, config_entry, async_add_entities,
|
||||
[discovery_info])
|
||||
|
||||
unsub = async_dispatcher_connect(
|
||||
hass, ZHA_DISCOVERY_NEW.format(light.DOMAIN), async_discover)
|
||||
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
||||
|
||||
lights = hass.data.get(DATA_ZHA, {}).get(light.DOMAIN)
|
||||
if lights is not None:
|
||||
await _async_setup_entities(hass, config_entry, async_add_entities,
|
||||
lights.values())
|
||||
del hass.data[DATA_ZHA][light.DOMAIN]
|
||||
|
||||
|
||||
async def _async_setup_entities(hass, config_entry, async_add_entities,
|
||||
discovery_infos):
|
||||
"""Set up the ZHA lights."""
|
||||
entities = []
|
||||
for discovery_info in discovery_infos:
|
||||
endpoint = discovery_info['endpoint']
|
||||
if hasattr(endpoint, 'light_color'):
|
||||
caps = await helpers.safe_read(
|
||||
endpoint.light_color, ['color_capabilities'])
|
||||
discovery_info['color_capabilities'] = caps.get(
|
||||
'color_capabilities')
|
||||
if discovery_info['color_capabilities'] is None:
|
||||
# ZCL Version 4 devices don't support the color_capabilities
|
||||
# attribute. In this version XY support is mandatory, but we
|
||||
# need to probe to determine if the device supports color
|
||||
# temperature.
|
||||
discovery_info['color_capabilities'] = \
|
||||
CAPABILITIES_COLOR_XY
|
||||
result = await helpers.safe_read(
|
||||
endpoint.light_color, ['color_temperature'])
|
||||
if (result.get('color_temperature') is not
|
||||
UNSUPPORTED_ATTRIBUTE):
|
||||
discovery_info['color_capabilities'] |= \
|
||||
CAPABILITIES_COLOR_TEMP
|
||||
entities.append(Light(**discovery_info))
|
||||
|
||||
async_add_entities(entities, update_before_add=True)
|
||||
|
||||
|
||||
class Light(ZhaEntity, light.Light):
|
||||
|
||||
Reference in new issue
Block a user