Improve type hints in homeassistant scene (#76930)

This commit is contained in:
epenet 2022-08-18 15:39:56 +02:00 committed by GitHub
parent c7301a449b
commit 24f1287bf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,9 @@
"""Allow users to set and activate scenes.""" """Allow users to set and activate scenes."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping, ValuesView
import logging import logging
from typing import Any, NamedTuple from typing import Any, NamedTuple, cast
import voluptuous as vol import voluptuous as vol
@ -34,16 +35,16 @@ from homeassistant.helpers import (
config_validation as cv, config_validation as cv,
entity_platform, entity_platform,
) )
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback, EntityPlatform
from homeassistant.helpers.service import async_register_admin_service from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.state import async_reproduce_state from homeassistant.helpers.state import async_reproduce_state
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.loader import async_get_integration from homeassistant.loader import async_get_integration
def _convert_states(states): def _convert_states(states: dict[str, Any]) -> dict[str, State]:
"""Convert state definitions to State objects.""" """Convert state definitions to State objects."""
result = {} result: dict[str, State] = {}
for entity_id, info in states.items(): for entity_id, info in states.items():
entity_id = cv.entity_id(entity_id) entity_id = cv.entity_id(entity_id)
@ -68,7 +69,7 @@ def _convert_states(states):
return result return result
def _ensure_no_intersection(value): def _ensure_no_intersection(value: dict[str, Any]) -> dict[str, Any]:
"""Validate that entities and snapshot_entities do not overlap.""" """Validate that entities and snapshot_entities do not overlap."""
if ( if (
CONF_SNAPSHOT not in value CONF_SNAPSHOT not in value
@ -143,11 +144,12 @@ def scenes_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]:
if DATA_PLATFORM not in hass.data: if DATA_PLATFORM not in hass.data:
return [] return []
platform = hass.data[DATA_PLATFORM] platform: EntityPlatform = hass.data[DATA_PLATFORM]
scene_entities = cast(ValuesView[HomeAssistantScene], platform.entities.values())
return [ return [
scene_entity.entity_id scene_entity.entity_id
for scene_entity in platform.entities.values() for scene_entity in scene_entities
if entity_id in scene_entity.scene_config.states if entity_id in scene_entity.scene_config.states
] ]
@ -158,12 +160,12 @@ def entities_in_scene(hass: HomeAssistant, entity_id: str) -> list[str]:
if DATA_PLATFORM not in hass.data: if DATA_PLATFORM not in hass.data:
return [] return []
platform = hass.data[DATA_PLATFORM] platform: EntityPlatform = hass.data[DATA_PLATFORM]
if (entity := platform.entities.get(entity_id)) is None: if (entity := platform.entities.get(entity_id)) is None:
return [] return []
return list(entity.scene_config.states) return list(cast(HomeAssistantScene, entity).scene_config.states)
async def async_setup_platform( async def async_setup_platform(
@ -270,9 +272,12 @@ async def async_setup_platform(
) )
def _process_scenes_config(hass, async_add_entities, config): def _process_scenes_config(
hass, async_add_entities: AddEntitiesCallback, config: dict[str, Any]
) -> None:
"""Process multiple scenes and add them.""" """Process multiple scenes and add them."""
# Check empty list # Check empty list
scene_config: list[dict[str, Any]]
if not (scene_config := config[STATES]): if not (scene_config := config[STATES]):
return return
@ -293,31 +298,33 @@ def _process_scenes_config(hass, async_add_entities, config):
class HomeAssistantScene(Scene): class HomeAssistantScene(Scene):
"""A scene is a group of entities and the states we want them to be.""" """A scene is a group of entities and the states we want them to be."""
def __init__(self, hass, scene_config, from_service=False): def __init__(
self, hass: HomeAssistant, scene_config: SceneConfig, from_service: bool = False
) -> None:
"""Initialize the scene.""" """Initialize the scene."""
self.hass = hass self.hass = hass
self.scene_config = scene_config self.scene_config = scene_config
self.from_service = from_service self.from_service = from_service
@property @property
def name(self): def name(self) -> str:
"""Return the name of the scene.""" """Return the name of the scene."""
return self.scene_config.name return self.scene_config.name
@property @property
def icon(self): def icon(self) -> str | None:
"""Return the icon of the scene.""" """Return the icon of the scene."""
return self.scene_config.icon return self.scene_config.icon
@property @property
def unique_id(self): def unique_id(self) -> str | None:
"""Return unique ID.""" """Return unique ID."""
return self.scene_config.id return self.scene_config.id
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the scene state attributes.""" """Return the scene state attributes."""
attributes = {ATTR_ENTITY_ID: list(self.scene_config.states)} attributes: dict[str, Any] = {ATTR_ENTITY_ID: list(self.scene_config.states)}
if (unique_id := self.unique_id) is not None: if (unique_id := self.unique_id) is not None:
attributes[CONF_ID] = unique_id attributes[CONF_ID] = unique_id
return attributes return attributes