From 61f509bdd82045db362cd482cc0d0a76bc5639b5 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Mon, 1 Mar 2021 09:10:28 -0800 Subject: [PATCH] Minor Hyperion mypy cleanups (#45765) --- homeassistant/components/hyperion/__init__.py | 3 ++- homeassistant/components/hyperion/light.py | 19 ++++++++++++++----- homeassistant/components/hyperion/switch.py | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/hyperion/__init__.py b/homeassistant/components/hyperion/__init__.py index 9e35ae2e6b8..8c001c2b467 100644 --- a/homeassistant/components/hyperion/__init__.py +++ b/homeassistant/components/hyperion/__init__.py @@ -286,7 +286,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b ] ) assert hyperion_client - await async_instances_to_clients_raw(hyperion_client.instances) + if hyperion_client.instances is not None: + await async_instances_to_clients_raw(hyperion_client.instances) hass.data[DOMAIN][config_entry.entry_id][CONF_ON_UNLOAD].append( config_entry.add_update_listener(_async_entry_updated) ) diff --git a/homeassistant/components/hyperion/light.py b/homeassistant/components/hyperion/light.py index 7bb8a75dfc7..c49a65b2bfd 100644 --- a/homeassistant/components/hyperion/light.py +++ b/homeassistant/components/hyperion/light.py @@ -4,7 +4,7 @@ from __future__ import annotations import functools import logging from types import MappingProxyType -from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple +from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple from hyperion import client, const @@ -147,7 +147,7 @@ class HyperionBaseLight(LightEntity): self._static_effect_list += list(const.KEY_COMPONENTID_EXTERNAL_SOURCES) self._effect_list: List[str] = self._static_effect_list[:] - self._client_callbacks = { + self._client_callbacks: Mapping[str, Callable[[Dict[str, Any]], None]] = { f"{const.KEY_ADJUSTMENT}-{const.KEY_UPDATE}": self._update_adjustment, f"{const.KEY_COMPONENTS}-{const.KEY_UPDATE}": self._update_components, f"{const.KEY_EFFECTS}-{const.KEY_UPDATE}": self._update_effect_list, @@ -236,7 +236,7 @@ class HyperionBaseLight(LightEntity): # == Set brightness == if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] - for item in self._client.adjustment: + for item in self._client.adjustment or []: if const.KEY_ID in item: if not await self._client.async_send_set_adjustment( **{ @@ -423,7 +423,12 @@ class HyperionBaseLight(LightEntity): def _get_priority_entry_that_dictates_state(self) -> Optional[Dict[str, Any]]: """Get the relevant Hyperion priority entry to consider.""" # Return the visible priority (whether or not it is the HA priority). - return self._client.visible_priority # type: ignore[no-any-return] + + # Explicit type specifier to ensure this works when the underlying (typed) + # library is installed along with the tests. Casts would trigger a + # redundant-cast warning in this case. + priority: Optional[Dict[str, Any]] = self._client.visible_priority + return priority # pylint: disable=no-self-use def _allow_priority_update(self, priority: Optional[Dict[str, Any]] = None) -> bool: @@ -530,7 +535,11 @@ class HyperionPriorityLight(HyperionBaseLight): if candidate[const.KEY_PRIORITY] == self._get_option( CONF_PRIORITY ) and candidate.get(const.KEY_ACTIVE, False): - return candidate # type: ignore[no-any-return] + # Explicit type specifier to ensure this works when the underlying + # (typed) library is installed along with the tests. Casts would trigger + # a redundant-cast warning in this case. + output: Dict[str, Any] = candidate + return output return None @classmethod diff --git a/homeassistant/components/hyperion/switch.py b/homeassistant/components/hyperion/switch.py index 9d90e1e12ef..0412018650a 100644 --- a/homeassistant/components/hyperion/switch.py +++ b/homeassistant/components/hyperion/switch.py @@ -157,7 +157,7 @@ class HyperionComponentSwitch(SwitchEntity): @property def is_on(self) -> bool: """Return true if the switch is on.""" - for component in self._client.components: + for component in self._client.components or []: if component[KEY_NAME] == self._component_name: return bool(component.setdefault(KEY_ENABLED, False)) return False