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>
This commit is contained in:
Ville Skyttä 2021-08-20 07:13:25 +03:00 committed by GitHub
parent fc6d45a63b
commit 4bb2c6e00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 75 additions and 30 deletions

View File

@ -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 = []

View File

@ -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 {}

View File

@ -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)

View File

@ -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 = []

View File

@ -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)

View File

@ -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 {}

View File

@ -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 = []

View File

@ -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)

View File

@ -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)}

View File

@ -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)

View File

@ -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 []

View File

@ -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)

View File

@ -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 = []

View File

@ -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 = []

View File

@ -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)

View File

@ -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 = []