diff --git a/homeassistant/components/zwave_js/__init__.py b/homeassistant/components/zwave_js/__init__.py index 13238cc0a6c..e0b0e3cd370 100644 --- a/homeassistant/components/zwave_js/__init__.py +++ b/homeassistant/components/zwave_js/__init__.py @@ -182,13 +182,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Set up websocket API async_register_api(hass) + entry.runtime_data = {} # Create a task to allow the config entry to be unloaded before the driver is ready. # Unloading the config entry is needed if the client listen task errors. start_client_task = hass.async_create_task(start_client(hass, entry, client)) - hass.data[DOMAIN].setdefault(entry.entry_id, {})[DATA_START_CLIENT_TASK] = ( - start_client_task - ) + entry.runtime_data[DATA_START_CLIENT_TASK] = start_client_task return True @@ -197,9 +196,8 @@ async def start_client( hass: HomeAssistant, entry: ConfigEntry, client: ZwaveClient ) -> None: """Start listening with the client.""" - entry_hass_data: dict = hass.data[DOMAIN].setdefault(entry.entry_id, {}) - entry_hass_data[DATA_CLIENT] = client - driver_events = entry_hass_data[DATA_DRIVER_EVENTS] = DriverEvents(hass, entry) + entry.runtime_data[DATA_CLIENT] = client + driver_events = entry.runtime_data[DATA_DRIVER_EVENTS] = DriverEvents(hass, entry) async def handle_ha_shutdown(event: Event) -> None: """Handle HA shutdown.""" @@ -208,7 +206,7 @@ async def start_client( listen_task = asyncio.create_task( client_listen(hass, entry, client, driver_events.ready) ) - entry_hass_data[DATA_CLIENT_LISTEN_TASK] = listen_task + entry.runtime_data[DATA_CLIENT_LISTEN_TASK] = listen_task entry.async_on_unload( hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, handle_ha_shutdown) ) @@ -935,11 +933,10 @@ async def client_listen( async def disconnect_client(hass: HomeAssistant, entry: ConfigEntry) -> None: """Disconnect client.""" - data = hass.data[DOMAIN][entry.entry_id] - client: ZwaveClient = data[DATA_CLIENT] - listen_task: asyncio.Task = data[DATA_CLIENT_LISTEN_TASK] - start_client_task: asyncio.Task = data[DATA_START_CLIENT_TASK] - driver_events: DriverEvents = data[DATA_DRIVER_EVENTS] + client: ZwaveClient = entry.runtime_data[DATA_CLIENT] + listen_task: asyncio.Task = entry.runtime_data[DATA_CLIENT_LISTEN_TASK] + start_client_task: asyncio.Task = entry.runtime_data[DATA_START_CLIENT_TASK] + driver_events: DriverEvents = entry.runtime_data[DATA_DRIVER_EVENTS] listen_task.cancel() start_client_task.cancel() platform_setup_tasks = driver_events.platform_setup_tasks.values() @@ -959,9 +956,8 @@ async def disconnect_client(hass: HomeAssistant, entry: ConfigEntry) -> None: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - info = hass.data[DOMAIN][entry.entry_id] - client: ZwaveClient = info[DATA_CLIENT] - driver_events: DriverEvents = info[DATA_DRIVER_EVENTS] + client: ZwaveClient = entry.runtime_data[DATA_CLIENT] + driver_events: DriverEvents = entry.runtime_data[DATA_DRIVER_EVENTS] tasks: list[Coroutine] = [ hass.config_entries.async_forward_entry_unload(entry, platform) @@ -973,11 +969,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if client.connected and client.driver: await async_disable_server_logging_if_needed(hass, entry, client.driver) - if DATA_CLIENT_LISTEN_TASK in info: + if DATA_CLIENT_LISTEN_TASK in entry.runtime_data: await disconnect_client(hass, entry) - hass.data[DOMAIN].pop(entry.entry_id) - if entry.data.get(CONF_USE_ADDON) and entry.disabled_by: addon_manager: AddonManager = get_addon_manager(hass) LOGGER.debug("Stopping Z-Wave JS add-on") @@ -1016,8 +1010,7 @@ async def async_remove_config_entry_device( hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry ) -> bool: """Remove a config entry from a device.""" - entry_hass_data = hass.data[DOMAIN][config_entry.entry_id] - client: ZwaveClient = entry_hass_data[DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] # Driver may not be ready yet so we can't allow users to remove a device since # we need to check if the device is still known to the controller @@ -1037,7 +1030,7 @@ async def async_remove_config_entry_device( ): return False - controller_events: ControllerEvents = entry_hass_data[ + controller_events: ControllerEvents = config_entry.runtime_data[ DATA_DRIVER_EVENTS ].controller_events controller_events.registered_unique_ids.pop(device_entry.id, None) diff --git a/homeassistant/components/zwave_js/api.py b/homeassistant/components/zwave_js/api.py index 8856cf2b41c..ca03cd643c9 100644 --- a/homeassistant/components/zwave_js/api.py +++ b/homeassistant/components/zwave_js/api.py @@ -75,7 +75,6 @@ from .config_validation import BITMASK_SCHEMA from .const import ( CONF_DATA_COLLECTION_OPTED_IN, DATA_CLIENT, - DOMAIN, EVENT_DEVICE_ADDED_TO_REGISTRY, USER_AGENT, ) @@ -285,7 +284,7 @@ async def _async_get_entry( ) return None, None, None - client: Client = hass.data[DOMAIN][entry_id][DATA_CLIENT] + client: Client = entry.runtime_data[DATA_CLIENT] if client.driver is None: connection.send_error( diff --git a/homeassistant/components/zwave_js/binary_sensor.py b/homeassistant/components/zwave_js/binary_sensor.py index 79181e818a2..bd5ce2d810b 100644 --- a/homeassistant/components/zwave_js/binary_sensor.py +++ b/homeassistant/components/zwave_js/binary_sensor.py @@ -254,7 +254,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave binary sensor from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_binary_sensor(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/button.py b/homeassistant/components/zwave_js/button.py index 5526faf9c59..7fd42700a05 100644 --- a/homeassistant/components/zwave_js/button.py +++ b/homeassistant/components/zwave_js/button.py @@ -27,7 +27,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave button from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_button(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/climate.py b/homeassistant/components/zwave_js/climate.py index 04e3d8c3950..14a3fe579c4 100644 --- a/homeassistant/components/zwave_js/climate.py +++ b/homeassistant/components/zwave_js/climate.py @@ -102,7 +102,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave climate from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_climate(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/cover.py b/homeassistant/components/zwave_js/cover.py index f0ef1913bbb..363b32cedda 100644 --- a/homeassistant/components/zwave_js/cover.py +++ b/homeassistant/components/zwave_js/cover.py @@ -57,7 +57,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Cover from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_cover(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/device_automation_helpers.py b/homeassistant/components/zwave_js/device_automation_helpers.py index 5c94b2bb02d..4eed2a5b50c 100644 --- a/homeassistant/components/zwave_js/device_automation_helpers.py +++ b/homeassistant/components/zwave_js/device_automation_helpers.py @@ -55,5 +55,5 @@ def async_bypass_dynamic_config_validation(hass: HomeAssistant, device_id: str) return True # The driver may not be ready when the config entry is loaded. - client: ZwaveClient = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] + client: ZwaveClient = entry.runtime_data[DATA_CLIENT] return client.driver is None diff --git a/homeassistant/components/zwave_js/diagnostics.py b/homeassistant/components/zwave_js/diagnostics.py index 3d61699472d..dde455bd9b6 100644 --- a/homeassistant/components/zwave_js/diagnostics.py +++ b/homeassistant/components/zwave_js/diagnostics.py @@ -20,7 +20,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession -from .const import DATA_CLIENT, DOMAIN, USER_AGENT +from .const import DATA_CLIENT, USER_AGENT from .helpers import ( ZwaveValueMatcher, get_home_and_node_id_from_device_entry, @@ -148,7 +148,7 @@ async def async_get_device_diagnostics( hass: HomeAssistant, config_entry: ConfigEntry, device: dr.DeviceEntry ) -> dict[str, Any]: """Return diagnostics for a device.""" - client: Client = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: Client = config_entry.runtime_data[DATA_CLIENT] identifiers = get_home_and_node_id_from_device_entry(device) node_id = identifiers[1] if identifiers else None driver = client.driver diff --git a/homeassistant/components/zwave_js/event.py b/homeassistant/components/zwave_js/event.py index 2b170bdf5bd..8dae66c26ac 100644 --- a/homeassistant/components/zwave_js/event.py +++ b/homeassistant/components/zwave_js/event.py @@ -25,7 +25,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Event entity from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_event(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/fan.py b/homeassistant/components/zwave_js/fan.py index 4cf9a5d40cf..925a48512d8 100644 --- a/homeassistant/components/zwave_js/fan.py +++ b/homeassistant/components/zwave_js/fan.py @@ -49,7 +49,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Fan from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_fan(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/helpers.py b/homeassistant/components/zwave_js/helpers.py index 4a4c1030812..598cf2f78f6 100644 --- a/homeassistant/components/zwave_js/helpers.py +++ b/homeassistant/components/zwave_js/helpers.py @@ -155,7 +155,7 @@ async def async_enable_server_logging_if_needed( if (curr_server_log_level := driver.log_config.level) and ( LOG_LEVEL_MAP[curr_server_log_level] ) > (lib_log_level := LIB_LOGGER.getEffectiveLevel()): - entry_data = hass.data[DOMAIN][entry.entry_id] + entry_data = entry.runtime_data LOGGER.warning( ( "Server logging is set to %s and is currently less verbose " @@ -174,7 +174,6 @@ async def async_disable_server_logging_if_needed( hass: HomeAssistant, entry: ConfigEntry, driver: Driver ) -> None: """Disable logging of zwave-js-server in the lib if still connected to server.""" - entry_data = hass.data[DOMAIN][entry.entry_id] if ( not driver or not driver.client.connected @@ -183,8 +182,8 @@ async def async_disable_server_logging_if_needed( return LOGGER.info("Disabling zwave_js server logging") if ( - DATA_OLD_SERVER_LOG_LEVEL in entry_data - and (old_server_log_level := entry_data.pop(DATA_OLD_SERVER_LOG_LEVEL)) + DATA_OLD_SERVER_LOG_LEVEL in entry.runtime_data + and (old_server_log_level := entry.runtime_data.pop(DATA_OLD_SERVER_LOG_LEVEL)) != driver.log_config.level ): LOGGER.info( @@ -275,12 +274,12 @@ def async_get_node_from_device_id( ) if entry and entry.state != ConfigEntryState.LOADED: raise ValueError(f"Device {device_id} config entry is not loaded") - if entry is None or entry.entry_id not in hass.data[DOMAIN]: + if entry is None: raise ValueError( f"Device {device_id} is not from an existing zwave_js config entry" ) - client: ZwaveClient = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] + client: ZwaveClient = entry.runtime_data[DATA_CLIENT] driver = client.driver if driver is None: @@ -443,7 +442,9 @@ def async_get_node_status_sensor_entity_id( if not (entry_id := _zwave_js_config_entry(hass, device)): return None - client = hass.data[DOMAIN][entry_id][DATA_CLIENT] + entry = hass.config_entries.async_get_entry(entry_id) + assert entry + client = entry.runtime_data[DATA_CLIENT] node = async_get_node_from_device_id(hass, device_id, dev_reg) return ent_reg.async_get_entity_id( SENSOR_DOMAIN, diff --git a/homeassistant/components/zwave_js/humidifier.py b/homeassistant/components/zwave_js/humidifier.py index 4030115ab1f..e883858036b 100644 --- a/homeassistant/components/zwave_js/humidifier.py +++ b/homeassistant/components/zwave_js/humidifier.py @@ -73,7 +73,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave humidifier from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_humidifier(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/light.py b/homeassistant/components/zwave_js/light.py index eba2d4a0cce..020f1b66b3d 100644 --- a/homeassistant/components/zwave_js/light.py +++ b/homeassistant/components/zwave_js/light.py @@ -68,7 +68,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Light from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_light(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/lock.py b/homeassistant/components/zwave_js/lock.py index 4b66cb0ed16..5eb89e17402 100644 --- a/homeassistant/components/zwave_js/lock.py +++ b/homeassistant/components/zwave_js/lock.py @@ -66,7 +66,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave lock from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_lock(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/number.py b/homeassistant/components/zwave_js/number.py index 15262710095..54162488d89 100644 --- a/homeassistant/components/zwave_js/number.py +++ b/homeassistant/components/zwave_js/number.py @@ -31,7 +31,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Number entity from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_number(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/select.py b/homeassistant/components/zwave_js/select.py index c970c17f5f0..49ad1868005 100644 --- a/homeassistant/components/zwave_js/select.py +++ b/homeassistant/components/zwave_js/select.py @@ -30,7 +30,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Select entity from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_select(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index f799a70110d..c07420615a1 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -530,7 +530,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave sensor from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] driver = client.driver assert driver is not None # Driver is ready before platforms are loaded. diff --git a/homeassistant/components/zwave_js/services.py b/homeassistant/components/zwave_js/services.py index bdd5090bcf8..a25095156ed 100644 --- a/homeassistant/components/zwave_js/services.py +++ b/homeassistant/components/zwave_js/services.py @@ -727,8 +727,8 @@ class ZWaveServices: first_node = next(node for node in nodes) client = first_node.client except StopIteration: - entry_id = self._hass.config_entries.async_entries(const.DOMAIN)[0].entry_id - client = self._hass.data[const.DOMAIN][entry_id][const.DATA_CLIENT] + data = self._hass.config_entries.async_entries(const.DOMAIN)[0].runtime_data + client = data[const.DATA_CLIENT] assert client.driver first_node = next( node diff --git a/homeassistant/components/zwave_js/siren.py b/homeassistant/components/zwave_js/siren.py index 413186da9bf..3a09049def3 100644 --- a/homeassistant/components/zwave_js/siren.py +++ b/homeassistant/components/zwave_js/siren.py @@ -33,7 +33,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave Siren entity from Config Entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_siren(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/switch.py b/homeassistant/components/zwave_js/switch.py index 30ee5fb72bc..ef769209b31 100644 --- a/homeassistant/components/zwave_js/switch.py +++ b/homeassistant/components/zwave_js/switch.py @@ -31,7 +31,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave sensor from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] @callback def async_add_switch(info: ZwaveDiscoveryInfo) -> None: diff --git a/homeassistant/components/zwave_js/triggers/event.py b/homeassistant/components/zwave_js/triggers/event.py index 6cf4a31c0eb..921cae19b3a 100644 --- a/homeassistant/components/zwave_js/triggers/event.py +++ b/homeassistant/components/zwave_js/triggers/event.py @@ -219,7 +219,9 @@ async def async_attach_trigger( drivers: set[Driver] = set() if not (nodes := async_get_nodes_from_targets(hass, config, dev_reg=dev_reg)): entry_id = config[ATTR_CONFIG_ENTRY_ID] - client: Client = hass.data[DOMAIN][entry_id][DATA_CLIENT] + entry = hass.config_entries.async_get_entry(entry_id) + assert entry + client: Client = entry.runtime_data[DATA_CLIENT] driver = client.driver assert driver drivers.add(driver) diff --git a/homeassistant/components/zwave_js/triggers/trigger_helpers.py b/homeassistant/components/zwave_js/triggers/trigger_helpers.py index 1dbe1f48f0a..1ef9ebaae28 100644 --- a/homeassistant/components/zwave_js/triggers/trigger_helpers.py +++ b/homeassistant/components/zwave_js/triggers/trigger_helpers.py @@ -37,7 +37,7 @@ def async_bypass_dynamic_config_validation( return True # The driver may not be ready when the config entry is loaded. - client: ZwaveClient = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] + client: ZwaveClient = entry.runtime_data[DATA_CLIENT] if client.driver is None: return True diff --git a/homeassistant/components/zwave_js/update.py b/homeassistant/components/zwave_js/update.py index 3fdbab8aacf..02c59d220e1 100644 --- a/homeassistant/components/zwave_js/update.py +++ b/homeassistant/components/zwave_js/update.py @@ -80,7 +80,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Z-Wave update entity from config entry.""" - client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] + client: ZwaveClient = config_entry.runtime_data[DATA_CLIENT] cnt: Counter = Counter() @callback