Improve bluetooth
generic typing (#84891)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
6b95fa5942
commit
972eb34ed9
10 changed files with 71 additions and 42 deletions
|
@ -1,13 +1,14 @@
|
|||
"""Helpers to help coordinate updates."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import abstractmethod
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable, Coroutine, Generator
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from random import randint
|
||||
from time import monotonic
|
||||
from typing import Any, Generic, TypeVar
|
||||
from typing import Any, Generic, Protocol, TypeVar
|
||||
import urllib.error
|
||||
|
||||
import aiohttp
|
||||
|
@ -29,6 +30,9 @@ REQUEST_REFRESH_DEFAULT_COOLDOWN = 10
|
|||
REQUEST_REFRESH_DEFAULT_IMMEDIATE = True
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_BaseDataUpdateCoordinatorT = TypeVar(
|
||||
"_BaseDataUpdateCoordinatorT", bound="BaseDataUpdateCoordinatorProtocol"
|
||||
)
|
||||
_DataUpdateCoordinatorT = TypeVar(
|
||||
"_DataUpdateCoordinatorT", bound="DataUpdateCoordinator[Any]"
|
||||
)
|
||||
|
@ -38,7 +42,17 @@ class UpdateFailed(Exception):
|
|||
"""Raised when an update has failed."""
|
||||
|
||||
|
||||
class DataUpdateCoordinator(Generic[_T]):
|
||||
class BaseDataUpdateCoordinatorProtocol(Protocol):
|
||||
"""Base protocol type for DataUpdateCoordinator."""
|
||||
|
||||
@callback
|
||||
def async_add_listener(
|
||||
self, update_callback: CALLBACK_TYPE, context: Any = None
|
||||
) -> Callable[[], None]:
|
||||
"""Listen for data updates."""
|
||||
|
||||
|
||||
class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]):
|
||||
"""Class to manage fetching data from single endpoint."""
|
||||
|
||||
def __init__(
|
||||
|
@ -346,11 +360,11 @@ class DataUpdateCoordinator(Generic[_T]):
|
|||
self.async_update_listeners()
|
||||
|
||||
|
||||
class CoordinatorEntity(entity.Entity, Generic[_DataUpdateCoordinatorT]):
|
||||
"""A class for entities using DataUpdateCoordinator."""
|
||||
class BaseCoordinatorEntity(entity.Entity, Generic[_BaseDataUpdateCoordinatorT]):
|
||||
"""Base class for all Coordinator entities."""
|
||||
|
||||
def __init__(
|
||||
self, coordinator: _DataUpdateCoordinatorT, context: Any = None
|
||||
self, coordinator: _BaseDataUpdateCoordinatorT, context: Any = None
|
||||
) -> None:
|
||||
"""Create the entity with a DataUpdateCoordinator."""
|
||||
self.coordinator = coordinator
|
||||
|
@ -361,11 +375,6 @@ class CoordinatorEntity(entity.Entity, Generic[_DataUpdateCoordinatorT]):
|
|||
"""No need to poll. Coordinator notifies entity of updates."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""When entity is added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
|
@ -380,6 +389,22 @@ class CoordinatorEntity(entity.Entity, Generic[_DataUpdateCoordinatorT]):
|
|||
"""Handle updated data from the coordinator."""
|
||||
self.async_write_ha_state()
|
||||
|
||||
@abstractmethod
|
||||
async def async_update(self) -> None:
|
||||
"""Update the entity.
|
||||
|
||||
Only used by the generic entity update service.
|
||||
"""
|
||||
|
||||
|
||||
class CoordinatorEntity(BaseCoordinatorEntity[_DataUpdateCoordinatorT]):
|
||||
"""A class for entities using DataUpdateCoordinator."""
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Update the entity.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue