From ee5f1b15776e7c3008de8139f477ded63aee72e9 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 23 May 2022 14:59:12 +0200 Subject: [PATCH] Adjust device_automation type hints in hue (#72144) --- .../components/hue/device_trigger.py | 18 +++++++---- .../components/hue/v1/device_trigger.py | 32 ++++++++++++++----- .../components/hue/v2/device_trigger.py | 12 ++++--- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/hue/device_trigger.py b/homeassistant/components/hue/device_trigger.py index 29df1fd2476d..e8eb7695ed99 100644 --- a/homeassistant/components/hue/device_trigger.py +++ b/homeassistant/components/hue/device_trigger.py @@ -1,7 +1,7 @@ """Provides device automations for Philips Hue events.""" from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -33,7 +33,9 @@ if TYPE_CHECKING: from .bridge import HueBridge -async def async_validate_trigger_config(hass: "HomeAssistant", config: ConfigType): +async def async_validate_trigger_config( + hass: HomeAssistant, config: ConfigType +) -> ConfigType: """Validate config.""" if DOMAIN not in hass.data: # happens at startup @@ -51,13 +53,14 @@ async def async_validate_trigger_config(hass: "HomeAssistant", config: ConfigTyp if bridge.api_version == 1: return await async_validate_trigger_config_v1(bridge, device_entry, config) return await async_validate_trigger_config_v2(bridge, device_entry, config) + return config async def async_attach_trigger( - hass: "HomeAssistant", + hass: HomeAssistant, config: ConfigType, - action: "AutomationActionType", - automation_info: "AutomationTriggerInfo", + action: AutomationActionType, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" device_id = config[CONF_DEVICE_ID] @@ -82,7 +85,9 @@ async def async_attach_trigger( ) -async def async_get_triggers(hass: "HomeAssistant", device_id: str): +async def async_get_triggers( + hass: HomeAssistant, device_id: str +) -> list[dict[str, Any]]: """Get device triggers for given (hass) device id.""" if DOMAIN not in hass.data: return [] @@ -101,3 +106,4 @@ async def async_get_triggers(hass: "HomeAssistant", device_id: str): if bridge.api_version == 1: return async_get_triggers_v1(bridge, device_entry) return async_get_triggers_v2(bridge, device_entry) + return [] diff --git a/homeassistant/components/hue/v1/device_trigger.py b/homeassistant/components/hue/v1/device_trigger.py index 7b58bf420899..579f4b71efbf 100644 --- a/homeassistant/components/hue/v1/device_trigger.py +++ b/homeassistant/components/hue/v1/device_trigger.py @@ -3,6 +3,10 @@ from typing import TYPE_CHECKING import voluptuous as vol +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -16,8 +20,9 @@ from homeassistant.const import ( CONF_TYPE, CONF_UNIQUE_ID, ) -from homeassistant.core import callback +from homeassistant.core import CALLBACK_TYPE, callback from homeassistant.helpers.device_registry import DeviceEntry +from homeassistant.helpers.typing import ConfigType from ..const import ATTR_HUE_EVENT, CONF_SUBTYPE, DOMAIN @@ -95,7 +100,7 @@ HUE_FOHSWITCH_REMOTE = { } -REMOTES = { +REMOTES: dict[str, dict[tuple[str, str], dict[str, int]]] = { HUE_DIMMER_REMOTE_MODEL: HUE_DIMMER_REMOTE, HUE_TAP_REMOTE_MODEL: HUE_TAP_REMOTE, HUE_BUTTON_REMOTE_MODEL: HUE_BUTTON_REMOTE, @@ -114,7 +119,9 @@ def _get_hue_event_from_device_id(hass, device_id): return None -async def async_validate_trigger_config(bridge, device_entry, config): +async def async_validate_trigger_config( + bridge: "HueBridge", device_entry: DeviceEntry, config: ConfigType +) -> ConfigType: """Validate config.""" config = TRIGGER_SCHEMA(config) trigger = (config[CONF_TYPE], config[CONF_SUBTYPE]) @@ -137,7 +144,13 @@ async def async_validate_trigger_config(bridge, device_entry, config): return config -async def async_attach_trigger(bridge, device_entry, config, action, automation_info): +async def async_attach_trigger( + bridge: "HueBridge", + device_entry: DeviceEntry, + config: ConfigType, + action: AutomationActionType, + automation_info: AutomationTriggerInfo, +) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" hass = bridge.hass @@ -145,9 +158,10 @@ async def async_attach_trigger(bridge, device_entry, config, action, automation_ if hue_event is None: raise InvalidDeviceAutomationConfig - trigger = (config[CONF_TYPE], config[CONF_SUBTYPE]) + trigger_key: tuple[str, str] = (config[CONF_TYPE], config[CONF_SUBTYPE]) - trigger = REMOTES[device_entry.model][trigger] + assert device_entry.model + trigger = REMOTES[device_entry.model][trigger_key] event_config = { event_trigger.CONF_PLATFORM: "event", @@ -162,7 +176,9 @@ async def async_attach_trigger(bridge, device_entry, config, action, automation_ @callback -def async_get_triggers(bridge: "HueBridge", device: DeviceEntry): +def async_get_triggers( + bridge: "HueBridge", device: DeviceEntry +) -> list[dict[str, str]]: """Return device triggers for device on `v1` bridge. Make sure device is a supported remote model. @@ -170,7 +186,7 @@ def async_get_triggers(bridge: "HueBridge", device: DeviceEntry): Generate device trigger list. """ if device.model not in REMOTES: - return + return [] triggers = [] for trigger, subtype in REMOTES[device.model]: diff --git a/homeassistant/components/hue/v2/device_trigger.py b/homeassistant/components/hue/v2/device_trigger.py index 8c5da8febb9b..2b868a4685c8 100644 --- a/homeassistant/components/hue/v2/device_trigger.py +++ b/homeassistant/components/hue/v2/device_trigger.py @@ -1,7 +1,7 @@ """Provides device automations for Philips Hue events.""" from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from aiohue.v2.models.button import ButtonEvent from aiohue.v2.models.resource import ResourceTypes @@ -86,7 +86,7 @@ async def async_validate_trigger_config( bridge: "HueBridge", device_entry: DeviceEntry, config: ConfigType, -): +) -> ConfigType: """Validate config.""" config = TRIGGER_SCHEMA(config) check_invalid_device_trigger(bridge, config, device_entry) @@ -97,8 +97,8 @@ async def async_attach_trigger( bridge: "HueBridge", device_entry: DeviceEntry, config: ConfigType, - action: "AutomationActionType", - automation_info: "AutomationTriggerInfo", + action: AutomationActionType, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" hass = bridge.hass @@ -120,7 +120,9 @@ async def async_attach_trigger( @callback -def async_get_triggers(bridge: HueBridge, device_entry: DeviceEntry): +def async_get_triggers( + bridge: HueBridge, device_entry: DeviceEntry +) -> list[dict[str, Any]]: """Return device triggers for device on `v2` bridge.""" api: HueBridgeV2 = bridge.api