From 4bb2c6e00fd60e768a7f52c2745fb62c88748b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= <ville.skytta@iki.fi> Date: Fri, 20 Aug 2021 07:13:25 +0300 Subject: [PATCH] Improve device action type hinting (#54850) * Improve device action type hinting * More precise _async_get_automations type hints Co-authored-by: Martin Hjelmare <marhje52@gmail.com> --- homeassistant/components/climate/device_action.py | 4 +++- homeassistant/components/cover/device_action.py | 9 +++++++-- .../components/device_automation/toggle_entity.py | 13 +++++++------ homeassistant/components/fan/device_action.py | 4 +++- .../components/humidifier/device_action.py | 6 +++++- homeassistant/components/light/device_action.py | 10 ++++++++-- homeassistant/components/lock/device_action.py | 4 +++- .../components/mobile_app/device_action.py | 4 +++- homeassistant/components/number/device_action.py | 13 ++++++++----- homeassistant/components/remote/device_action.py | 6 +++++- homeassistant/components/select/device_action.py | 8 ++++---- homeassistant/components/switch/device_action.py | 6 +++++- homeassistant/components/vacuum/device_action.py | 4 +++- .../components/water_heater/device_action.py | 4 +++- homeassistant/components/zha/device_action.py | 6 +++++- .../device_action/integration/device_action.py | 4 +++- 16 files changed, 75 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/climate/device_action.py b/homeassistant/components/climate/device_action.py index 6afc4d294cb..34217e8872d 100644 --- a/homeassistant/components/climate/device_action.py +++ b/homeassistant/components/climate/device_action.py @@ -38,7 +38,9 @@ SET_PRESET_MODE_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ACTION_SCHEMA = vol.Any(SET_HVAC_MODE_SCHEMA, SET_PRESET_MODE_SCHEMA) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Climate devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] diff --git a/homeassistant/components/cover/device_action.py b/homeassistant/components/cover/device_action.py index 13ef4523f5b..debb2368cf2 100644 --- a/homeassistant/components/cover/device_action.py +++ b/homeassistant/components/cover/device_action.py @@ -21,6 +21,7 @@ from homeassistant.core import Context, HomeAssistant from homeassistant.helpers import entity_registry import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import get_supported_features +from homeassistant.helpers.typing import ConfigType from . import ( ATTR_POSITION, @@ -58,7 +59,9 @@ POSITION_ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ACTION_SCHEMA = vol.Any(CMD_ACTION_SCHEMA, POSITION_ACTION_SCHEMA) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Cover devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] @@ -98,7 +101,9 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: return actions -async def async_get_action_capabilities(hass: HomeAssistant, config: dict) -> dict: +async def async_get_action_capabilities( + hass: HomeAssistant, config: ConfigType +) -> dict[str, vol.Schema]: """List action capabilities.""" if config[CONF_TYPE] not in POSITION_ACTION_TYPES: return {} diff --git a/homeassistant/components/device_automation/toggle_entity.py b/homeassistant/components/device_automation/toggle_entity.py index cf41fc93d83..6ad2264b516 100644 --- a/homeassistant/components/device_automation/toggle_entity.py +++ b/homeassistant/components/device_automation/toggle_entity.py @@ -1,8 +1,6 @@ """Device automation helpers for toggle entity.""" from __future__ import annotations -from typing import Any - import voluptuous as vol from homeassistant.components.automation import AutomationActionType @@ -169,10 +167,13 @@ async def async_attach_trigger( async def _async_get_automations( - hass: HomeAssistant, device_id: str, automation_templates: list[dict], domain: str -) -> list[dict]: + hass: HomeAssistant, + device_id: str, + automation_templates: list[dict[str, str]], + domain: str, +) -> list[dict[str, str]]: """List device automations.""" - automations: list[dict[str, Any]] = [] + automations: list[dict[str, str]] = [] entity_registry = await hass.helpers.entity_registry.async_get_registry() entries = [ @@ -197,7 +198,7 @@ async def _async_get_automations( async def async_get_actions( hass: HomeAssistant, device_id: str, domain: str -) -> list[dict]: +) -> list[dict[str, str]]: """List device actions.""" return await _async_get_automations(hass, device_id, ENTITY_ACTIONS, domain) diff --git a/homeassistant/components/fan/device_action.py b/homeassistant/components/fan/device_action.py index ddf6a76d3c8..0482c31b929 100644 --- a/homeassistant/components/fan/device_action.py +++ b/homeassistant/components/fan/device_action.py @@ -28,7 +28,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Fan devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] diff --git a/homeassistant/components/humidifier/device_action.py b/homeassistant/components/humidifier/device_action.py index 81df6938236..3ad4b22dcec 100644 --- a/homeassistant/components/humidifier/device_action.py +++ b/homeassistant/components/humidifier/device_action.py @@ -19,6 +19,8 @@ from homeassistant.helpers.entity import get_capability, get_supported_features from . import DOMAIN, const +# mypy: disallow-any-generics + SET_HUMIDITY_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( { vol.Required(CONF_TYPE): "set_humidity", @@ -40,7 +42,9 @@ ONOFF_SCHEMA = toggle_entity.ACTION_SCHEMA.extend({vol.Required(CONF_DOMAIN): DO ACTION_SCHEMA = vol.Any(SET_HUMIDITY_SCHEMA, SET_MODE_SCHEMA, ONOFF_SCHEMA) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Humidifier devices.""" registry = await entity_registry.async_get_registry(hass) actions = await toggle_entity.async_get_actions(hass, device_id, DOMAIN) diff --git a/homeassistant/components/light/device_action.py b/homeassistant/components/light/device_action.py index 2180bdd3094..a933d04066e 100644 --- a/homeassistant/components/light/device_action.py +++ b/homeassistant/components/light/device_action.py @@ -32,6 +32,8 @@ from . import ( get_supported_color_modes, ) +# mypy: disallow-any-generics + TYPE_BRIGHTNESS_INCREASE = "brightness_increase" TYPE_BRIGHTNESS_DECREASE = "brightness_decrease" TYPE_FLASH = "flash" @@ -86,7 +88,9 @@ async def async_call_action_from_config( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions.""" actions = await toggle_entity.async_get_actions(hass, device_id, DOMAIN) @@ -119,7 +123,9 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: return actions -async def async_get_action_capabilities(hass: HomeAssistant, config: dict) -> dict: +async def async_get_action_capabilities( + hass: HomeAssistant, config: ConfigType +) -> dict[str, vol.Schema]: """List action capabilities.""" if config[CONF_TYPE] != toggle_entity.CONF_TURN_ON: return {} diff --git a/homeassistant/components/lock/device_action.py b/homeassistant/components/lock/device_action.py index 6c0eb2a41d4..50c205d113a 100644 --- a/homeassistant/components/lock/device_action.py +++ b/homeassistant/components/lock/device_action.py @@ -30,7 +30,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Lock devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] diff --git a/homeassistant/components/mobile_app/device_action.py b/homeassistant/components/mobile_app/device_action.py index 33a7510da21..193c25e482c 100644 --- a/homeassistant/components/mobile_app/device_action.py +++ b/homeassistant/components/mobile_app/device_action.py @@ -22,7 +22,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Mobile App devices.""" webhook_id = webhook_id_from_device_id(hass, device_id) diff --git a/homeassistant/components/number/device_action.py b/homeassistant/components/number/device_action.py index 77b36b49f20..77ca633d947 100644 --- a/homeassistant/components/number/device_action.py +++ b/homeassistant/components/number/device_action.py @@ -1,8 +1,6 @@ """Provides device actions for Number.""" from __future__ import annotations -from typing import Any - import voluptuous as vol from homeassistant.const import ( @@ -15,6 +13,7 @@ from homeassistant.const import ( from homeassistant.core import Context, HomeAssistant from homeassistant.helpers import entity_registry import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.typing import ConfigType from . import DOMAIN, const @@ -29,10 +28,12 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Number.""" registry = await entity_registry.async_get_registry(hass) - actions: list[dict[str, Any]] = [] + actions: list[dict[str, str]] = [] # Get all the integrations entities for this device for entry in entity_registry.async_entries_for_device(registry, device_id): @@ -67,7 +68,9 @@ async def async_call_action_from_config( ) -async def async_get_action_capabilities(hass: HomeAssistant, config: dict) -> dict: +async def async_get_action_capabilities( + hass: HomeAssistant, config: ConfigType +) -> dict[str, vol.Schema]: """List action capabilities.""" fields = {vol.Required(const.ATTR_VALUE): vol.Coerce(float)} diff --git a/homeassistant/components/remote/device_action.py b/homeassistant/components/remote/device_action.py index aa34eb33224..a337f3275eb 100644 --- a/homeassistant/components/remote/device_action.py +++ b/homeassistant/components/remote/device_action.py @@ -10,6 +10,8 @@ from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import DOMAIN +# mypy: disallow-any-generics + ACTION_SCHEMA = toggle_entity.ACTION_SCHEMA.extend({vol.Required(CONF_DOMAIN): DOMAIN}) @@ -25,6 +27,8 @@ async def async_call_action_from_config( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions.""" return await toggle_entity.async_get_actions(hass, device_id, DOMAIN) diff --git a/homeassistant/components/select/device_action.py b/homeassistant/components/select/device_action.py index ece3c981690..ca9c1963782 100644 --- a/homeassistant/components/select/device_action.py +++ b/homeassistant/components/select/device_action.py @@ -1,8 +1,6 @@ """Provides device actions for Select.""" from __future__ import annotations -from typing import Any - import voluptuous as vol from homeassistant.const import ( @@ -31,7 +29,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Select devices.""" registry = await entity_registry.async_get_registry(hass) return [ @@ -64,7 +64,7 @@ async def async_call_action_from_config( async def async_get_action_capabilities( hass: HomeAssistant, config: ConfigType -) -> dict[str, Any]: +) -> dict[str, vol.Schema]: """List action capabilities.""" try: options = get_capability(hass, config[CONF_ENTITY_ID], ATTR_OPTIONS) or [] diff --git a/homeassistant/components/switch/device_action.py b/homeassistant/components/switch/device_action.py index 0f3890d329f..6947656406b 100644 --- a/homeassistant/components/switch/device_action.py +++ b/homeassistant/components/switch/device_action.py @@ -10,6 +10,8 @@ from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import DOMAIN +# mypy: disallow-any-generics + ACTION_SCHEMA = toggle_entity.ACTION_SCHEMA.extend({vol.Required(CONF_DOMAIN): DOMAIN}) @@ -25,6 +27,8 @@ async def async_call_action_from_config( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions.""" return await toggle_entity.async_get_actions(hass, device_id, DOMAIN) diff --git a/homeassistant/components/vacuum/device_action.py b/homeassistant/components/vacuum/device_action.py index a4df68c3b93..702f3fe7439 100644 --- a/homeassistant/components/vacuum/device_action.py +++ b/homeassistant/components/vacuum/device_action.py @@ -26,7 +26,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Vacuum devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] diff --git a/homeassistant/components/water_heater/device_action.py b/homeassistant/components/water_heater/device_action.py index 3662dee9a5e..dae9e4d579b 100644 --- a/homeassistant/components/water_heater/device_action.py +++ b/homeassistant/components/water_heater/device_action.py @@ -28,7 +28,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for Water Heater devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] diff --git a/homeassistant/components/zha/device_action.py b/homeassistant/components/zha/device_action.py index de39ff50511..36696517eb6 100644 --- a/homeassistant/components/zha/device_action.py +++ b/homeassistant/components/zha/device_action.py @@ -13,6 +13,8 @@ from .api import SERVICE_WARNING_DEVICE_SQUAWK, SERVICE_WARNING_DEVICE_WARN from .core.const import CHANNEL_IAS_WD from .core.helpers import async_get_zha_device +# mypy: disallow-any-generics + ACTION_SQUAWK = "squawk" ACTION_WARN = "warn" ATTR_DATA = "data" @@ -54,7 +56,9 @@ async def async_call_action_from_config( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions.""" try: zha_device = await async_get_zha_device(hass, device_id) diff --git a/script/scaffold/templates/device_action/integration/device_action.py b/script/scaffold/templates/device_action/integration/device_action.py index 720e472851c..5eb5249211b 100644 --- a/script/scaffold/templates/device_action/integration/device_action.py +++ b/script/scaffold/templates/device_action/integration/device_action.py @@ -29,7 +29,9 @@ ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend( ) -async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: +async def async_get_actions( + hass: HomeAssistant, device_id: str +) -> list[dict[str, str]]: """List device actions for NEW_NAME devices.""" registry = await entity_registry.async_get_registry(hass) actions = [] -- GitLab