Store runtime data in entry in onewire (#116450)

* Store runtime data in entry in onewire

* Adjust
This commit is contained in:
epenet 2024-04-30 15:55:20 +02:00 committed by GitHub
parent a12301f696
commit 8291769361
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 30 additions and 51 deletions

View file

@ -13,12 +13,11 @@ from .const import DOMAIN, PLATFORMS
from .onewirehub import CannotConnect, OneWireHub from .onewirehub import CannotConnect, OneWireHub
_LOGGER = logging.getLogger(__name__) _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.""" """Set up a 1-Wire proxy for a config entry."""
hass.data.setdefault(DOMAIN, {})
onewire_hub = OneWireHub(hass) onewire_hub = OneWireHub(hass)
try: try:
await onewire_hub.initialize(entry) await onewire_hub.initialize(entry)
@ -28,7 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) as exc: ) as exc:
raise ConfigEntryNotReady from 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) 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( 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: ) -> bool:
"""Remove a config entry from a device.""" """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( return not device_entry.identifiers.intersection(
(DOMAIN, device.id) for device in onewire_hub.devices or [] (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 a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms( return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
config_entry, PLATFORMS
)
if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
async def options_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: async def options_update_listener(
hass: HomeAssistant, entry: OneWireConfigEntry
) -> None:
"""Handle options update.""" """Handle options update."""
_LOGGER.debug("Configuration options updated, reloading OneWire integration") _LOGGER.debug("Configuration options updated, reloading OneWire integration")
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)

View file

@ -10,18 +10,12 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ( from . import OneWireConfigEntry
DEVICE_KEYS_0_3, from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
DEVICE_KEYS_0_7,
DEVICE_KEYS_A_B,
DOMAIN,
READ_MODE_BOOL,
)
from .onewire_entities import OneWireEntity, OneWireEntityDescription from .onewire_entities import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireHub from .onewirehub import OneWireHub
@ -95,13 +89,13 @@ def get_sensor_types(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: OneWireConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up 1-Wire platform.""" """Set up 1-Wire platform."""
onewire_hub = hass.data[DOMAIN][config_entry.entry_id] entities = await hass.async_add_executor_job(
get_entities, config_entry.runtime_data
entities = await hass.async_add_executor_job(get_entities, onewire_hub) )
async_add_entities(entities, True) async_add_entities(entities, True)

View file

@ -6,21 +6,19 @@ from dataclasses import asdict
from typing import Any from typing import Any
from homeassistant.components.diagnostics import async_redact_data from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN from . import OneWireConfigEntry
from .onewirehub import OneWireHub
TO_REDACT = {CONF_HOST} TO_REDACT = {CONF_HOST}
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry hass: HomeAssistant, entry: OneWireConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
onewire_hub: OneWireHub = hass.data[DOMAIN][entry.entry_id] onewire_hub = entry.runtime_data
return { return {
"entry": { "entry": {

View file

@ -17,7 +17,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE, PERCENTAGE,
@ -29,10 +28,10 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from . import OneWireConfigEntry
from .const import ( from .const import (
DEVICE_KEYS_0_3, DEVICE_KEYS_0_3,
DEVICE_KEYS_A_B, DEVICE_KEYS_A_B,
DOMAIN,
OPTION_ENTRY_DEVICE_OPTIONS, OPTION_ENTRY_DEVICE_OPTIONS,
OPTION_ENTRY_SENSOR_PRECISION, OPTION_ENTRY_SENSOR_PRECISION,
PRECISION_MAPPING_FAMILY_28, PRECISION_MAPPING_FAMILY_28,
@ -350,13 +349,12 @@ def get_sensor_types(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: OneWireConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up 1-Wire platform.""" """Set up 1-Wire platform."""
onewire_hub = hass.data[DOMAIN][config_entry.entry_id]
entities = await hass.async_add_executor_job( 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) async_add_entities(entities, True)

View file

@ -7,18 +7,12 @@ import os
from typing import Any from typing import Any
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ( from . import OneWireConfigEntry
DEVICE_KEYS_0_3, from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
DEVICE_KEYS_0_7,
DEVICE_KEYS_A_B,
DOMAIN,
READ_MODE_BOOL,
)
from .onewire_entities import OneWireEntity, OneWireEntityDescription from .onewire_entities import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireHub from .onewirehub import OneWireHub
@ -155,13 +149,13 @@ def get_sensor_types(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: OneWireConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up 1-Wire platform.""" """Set up 1-Wire platform."""
onewire_hub = hass.data[DOMAIN][config_entry.entry_id] entities = await hass.async_add_executor_job(
get_entities, config_entry.runtime_data
entities = await hass.async_add_executor_job(get_entities, onewire_hub) )
async_add_entities(entities, True) async_add_entities(entities, True)

View file

@ -26,7 +26,6 @@ async def test_connect_failure(hass: HomeAssistant, config_entry: ConfigEntry) -
assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)
async def test_listing_failure( async def test_listing_failure(
@ -40,7 +39,6 @@ async def test_listing_failure(
assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)
@pytest.mark.usefixtures("owproxy") @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() await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.NOT_LOADED assert config_entry.state is ConfigEntryState.NOT_LOADED
assert not hass.data.get(DOMAIN)
async def test_update_options( async def test_update_options(