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
@@ -6,9 +6,13 @@ at https://home-assistant.io/components/switch.zha/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.components.zha.const import (
|
||||
ZHA_DISCOVERY_NEW, DATA_ZHA, DATA_ZHA_DISPATCHERS
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -17,24 +21,44 @@ DEPENDENCIES = ['zha']
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the Zigbee Home Automation switches."""
|
||||
"""Old way of setting up Zigbee Home Automation switches."""
|
||||
pass
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up the Zigbee Home Automation switch 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(DOMAIN), async_discover)
|
||||
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
||||
|
||||
switches = hass.data.get(DATA_ZHA, {}).get(DOMAIN)
|
||||
if switches is not None:
|
||||
await _async_setup_entities(hass, config_entry, async_add_entities,
|
||||
switches.values())
|
||||
del hass.data[DATA_ZHA][DOMAIN]
|
||||
|
||||
|
||||
async def _async_setup_entities(hass, config_entry, async_add_entities,
|
||||
discovery_infos):
|
||||
"""Set up the ZHA switches."""
|
||||
from zigpy.zcl.clusters.general import OnOff
|
||||
entities = []
|
||||
for discovery_info in discovery_infos:
|
||||
switch = Switch(**discovery_info)
|
||||
if discovery_info['new_join']:
|
||||
in_clusters = discovery_info['in_clusters']
|
||||
cluster = in_clusters[OnOff.cluster_id]
|
||||
await helpers.configure_reporting(
|
||||
switch.entity_id, cluster, switch.value_attribute,
|
||||
min_report=0, max_report=600, reportable_change=1
|
||||
)
|
||||
entities.append(switch)
|
||||
|
||||
discovery_info = helpers.get_discovery_info(hass, discovery_info)
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
switch = Switch(**discovery_info)
|
||||
|
||||
if discovery_info['new_join']:
|
||||
in_clusters = discovery_info['in_clusters']
|
||||
cluster = in_clusters[OnOff.cluster_id]
|
||||
await helpers.configure_reporting(
|
||||
switch.entity_id, cluster, switch.value_attribute,
|
||||
min_report=0, max_report=600, reportable_change=1
|
||||
)
|
||||
|
||||
async_add_entities([switch], update_before_add=True)
|
||||
async_add_entities(entities, update_before_add=True)
|
||||
|
||||
|
||||
class Switch(ZhaEntity, SwitchDevice):
|
||||
|
||||
Reference in new issue
Block a user