diff --git a/homeassistant/components/onewire/__init__.py b/homeassistant/components/onewire/__init__.py index 72119915246..73f3374ba97 100644 --- a/homeassistant/components/onewire/__init__.py +++ b/homeassistant/components/onewire/__init__.py @@ -13,12 +13,11 @@ from .const import DOMAIN, PLATFORMS from .onewirehub import CannotConnect, OneWireHub _LOGGER = logging.getLogger(__name__) +OneWireConfigEntry = ConfigEntry[OneWireHub] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: OneWireConfigEntry) -> bool: """Set up a 1-Wire proxy for a config entry.""" - hass.data.setdefault(DOMAIN, {}) - onewire_hub = OneWireHub(hass) try: await onewire_hub.initialize(entry) @@ -28,7 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) as exc: raise ConfigEntryNotReady from exc - hass.data[DOMAIN][entry.entry_id] = onewire_hub + entry.runtime_data = onewire_hub await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) @@ -38,26 +37,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_remove_config_entry_device( - hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry + hass: HomeAssistant, config_entry: OneWireConfigEntry, device_entry: dr.DeviceEntry ) -> bool: """Remove a config entry from a device.""" - onewire_hub: OneWireHub = hass.data[DOMAIN][config_entry.entry_id] + onewire_hub = config_entry.runtime_data return not device_entry.identifiers.intersection( (DOMAIN, device.id) for device in onewire_hub.devices or [] ) -async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, config_entry: OneWireConfigEntry +) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms( - config_entry, PLATFORMS - ) - if unload_ok: - hass.data[DOMAIN].pop(config_entry.entry_id) - return unload_ok + return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) -async def options_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: +async def options_update_listener( + hass: HomeAssistant, entry: OneWireConfigEntry +) -> None: """Handle options update.""" _LOGGER.debug("Configuration options updated, reloading OneWire integration") await hass.config_entries.async_reload(entry.entry_id) diff --git a/homeassistant/components/onewire/binary_sensor.py b/homeassistant/components/onewire/binary_sensor.py index 3c2ca3529cc..82cdb1936f7 100644 --- a/homeassistant/components/onewire/binary_sensor.py +++ b/homeassistant/components/onewire/binary_sensor.py @@ -10,18 +10,12 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import ( - DEVICE_KEYS_0_3, - DEVICE_KEYS_0_7, - DEVICE_KEYS_A_B, - DOMAIN, - READ_MODE_BOOL, -) +from . import OneWireConfigEntry +from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL from .onewire_entities import OneWireEntity, OneWireEntityDescription from .onewirehub import OneWireHub @@ -95,13 +89,13 @@ def get_sensor_types( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: OneWireConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up 1-Wire platform.""" - onewire_hub = hass.data[DOMAIN][config_entry.entry_id] - - entities = await hass.async_add_executor_job(get_entities, onewire_hub) + entities = await hass.async_add_executor_job( + get_entities, config_entry.runtime_data + ) async_add_entities(entities, True) diff --git a/homeassistant/components/onewire/diagnostics.py b/homeassistant/components/onewire/diagnostics.py index 387553849f3..523bb4e2580 100644 --- a/homeassistant/components/onewire/diagnostics.py +++ b/homeassistant/components/onewire/diagnostics.py @@ -6,21 +6,19 @@ from dataclasses import asdict from typing import Any from homeassistant.components.diagnostics import async_redact_data -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant -from .const import DOMAIN -from .onewirehub import OneWireHub +from . import OneWireConfigEntry TO_REDACT = {CONF_HOST} async def async_get_config_entry_diagnostics( - hass: HomeAssistant, entry: ConfigEntry + hass: HomeAssistant, entry: OneWireConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - onewire_hub: OneWireHub = hass.data[DOMAIN][entry.entry_id] + onewire_hub = entry.runtime_data return { "entry": { diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 3e43df4dddd..b7d7e3ddbe9 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -17,7 +17,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( LIGHT_LUX, PERCENTAGE, @@ -29,10 +28,10 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType +from . import OneWireConfigEntry from .const import ( DEVICE_KEYS_0_3, DEVICE_KEYS_A_B, - DOMAIN, OPTION_ENTRY_DEVICE_OPTIONS, OPTION_ENTRY_SENSOR_PRECISION, PRECISION_MAPPING_FAMILY_28, @@ -350,13 +349,12 @@ def get_sensor_types( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: OneWireConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up 1-Wire platform.""" - onewire_hub = hass.data[DOMAIN][config_entry.entry_id] entities = await hass.async_add_executor_job( - get_entities, onewire_hub, config_entry.options + get_entities, config_entry.runtime_data, config_entry.options ) async_add_entities(entities, True) diff --git a/homeassistant/components/onewire/switch.py b/homeassistant/components/onewire/switch.py index 94a7d41ab85..11bcbff5970 100644 --- a/homeassistant/components/onewire/switch.py +++ b/homeassistant/components/onewire/switch.py @@ -7,18 +7,12 @@ import os from typing import Any from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import ( - DEVICE_KEYS_0_3, - DEVICE_KEYS_0_7, - DEVICE_KEYS_A_B, - DOMAIN, - READ_MODE_BOOL, -) +from . import OneWireConfigEntry +from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL from .onewire_entities import OneWireEntity, OneWireEntityDescription from .onewirehub import OneWireHub @@ -155,13 +149,13 @@ def get_sensor_types( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: OneWireConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up 1-Wire platform.""" - onewire_hub = hass.data[DOMAIN][config_entry.entry_id] - - entities = await hass.async_add_executor_job(get_entities, onewire_hub) + entities = await hass.async_add_executor_job( + get_entities, config_entry.runtime_data + ) async_add_entities(entities, True) diff --git a/tests/components/onewire/test_init.py b/tests/components/onewire/test_init.py index a1a24cd8f83..b8ab2fa9ccf 100644 --- a/tests/components/onewire/test_init.py +++ b/tests/components/onewire/test_init.py @@ -26,7 +26,6 @@ async def test_connect_failure(hass: HomeAssistant, config_entry: ConfigEntry) - assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert config_entry.state is ConfigEntryState.SETUP_RETRY - assert not hass.data.get(DOMAIN) async def test_listing_failure( @@ -40,7 +39,6 @@ async def test_listing_failure( assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert config_entry.state is ConfigEntryState.SETUP_RETRY - assert not hass.data.get(DOMAIN) @pytest.mark.usefixtures("owproxy") @@ -56,7 +54,6 @@ async def test_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> N await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.NOT_LOADED - assert not hass.data.get(DOMAIN) async def test_update_options(