Migrate smappee to use runtime_data (#125529)

This commit is contained in:
epenet 2024-09-08 21:18:08 +02:00 committed by GitHub
parent 26ac8e35cb
commit 8b8083a639
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 18 deletions

View File

@ -25,6 +25,8 @@ from .const import (
TOKEN_URL,
)
type SmappeeConfigEntry = ConfigEntry[SmappeeBase]
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
@ -72,7 +74,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SmappeeConfigEntry) -> bool:
"""Set up Smappee from a zeroconf or config entry."""
if CONF_IP_ADDRESS in entry.data:
if helper.is_smappee_genius(entry.data[CONF_SERIALNUMBER]):
@ -103,31 +105,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
smappee = Smappee(api=smappee_api)
await hass.async_add_executor_job(smappee.load_service_locations)
hass.data[DOMAIN][entry.entry_id] = SmappeeBase(hass, smappee)
entry.runtime_data = SmappeeBase(hass, smappee)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: SmappeeConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id, None)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
class SmappeeBase:
"""An object to hold the PySmappee instance."""
def __init__(self, hass, smappee):
def __init__(self, hass: HomeAssistant, smappee: Smappee) -> None:
"""Initialize the Smappee API wrapper class."""
self.hass = hass
self.smappee = smappee
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self):
async def async_update(self) -> None:
"""Update all Smappee trends and appliance states."""
await self.hass.async_add_executor_job(
self.smappee.update_trends_and_appliance_states

View File

@ -6,11 +6,11 @@ from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmappeeConfigEntry
from .const import DOMAIN
BINARY_SENSOR_PREFIX = "Appliance"
@ -36,11 +36,11 @@ ICON_MAPPING = {
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: SmappeeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smappee binary sensor."""
smappee_base = hass.data[DOMAIN][config_entry.entry_id]
smappee_base = config_entry.runtime_data
entities: list[BinarySensorEntity] = []
for service_location in smappee_base.smappee.service_locations.values():

View File

@ -10,12 +10,12 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfElectricPotential, UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmappeeConfigEntry
from .const import DOMAIN
@ -188,11 +188,11 @@ VOLTAGE_SENSORS: tuple[SmappeeVoltageSensorEntityDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: SmappeeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smappee sensor."""
smappee_base = hass.data[DOMAIN][config_entry.entry_id]
smappee_base = config_entry.runtime_data
entities = []
for service_location in smappee_base.smappee.service_locations.values():

View File

@ -3,11 +3,11 @@
from typing import Any
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmappeeConfigEntry
from .const import DOMAIN
SWITCH_PREFIX = "Switch"
@ -15,11 +15,11 @@ SWITCH_PREFIX = "Switch"
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: SmappeeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smappee Comfort Plugs."""
smappee_base = hass.data[DOMAIN][config_entry.entry_id]
smappee_base = config_entry.runtime_data
entities = []
for service_location in smappee_base.smappee.service_locations.values():