mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Migrate laundrify to use runtime_data (#147331)
* Migrate laundrify to use runtime_data * Adjust test
This commit is contained in:
parent
82c1751f85
commit
4d2f0f2de6
@ -7,21 +7,19 @@ import logging
|
|||||||
from laundrify_aio import LaundrifyAPI
|
from laundrify_aio import LaundrifyAPI
|
||||||
from laundrify_aio.exceptions import ApiConnectionException, UnauthorizedException
|
from laundrify_aio.exceptions import ApiConnectionException, UnauthorizedException
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
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 LaundrifyConfigEntry, LaundrifyUpdateCoordinator
|
||||||
from .coordinator import LaundrifyUpdateCoordinator
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: LaundrifyConfigEntry) -> bool:
|
||||||
"""Set up laundrify from a config entry."""
|
"""Set up laundrify from a config entry."""
|
||||||
|
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
@ -38,26 +36,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
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
|
||||||
"api": api_client,
|
|
||||||
"coordinator": coordinator,
|
|
||||||
}
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: LaundrifyConfigEntry) -> 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
|
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_migrate_entry(hass: HomeAssistant, entry: LaundrifyConfigEntry) -> bool:
|
||||||
"""Migrate entry."""
|
"""Migrate entry."""
|
||||||
|
|
||||||
_LOGGER.debug("Migrating from version %s", entry.version)
|
_LOGGER.debug("Migrating from version %s", entry.version)
|
||||||
|
@ -10,28 +10,25 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN, MANUFACTURER, MODELS
|
from .const import DOMAIN, MANUFACTURER, MODELS
|
||||||
from .coordinator import LaundrifyUpdateCoordinator
|
from .coordinator import LaundrifyConfigEntry, LaundrifyUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigEntry,
|
entry: LaundrifyConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up sensors from a config entry created in the integrations UI."""
|
"""Set up sensors from a config entry created in the integrations UI."""
|
||||||
|
|
||||||
coordinator: LaundrifyUpdateCoordinator = hass.data[DOMAIN][config.entry_id][
|
coordinator = entry.runtime_data
|
||||||
"coordinator"
|
|
||||||
]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
LaundrifyPowerPlug(coordinator, device) for device in coordinator.data.values()
|
LaundrifyPowerPlug(coordinator, device) for device in coordinator.data.values()
|
||||||
|
@ -16,6 +16,8 @@ from .const import DEFAULT_POLL_INTERVAL, DOMAIN, REQUEST_TIMEOUT
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type LaundrifyConfigEntry = ConfigEntry[LaundrifyUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class LaundrifyUpdateCoordinator(DataUpdateCoordinator[dict[str, LaundrifyDevice]]):
|
class LaundrifyUpdateCoordinator(DataUpdateCoordinator[dict[str, LaundrifyDevice]]):
|
||||||
"""Class to manage fetching laundrify API data."""
|
"""Class to manage fetching laundrify API data."""
|
||||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import UnitOfEnergy, UnitOfPower
|
from homeassistant.const import UnitOfEnergy, UnitOfPower
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
@ -18,21 +17,19 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import LaundrifyUpdateCoordinator
|
from .coordinator import LaundrifyConfigEntry, LaundrifyUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigEntry,
|
entry: LaundrifyConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add power sensor for passed config_entry in HA."""
|
"""Add power sensor for passed config_entry in HA."""
|
||||||
|
|
||||||
coordinator: LaundrifyUpdateCoordinator = hass.data[DOMAIN][config.entry_id][
|
coordinator = entry.runtime_data
|
||||||
"coordinator"
|
|
||||||
]
|
|
||||||
|
|
||||||
sensor_entities: list[LaundrifyPowerSensor | LaundrifyEnergySensor] = []
|
sensor_entities: list[LaundrifyPowerSensor | LaundrifyEnergySensor] = []
|
||||||
for device in coordinator.data.values():
|
for device in coordinator.data.values():
|
||||||
|
@ -6,8 +6,7 @@ from unittest.mock import AsyncMock, patch
|
|||||||
from laundrify_aio import LaundrifyAPI, LaundrifyDevice
|
from laundrify_aio import LaundrifyAPI, LaundrifyDevice
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.laundrify import DOMAIN
|
from homeassistant.components.laundrify.const import DOMAIN, MANUFACTURER
|
||||||
from homeassistant.components.laundrify.const import MANUFACTURER
|
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user