Skip to content

Commit

Permalink
Improve device condition type hinting (home-assistant#54906)
Browse files Browse the repository at this point in the history
  • Loading branch information
scop authored Aug 20, 2021
1 parent 9633b9f commit debc6d6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
6 changes: 5 additions & 1 deletion homeassistant/components/binary_sensor/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
DOMAIN,
)

# mypy: disallow-any-generics

DEVICE_CLASS_NONE = "none"

CONF_IS_BAT_LOW = "is_bat_low"
Expand Down Expand Up @@ -266,7 +268,9 @@ def async_condition_from_config(
return condition.state_from_config(state_config)


async def async_get_condition_capabilities(hass: HomeAssistant, config: dict) -> dict:
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
return {
"extra_fields": vol.Schema(
Expand Down
14 changes: 9 additions & 5 deletions homeassistant/components/cover/device_condition.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Provides device automations for Cover."""
from __future__ import annotations

from typing import Any

import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -38,6 +36,8 @@
SUPPORT_SET_TILT_POSITION,
)

# mypy: disallow-any-generics

POSITION_CONDITION_TYPES = {"is_position", "is_tilt_position"}
STATE_CONDITION_TYPES = {"is_open", "is_closed", "is_opening", "is_closing"}

Expand Down Expand Up @@ -67,10 +67,12 @@
CONDITION_SCHEMA = vol.Any(POSITION_CONDITION_SCHEMA, STATE_CONDITION_SCHEMA)


async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict]:
async def async_get_conditions(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
"""List device conditions for Cover devices."""
registry = await entity_registry.async_get_registry(hass)
conditions: list[dict[str, Any]] = []
conditions: list[dict[str, str]] = []

# Get all the integrations entities for this device
for entry in entity_registry.async_entries_for_device(registry, device_id):
Expand Down Expand Up @@ -100,7 +102,9 @@ async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict
return conditions


async def async_get_condition_capabilities(hass: HomeAssistant, config: dict) -> dict:
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
if config[CONF_TYPE] not in ["is_position", "is_tilt_position"]:
return {}
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/device_automation/toggle_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ async def async_get_triggers(
return await _async_get_automations(hass, device_id, ENTITY_TRIGGERS, domain)


async def async_get_condition_capabilities(hass: HomeAssistant, config: dict) -> dict:
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
return {
"extra_fields": vol.Schema(
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/light/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from . import DOMAIN

# mypy: disallow-any-generics

CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
Expand All @@ -33,6 +35,8 @@ async def async_get_conditions(
return await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)


async def async_get_condition_capabilities(hass: HomeAssistant, config: dict) -> dict:
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
return await toggle_entity.async_get_condition_capabilities(hass, config)
6 changes: 5 additions & 1 deletion homeassistant/components/lock/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from . import DOMAIN

# mypy: disallow-any-generics

CONDITION_TYPES = {
"is_locked",
"is_unlocked",
Expand All @@ -39,7 +41,9 @@
)


async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict]:
async def async_get_conditions(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
"""List device conditions for Lock devices."""
registry = await entity_registry.async_get_registry(hass)
conditions = []
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/remote/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from . import DOMAIN

# mypy: disallow-any-generics

CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
Expand All @@ -33,6 +35,8 @@ async def async_get_conditions(
return await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)


async def async_get_condition_capabilities(hass: HomeAssistant, config: dict) -> dict:
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
return await toggle_entity.async_get_condition_capabilities(hass, config)
6 changes: 3 additions & 3 deletions homeassistant/components/select/device_condition.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Provide the device conditions for Select."""
from __future__ import annotations

from typing import Any

import voluptuous as vol

from homeassistant.const import (
Expand All @@ -21,6 +19,8 @@

from .const import ATTR_OPTIONS, CONF_OPTION, DOMAIN

# nypy: disallow-any-generics

CONDITION_TYPES = {"selected_option"}

CONDITION_SCHEMA = DEVICE_CONDITION_BASE_SCHEMA.extend(
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:

async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, Any]:
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
try:
options = get_capability(hass, config[CONF_ENTITY_ID], ATTR_OPTIONS) or []
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/switch/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from . import DOMAIN

# mypy: disallow-any-generics

CONDITION_SCHEMA = toggle_entity.CONDITION_SCHEMA.extend(
{vol.Required(CONF_DOMAIN): DOMAIN}
)
Expand All @@ -33,6 +35,8 @@ async def async_get_conditions(
return await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)


async def async_get_condition_capabilities(hass: HomeAssistant, config: dict) -> dict:
async def async_get_condition_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List condition capabilities."""
return await toggle_entity.async_get_condition_capabilities(hass, config)

0 comments on commit debc6d6

Please sign in to comment.