Improve type hints in harmony (#76445)

This commit is contained in:
epenet 2022-08-11 09:50:35 +02:00 committed by GitHub
parent 8f0ade7a68
commit 7fc2a73c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 23 deletions

View File

@ -40,7 +40,7 @@ class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
self._attr_name = name self._attr_name = name
@property @property
def icon(self): def icon(self) -> str:
"""Return a representative icon.""" """Return a representative icon."""
if not self.available or self.current_option == ACTIVITY_POWER_OFF: if not self.available or self.current_option == ACTIVITY_POWER_OFF:
return "mdi:remote-tv-off" return "mdi:remote-tv-off"
@ -52,7 +52,7 @@ class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
return [ACTIVITY_POWER_OFF] + sorted(self._data.activity_names) return [ACTIVITY_POWER_OFF] + sorted(self._data.activity_names)
@property @property
def current_option(self): def current_option(self) -> str | None:
"""Return the current activity.""" """Return the current activity."""
_, activity_name = self._data.current_activity _, activity_name = self._data.current_activity
return activity_name return activity_name
@ -61,18 +61,19 @@ class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
"""Change the current activity.""" """Change the current activity."""
await self._data.async_start_activity(option) await self._data.async_start_activity(option)
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass.""" """Call when entity is added to hass."""
self.async_on_remove(
callbacks = { self._data.async_subscribe(
"connected": self.async_got_connected, HarmonyCallback(
"disconnected": self.async_got_disconnected, connected=self.async_got_connected,
"activity_starting": self._async_activity_update, disconnected=self.async_got_disconnected,
"activity_started": self._async_activity_update, activity_starting=self._async_activity_update,
"config_updated": None, activity_started=self._async_activity_update,
} config_updated=None,
)
self.async_on_remove(self._data.async_subscribe(HarmonyCallback(**callbacks))) )
)
@callback @callback
def _async_activity_update(self, activity_info: tuple): def _async_activity_update(self, activity_info: tuple):

View File

@ -7,12 +7,12 @@ import logging
# https://bugs.python.org/issue42965 # https://bugs.python.org/issue42965
from typing import Any, Callable, NamedTuple, Optional from typing import Any, Callable, NamedTuple, Optional
from homeassistant.core import callback from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
NoParamCallback = Optional[Callable[[object], Any]] NoParamCallback = Optional[Callable[[], Any]]
ActivityCallback = Optional[Callable[[object, tuple], Any]] ActivityCallback = Optional[Callable[[tuple], Any]]
class HarmonyCallback(NamedTuple): class HarmonyCallback(NamedTuple):
@ -28,11 +28,11 @@ class HarmonyCallback(NamedTuple):
class HarmonySubscriberMixin: class HarmonySubscriberMixin:
"""Base implementation for a subscriber.""" """Base implementation for a subscriber."""
def __init__(self, hass): def __init__(self, hass: HomeAssistant) -> None:
"""Initialize an subscriber.""" """Initialize an subscriber."""
super().__init__() super().__init__()
self._hass = hass self._hass = hass
self._subscriptions = [] self._subscriptions: list[HarmonyCallback] = []
self._activity_lock = asyncio.Lock() self._activity_lock = asyncio.Lock()
async def async_lock_start_activity(self): async def async_lock_start_activity(self):
@ -40,23 +40,23 @@ class HarmonySubscriberMixin:
await self._activity_lock.acquire() await self._activity_lock.acquire()
@callback @callback
def async_unlock_start_activity(self): def async_unlock_start_activity(self) -> None:
"""Release the lock.""" """Release the lock."""
if self._activity_lock.locked(): if self._activity_lock.locked():
self._activity_lock.release() self._activity_lock.release()
@callback @callback
def async_subscribe(self, update_callbacks: HarmonyCallback) -> Callable: def async_subscribe(self, update_callbacks: HarmonyCallback) -> CALLBACK_TYPE:
"""Add a callback subscriber.""" """Add a callback subscriber."""
self._subscriptions.append(update_callbacks) self._subscriptions.append(update_callbacks)
def _unsubscribe(): def _unsubscribe() -> None:
self.async_unsubscribe(update_callbacks) self.async_unsubscribe(update_callbacks)
return _unsubscribe return _unsubscribe
@callback @callback
def async_unsubscribe(self, update_callback: HarmonyCallback): def async_unsubscribe(self, update_callback: HarmonyCallback) -> None:
"""Remove a callback subscriber.""" """Remove a callback subscriber."""
self._subscriptions.remove(update_callback) self._subscriptions.remove(update_callback)
@ -85,7 +85,7 @@ class HarmonySubscriberMixin:
self.async_unlock_start_activity() self.async_unlock_start_activity()
self._call_callbacks("activity_started", activity_info) self._call_callbacks("activity_started", activity_info)
def _call_callbacks(self, callback_func_name: str, argument: tuple = None): def _call_callbacks(self, callback_func_name: str, argument: tuple = None) -> None:
for subscription in self._subscriptions: for subscription in self._subscriptions:
current_callback = getattr(subscription, callback_func_name) current_callback = getattr(subscription, callback_func_name)
if current_callback: if current_callback: