From 04b5c629e89ffee376fbb1606df783a504e493db Mon Sep 17 00:00:00 2001 From: David Mulcahey Date: Mon, 24 Dec 2018 08:01:29 -0500 Subject: [PATCH 1/3] remove global from application controller per request --- homeassistant/components/zha/__init__.py | 26 ++++++++++++------------ homeassistant/components/zha/const.py | 1 + 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index e685deed1c6fc..98f8aedab94e8 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -28,7 +28,8 @@ DATA_ZHA_CONFIG, DATA_ZHA_CORE_COMPONENT, DATA_ZHA_DISPATCHERS, DATA_ZHA_RADIO, DEFAULT_BAUDRATE, DEFAULT_DATABASE_NAME, DEFAULT_RADIO_TYPE, DOMAIN, ZHA_DISCOVERY_NEW, RadioType, - EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS) + EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS, + APPLICATION_CONTROLLER) REQUIREMENTS = [ 'bellows==0.7.0', @@ -76,7 +77,6 @@ CENTICELSIUS = 'C-100' # Internal definitions -APPLICATION_CONTROLLER = None _LOGGER = logging.getLogger(__name__) @@ -89,6 +89,7 @@ async def async_setup(hass, config): conf = config[DOMAIN] hass.data[DATA_ZHA][DATA_ZHA_CONFIG] = conf + hass.data[DATA_ZHA][APPLICATION_CONTROLLER] = None if not hass.config_entries.async_entries(DOMAIN): hass.async_create_task(hass.config_entries.flow.async_init( @@ -114,8 +115,6 @@ async def async_setup_entry(hass, config_entry): # pylint: disable=W0611, W0612 import zhaquirks # noqa - global APPLICATION_CONTROLLER - hass.data[DATA_ZHA] = hass.data.get(DATA_ZHA, {}) hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS] = [] @@ -155,12 +154,13 @@ def zha_send_event(self, cluster, command, args): ClusterPersistingListener ) - APPLICATION_CONTROLLER = ControllerApplication(radio, database) + application_controller = ControllerApplication(radio, database) + hass.data[DATA_ZHA][APPLICATION_CONTROLLER] = application_controller listener = ApplicationListener(hass, config) - APPLICATION_CONTROLLER.add_listener(listener) - await APPLICATION_CONTROLLER.startup(auto_form=True) + application_controller.add_listener(listener) + await application_controller.startup(auto_form=True) - for device in APPLICATION_CONTROLLER.devices.values(): + for device in application_controller.devices.values(): hass.async_create_task( listener.async_device_initialized(device, False)) @@ -168,14 +168,14 @@ def zha_send_event(self, cluster, command, args): hass.helpers.device_registry.async_get_registry() device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, - connections={(CONNECTION_ZIGBEE, str(APPLICATION_CONTROLLER.ieee))}, - identifiers={(DOMAIN, str(APPLICATION_CONTROLLER.ieee))}, + connections={(CONNECTION_ZIGBEE, str(application_controller.ieee))}, + identifiers={(DOMAIN, str(application_controller.ieee))}, name="Zigbee Coordinator", manufacturer="ZHA", model=radio_description, ) - hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = str(APPLICATION_CONTROLLER.ieee) + hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = str(application_controller.ieee) for component in COMPONENTS: hass.async_create_task( @@ -187,7 +187,7 @@ async def permit(service): """Allow devices to join this network.""" duration = service.data.get(ATTR_DURATION) _LOGGER.info("Permitting joins for %ss", duration) - await APPLICATION_CONTROLLER.permit(duration) + await application_controller.permit(duration) hass.services.async_register(DOMAIN, SERVICE_PERMIT, permit, schema=SERVICE_SCHEMAS[SERVICE_PERMIT]) @@ -198,7 +198,7 @@ async def remove(service): ieee = service.data.get(ATTR_IEEE) ieee = EmberEUI64([uint8_t(p, base=16) for p in ieee.split(':')]) _LOGGER.info("Removing node %s", ieee) - await APPLICATION_CONTROLLER.remove(ieee) + await application_controller.remove(ieee) hass.services.async_register(DOMAIN, SERVICE_REMOVE, remove, schema=SERVICE_SCHEMAS[SERVICE_REMOVE]) diff --git a/homeassistant/components/zha/const.py b/homeassistant/components/zha/const.py index 3e7f9f89f912a..45401aae18bbd 100644 --- a/homeassistant/components/zha/const.py +++ b/homeassistant/components/zha/const.py @@ -15,6 +15,7 @@ DATA_ZHA_CORE_COMPONENT = 'zha_core_component' DATA_ZHA_CORE_EVENTS = 'zha_core_events' ZHA_DISCOVERY_NEW = 'zha_discovery_new_{}' +APPLICATION_CONTROLLER = 'application_controller' COMPONENTS = [ 'binary_sensor', From 011ab57f57d11063ac92ed1457e996ec05c4bcc5 Mon Sep 17 00:00:00 2001 From: David Mulcahey Date: Mon, 24 Dec 2018 16:32:06 -0500 Subject: [PATCH 2/3] remove unneeded line --- homeassistant/components/zha/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 98f8aedab94e8..f65060266aa7b 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -89,7 +89,6 @@ async def async_setup(hass, config): conf = config[DOMAIN] hass.data[DATA_ZHA][DATA_ZHA_CONFIG] = conf - hass.data[DATA_ZHA][APPLICATION_CONTROLLER] = None if not hass.config_entries.async_entries(DOMAIN): hass.async_create_task(hass.config_entries.flow.async_init( From 4108588e922cd8d173f8bafae8b9a08c0b67104b Mon Sep 17 00:00:00 2001 From: David Mulcahey Date: Mon, 24 Dec 2018 17:01:52 -0500 Subject: [PATCH 3/3] don't store controller application in hass.data - review comment --- homeassistant/components/zha/__init__.py | 4 +--- homeassistant/components/zha/const.py | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index f65060266aa7b..5ad06565b0051 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -28,8 +28,7 @@ DATA_ZHA_CONFIG, DATA_ZHA_CORE_COMPONENT, DATA_ZHA_DISPATCHERS, DATA_ZHA_RADIO, DEFAULT_BAUDRATE, DEFAULT_DATABASE_NAME, DEFAULT_RADIO_TYPE, DOMAIN, ZHA_DISCOVERY_NEW, RadioType, - EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS, - APPLICATION_CONTROLLER) + EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS) REQUIREMENTS = [ 'bellows==0.7.0', @@ -154,7 +153,6 @@ def zha_send_event(self, cluster, command, args): ) application_controller = ControllerApplication(radio, database) - hass.data[DATA_ZHA][APPLICATION_CONTROLLER] = application_controller listener = ApplicationListener(hass, config) application_controller.add_listener(listener) await application_controller.startup(auto_form=True) diff --git a/homeassistant/components/zha/const.py b/homeassistant/components/zha/const.py index 45401aae18bbd..3e7f9f89f912a 100644 --- a/homeassistant/components/zha/const.py +++ b/homeassistant/components/zha/const.py @@ -15,7 +15,6 @@ DATA_ZHA_CORE_COMPONENT = 'zha_core_component' DATA_ZHA_CORE_EVENTS = 'zha_core_events' ZHA_DISCOVERY_NEW = 'zha_discovery_new_{}' -APPLICATION_CONTROLLER = 'application_controller' COMPONENTS = [ 'binary_sensor',