mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Use typed config entry in tplink coordinator (#135182)
This commit is contained in:
parent
a5f70dec96
commit
1ca5f79708
@ -22,7 +22,6 @@ from kasa.iot import IotStrip
|
|||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import network
|
from homeassistant.components import network
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_ALIAS,
|
CONF_ALIAS,
|
||||||
CONF_AUTHENTICATION,
|
CONF_AUTHENTICATION,
|
||||||
@ -59,10 +58,7 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
)
|
)
|
||||||
from .coordinator import TPLinkDataUpdateCoordinator
|
from .coordinator import TPLinkConfigEntry, TPLinkData, TPLinkDataUpdateCoordinator
|
||||||
from .models import TPLinkData
|
|
||||||
|
|
||||||
type TPLinkConfigEntry = ConfigEntry[TPLinkData]
|
|
||||||
|
|
||||||
DISCOVERY_INTERVAL = timedelta(minutes=15)
|
DISCOVERY_INTERVAL = timedelta(minutes=15)
|
||||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||||
@ -236,7 +232,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: TPLinkConfigEntry) -> bo
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
parent_coordinator = TPLinkDataUpdateCoordinator(hass, device, timedelta(seconds=5))
|
parent_coordinator = TPLinkDataUpdateCoordinator(
|
||||||
|
hass, device, timedelta(seconds=5), entry
|
||||||
|
)
|
||||||
child_coordinators: list[TPLinkDataUpdateCoordinator] = []
|
child_coordinators: list[TPLinkDataUpdateCoordinator] = []
|
||||||
|
|
||||||
# The iot HS300 allows a limited number of concurrent requests and fetching the
|
# The iot HS300 allows a limited number of concurrent requests and fetching the
|
||||||
@ -245,7 +243,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: TPLinkConfigEntry) -> bo
|
|||||||
child_coordinators = [
|
child_coordinators = [
|
||||||
# The child coordinators only update energy data so we can
|
# The child coordinators only update energy data so we can
|
||||||
# set a longer update interval to avoid flooding the device
|
# set a longer update interval to avoid flooding the device
|
||||||
TPLinkDataUpdateCoordinator(hass, child, timedelta(seconds=60))
|
TPLinkDataUpdateCoordinator(hass, child, timedelta(seconds=60), entry)
|
||||||
for child in device.children
|
for child in device.children
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -2,38 +2,56 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from kasa import AuthenticationError, Device, KasaException
|
from kasa import AuthenticationError, Credentials, Device, KasaException
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers.debounce import Debouncer
|
from homeassistant.helpers.debounce import Debouncer
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(slots=True)
|
||||||
|
class TPLinkData:
|
||||||
|
"""Data for the tplink integration."""
|
||||||
|
|
||||||
|
parent_coordinator: TPLinkDataUpdateCoordinator
|
||||||
|
children_coordinators: list[TPLinkDataUpdateCoordinator]
|
||||||
|
camera_credentials: Credentials | None
|
||||||
|
live_view: bool | None
|
||||||
|
|
||||||
|
|
||||||
|
type TPLinkConfigEntry = ConfigEntry[TPLinkData]
|
||||||
|
|
||||||
REQUEST_REFRESH_DELAY = 0.35
|
REQUEST_REFRESH_DELAY = 0.35
|
||||||
|
|
||||||
|
|
||||||
class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||||
"""DataUpdateCoordinator to gather data for a specific TPLink device."""
|
"""DataUpdateCoordinator to gather data for a specific TPLink device."""
|
||||||
|
|
||||||
config_entry: config_entries.ConfigEntry
|
config_entry: TPLinkConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
device: Device,
|
device: Device,
|
||||||
update_interval: timedelta,
|
update_interval: timedelta,
|
||||||
|
config_entry: TPLinkConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize DataUpdateCoordinator to gather data for specific SmartPlug."""
|
"""Initialize DataUpdateCoordinator to gather data for specific SmartPlug."""
|
||||||
self.device = device
|
self.device = device
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=config_entry,
|
||||||
name=device.host,
|
name=device.host,
|
||||||
update_interval=update_interval,
|
update_interval=update_interval,
|
||||||
# We don't want an immediate refresh since the device
|
# We don't want an immediate refresh since the device
|
||||||
@ -48,6 +66,20 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
|||||||
try:
|
try:
|
||||||
await self.device.update(update_children=False)
|
await self.device.update(update_children=False)
|
||||||
except AuthenticationError as ex:
|
except AuthenticationError as ex:
|
||||||
raise ConfigEntryAuthFailed from ex
|
raise ConfigEntryAuthFailed(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="device_authentication",
|
||||||
|
translation_placeholders={
|
||||||
|
"func": "update",
|
||||||
|
"exc": str(ex),
|
||||||
|
},
|
||||||
|
) from ex
|
||||||
except KasaException as ex:
|
except KasaException as ex:
|
||||||
raise UpdateFailed(ex) from ex
|
raise UpdateFailed(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="device_error",
|
||||||
|
translation_placeholders={
|
||||||
|
"func": "update",
|
||||||
|
"exc": str(ex),
|
||||||
|
},
|
||||||
|
) from ex
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
"""The tplink integration models."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
from kasa import Credentials
|
|
||||||
|
|
||||||
from .coordinator import TPLinkDataUpdateCoordinator
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
|
||||||
class TPLinkData:
|
|
||||||
"""Data for the tplink integration."""
|
|
||||||
|
|
||||||
parent_coordinator: TPLinkDataUpdateCoordinator
|
|
||||||
children_coordinators: list[TPLinkDataUpdateCoordinator]
|
|
||||||
camera_credentials: Credentials | None
|
|
||||||
live_view: bool | None
|
|
Loading…
x
Reference in New Issue
Block a user