Migrate lacrosse_view to use runtime_data (#147202)

This commit is contained in:
epenet 2025-06-20 12:23:29 +02:00 committed by GitHub
parent cd51070219
commit 544fd2a4a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 18 additions and 39 deletions

View File

@ -6,20 +6,18 @@ import logging
from lacrosse_view import LaCrosse, LoginError from lacrosse_view import LaCrosse, LoginError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN from .coordinator import LaCrosseConfigEntry, LaCrosseUpdateCoordinator
from .coordinator import LaCrosseUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: LaCrosseConfigEntry) -> bool:
"""Set up LaCrosse View from a config entry.""" """Set up LaCrosse View from a config entry."""
api = LaCrosse(async_get_clientsession(hass)) api = LaCrosse(async_get_clientsession(hass))
@ -35,9 +33,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.debug("First refresh") _LOGGER.debug("First refresh")
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = { entry.runtime_data = coordinator
"coordinator": coordinator,
}
_LOGGER.debug("Setting up platforms") _LOGGER.debug("Setting up platforms")
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -45,9 +41,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: LaCrosseConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -17,6 +17,8 @@ from .const import DOMAIN, SCAN_INTERVAL
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type LaCrosseConfigEntry = ConfigEntry[LaCrosseUpdateCoordinator]
class LaCrosseUpdateCoordinator(DataUpdateCoordinator[list[Sensor]]): class LaCrosseUpdateCoordinator(DataUpdateCoordinator[list[Sensor]]):
"""DataUpdateCoordinator for LaCrosse View.""" """DataUpdateCoordinator for LaCrosse View."""
@ -27,12 +29,12 @@ class LaCrosseUpdateCoordinator(DataUpdateCoordinator[list[Sensor]]):
id: str id: str
hass: HomeAssistant hass: HomeAssistant
devices: list[Sensor] | None = None devices: list[Sensor] | None = None
config_entry: ConfigEntry config_entry: LaCrosseConfigEntry
def __init__( def __init__(
self, self,
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: LaCrosseConfigEntry,
api: LaCrosse, api: LaCrosse,
) -> None: ) -> None:
"""Initialize DataUpdateCoordinator for LaCrosse View.""" """Initialize DataUpdateCoordinator for LaCrosse View."""

View File

@ -5,25 +5,20 @@ from __future__ import annotations
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_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN from .coordinator import LaCrosseConfigEntry
from .coordinator import LaCrosseUpdateCoordinator
TO_REDACT = {CONF_PASSWORD, CONF_USERNAME} TO_REDACT = {CONF_PASSWORD, CONF_USERNAME}
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry hass: HomeAssistant, entry: LaCrosseConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
coordinator: LaCrosseUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
"coordinator"
]
return { return {
"entry": async_redact_data(entry.as_dict(), TO_REDACT), "entry": async_redact_data(entry.as_dict(), TO_REDACT),
"coordinator_data": coordinator.data, "coordinator_data": entry.runtime_data.data,
} }

View File

@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DEGREE, DEGREE,
PERCENTAGE, PERCENTAGE,
@ -32,6 +31,7 @@ from homeassistant.helpers.update_coordinator import (
) )
from .const import DOMAIN from .const import DOMAIN
from .coordinator import LaCrosseConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -159,17 +159,14 @@ UNIT_OF_MEASUREMENT_MAP = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: LaCrosseConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up LaCrosse View from a config entry.""" """Set up LaCrosse View from a config entry."""
coordinator: DataUpdateCoordinator[list[Sensor]] = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]["coordinator"]
sensors: list[Sensor] = coordinator.data
sensor_list = [] sensor_list = []
for i, sensor in enumerate(sensors): for i, sensor in enumerate(coordinator.data):
for field in sensor.sensor_field_names: for field in sensor.sensor_field_names:
description = SENSOR_DESCRIPTIONS.get(field) description = SENSOR_DESCRIPTIONS.get(field)
if description is None: if description is None:

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props from syrupy.filters import props
from homeassistant.components.lacrosse_view import DOMAIN from homeassistant.components.lacrosse_view.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import MOCK_ENTRY_DATA, TEST_SENSOR from . import MOCK_ENTRY_DATA, TEST_SENSOR

View File

@ -35,8 +35,6 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1

View File

@ -6,7 +6,7 @@ from unittest.mock import patch
from lacrosse_view import Sensor from lacrosse_view import Sensor
import pytest import pytest
from homeassistant.components.lacrosse_view import DOMAIN from homeassistant.components.lacrosse_view.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -46,7 +46,6 @@ async def test_entities_added(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1
@ -103,7 +102,6 @@ async def test_field_not_supported(
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1
@ -144,7 +142,6 @@ async def test_field_types(
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1
@ -172,7 +169,6 @@ async def test_no_field(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1
@ -200,7 +196,6 @@ async def test_field_data_missing(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1
@ -228,7 +223,6 @@ async def test_no_readings(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[DOMAIN]
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
assert entries assert entries
assert len(entries) == 1 assert len(entries) == 1