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
_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)

View file

@ -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)

View file

@ -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": {

View file

@ -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)

View file

@ -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)

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 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(