Update coordinator improvements (#38366)
* Make generic * Add type info to bunch of uses * Recognize requests exceptions * Recognize urllib exceptions
This commit is contained in:
1 parent
76e8870e98
commit
c2a21fa496
11 files changed
+46
-25
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""The cert_expiry component."""
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
@@ -50,7 +51,7 @@ async def async_unload_entry(hass, entry):
|
||||
return await hass.config_entries.async_forward_entry_unload(entry, "sensor")
|
||||
|
||||
|
||||
class CertExpiryDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
class CertExpiryDataUpdateCoordinator(DataUpdateCoordinator[datetime]):
|
||||
"""Class to manage fetching Cert Expiry data from single endpoint."""
|
||||
|
||||
def __init__(self, hass, host, port):
|
||||
@@ -67,7 +68,7 @@ class CertExpiryDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
hass, _LOGGER, name=name, update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
|
||||
async def _async_update_data(self):
|
||||
async def _async_update_data(self) -> Optional[datetime]:
|
||||
"""Fetch certificate."""
|
||||
try:
|
||||
timestamp = await get_cert_expiry_timestamp(self.hass, self.host, self.port)
|
||||
|
||||
@@ -14,7 +14,7 @@ from .const import LOGGER
|
||||
DEFAULT_UPDATE_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
|
||||
class GuardianDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
class GuardianDataUpdateCoordinator(DataUpdateCoordinator[dict]):
|
||||
"""Define an extended DataUpdateCoordinator with some Guardian goodies."""
|
||||
|
||||
def __init__(
|
||||
|
||||
@@ -86,7 +86,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return unload_ok
|
||||
|
||||
|
||||
class IPPDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
class IPPDataUpdateCoordinator(DataUpdateCoordinator[IPPPrinter]):
|
||||
"""Class to manage fetching IPP data from single endpoint."""
|
||||
|
||||
def __init__(
|
||||
|
||||
@@ -108,7 +108,7 @@ def roku_exception_handler(func):
|
||||
return handler
|
||||
|
||||
|
||||
class RokuDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
class RokuDataUpdateCoordinator(DataUpdateCoordinator[Device]):
|
||||
"""Class to manage fetching Roku data."""
|
||||
|
||||
def __init__(
|
||||
|
||||
@@ -21,8 +21,8 @@ from .const import CONF_CLOUDHOOK_URL, DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ToonDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
"""Class to manage fetching WLED data from single endpoint."""
|
||||
class ToonDataUpdateCoordinator(DataUpdateCoordinator[Status]):
|
||||
"""Class to manage fetching Toon data from single endpoint."""
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, *, entry: ConfigEntry, session: OAuth2Session
|
||||
|
||||
@@ -64,7 +64,7 @@ async def async_setup(hass, config):
|
||||
|
||||
include_components = conf.get(CONF_COMPONENT_REPORTING)
|
||||
|
||||
async def check_new_version():
|
||||
async def check_new_version() -> Updater:
|
||||
"""Check if a new version is available and report if one is."""
|
||||
newest, release_notes = await get_newest_version(
|
||||
hass, huuid, include_components
|
||||
@@ -98,7 +98,7 @@ async def async_setup(hass, config):
|
||||
|
||||
return Updater(update_available, newest, release_notes)
|
||||
|
||||
coordinator = hass.data[DOMAIN] = update_coordinator.DataUpdateCoordinator(
|
||||
coordinator = hass.data[DOMAIN] = update_coordinator.DataUpdateCoordinator[Updater](
|
||||
hass,
|
||||
_LOGGER,
|
||||
name="Home Assistant update",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Support for UPnP/IGD Sensors."""
|
||||
from datetime import timedelta
|
||||
from typing import Mapping
|
||||
from typing import Any, Mapping
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import DATA_BYTES, DATA_RATE_KIBIBYTES_PER_SECOND
|
||||
@@ -94,7 +94,7 @@ async def async_setup_entry(
|
||||
update_interval = timedelta(seconds=update_interval_sec)
|
||||
_LOGGER.debug("update_interval: %s", update_interval)
|
||||
_LOGGER.debug("Adding sensors")
|
||||
coordinator = DataUpdateCoordinator(
|
||||
coordinator = DataUpdateCoordinator[Mapping[str, Any]](
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=device.name,
|
||||
@@ -122,7 +122,7 @@ class UpnpSensor(Entity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
||||
device: Device,
|
||||
sensor_type: Mapping[str, str],
|
||||
update_multiplier: int = 2,
|
||||
@@ -169,7 +169,7 @@ class UpnpSensor(Entity):
|
||||
return self._sensor_type["unit"]
|
||||
|
||||
@property
|
||||
def device_info(self) -> Mapping[str, any]:
|
||||
def device_info(self) -> Mapping[str, Any]:
|
||||
"""Get device info."""
|
||||
return {
|
||||
"connections": {(dr.CONNECTION_UPNP, self._device.udn)},
|
||||
|
||||
@@ -582,7 +582,9 @@ class DataManager:
|
||||
update_interval=timedelta(minutes=120),
|
||||
update_method=self.async_subscribe_webhook,
|
||||
)
|
||||
self.poll_data_update_coordinator = DataUpdateCoordinator(
|
||||
self.poll_data_update_coordinator = DataUpdateCoordinator[
|
||||
Dict[MeasureType, Any]
|
||||
](
|
||||
hass,
|
||||
_LOGGER,
|
||||
name="poll_data_update_coordinator",
|
||||
|
||||
@@ -107,7 +107,7 @@ def wled_exception_handler(func):
|
||||
return handler
|
||||
|
||||
|
||||
class WLEDDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
class WLEDDataUpdateCoordinator(DataUpdateCoordinator[WLEDDevice]):
|
||||
"""Class to manage fetching WLED data from single endpoint."""
|
||||
|
||||
def __init__(
|
||||
|
||||
Reference in new issue
Block a user