Migrate kmtronic to use runtime_data (#147193)

This commit is contained in:
epenet 2025-06-20 11:39:13 +02:00 committed by GitHub
parent 3c91c78383
commit cd51070219
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 25 deletions

View File

@ -3,18 +3,16 @@
from pykmtronic.auth import Auth from pykmtronic.auth import Auth
from pykmtronic.hub import KMTronicHubAPI from pykmtronic.hub import KMTronicHubAPI
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from .const import DATA_COORDINATOR, DATA_HUB, DOMAIN from .coordinator import KMTronicConfigEntry, KMtronicCoordinator
from .coordinator import KMtronicCoordinator
PLATFORMS = [Platform.SWITCH] PLATFORMS = [Platform.SWITCH]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: KMTronicConfigEntry) -> bool:
"""Set up kmtronic from a config entry.""" """Set up kmtronic from a config entry."""
session = aiohttp_client.async_get_clientsession(hass) session = aiohttp_client.async_get_clientsession(hass)
auth = Auth( auth = Auth(
@ -27,11 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = KMtronicCoordinator(hass, entry, hub) coordinator = KMtronicCoordinator(hass, entry, hub)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {}) entry.runtime_data = coordinator
hass.data[DOMAIN][entry.entry_id] = {
DATA_HUB: hub,
DATA_COORDINATOR: coordinator,
}
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -40,15 +34,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None: async def async_update_options(
hass: HomeAssistant, config_entry: KMTronicConfigEntry
) -> None:
"""Update options.""" """Update options."""
await hass.config_entries.async_reload(config_entry.entry_id) await hass.config_entries.async_reload(config_entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: KMTronicConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -4,7 +4,4 @@ DOMAIN = "kmtronic"
CONF_REVERSE = "reverse" CONF_REVERSE = "reverse"
DATA_HUB = "hub"
DATA_COORDINATOR = "coordinator"
MANUFACTURER = "KMtronic" MANUFACTURER = "KMtronic"

View File

@ -18,12 +18,16 @@ PLATFORMS = [Platform.SWITCH]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type KMTronicConfigEntry = ConfigEntry[KMtronicCoordinator]
class KMtronicCoordinator(DataUpdateCoordinator[None]): class KMtronicCoordinator(DataUpdateCoordinator[None]):
"""Coordinator for KMTronic.""" """Coordinator for KMTronic."""
entry: KMTronicConfigEntry
def __init__( def __init__(
self, hass: HomeAssistant, entry: ConfigEntry, hub: KMTronicHubAPI self, hass: HomeAssistant, entry: KMTronicConfigEntry, hub: KMTronicHubAPI
) -> None: ) -> None:
"""Initialize the KMTronic coordinator.""" """Initialize the KMTronic coordinator."""
super().__init__( super().__init__(

View File

@ -4,23 +4,23 @@ from typing import Any
import urllib.parse import urllib.parse
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
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 CONF_REVERSE, DATA_COORDINATOR, DATA_HUB, DOMAIN, MANUFACTURER from .const import CONF_REVERSE, DOMAIN, MANUFACTURER
from .coordinator import KMTronicConfigEntry
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: KMTronicConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Config entry example.""" """Config entry example."""
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] coordinator = entry.runtime_data
hub = hass.data[DOMAIN][entry.entry_id][DATA_HUB] hub = coordinator.hub
reverse = entry.options.get(CONF_REVERSE, False) reverse = entry.options.get(CONF_REVERSE, False)
await hub.async_get_relays() await hub.async_get_relays()