Add setup type hints to group (#64002)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-01-13 12:25:20 +01:00 committed by GitHub
parent af23a23888
commit bd859f428a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ import asyncio
from collections.abc import Iterable from collections.abc import Iterable
from contextvars import ContextVar from contextvars import ContextVar
import logging import logging
from typing import Any, cast from typing import Any, Union, cast
import voluptuous as vol import voluptuous as vol
@ -36,6 +36,7 @@ from homeassistant.helpers.integration_platform import (
async_process_integration_platforms, async_process_integration_platforms,
) )
from homeassistant.helpers.reload import async_reload_integration_platforms from homeassistant.helpers.reload import async_reload_integration_platforms
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs # mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
@ -216,10 +217,12 @@ def groups_with_entity(hass: HomeAssistant, entity_id: str) -> list[str]:
return groups return groups
async def async_setup(hass, config): async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up all groups found defined in the configuration.""" """Set up all groups found defined in the configuration."""
if (component := hass.data.get(DOMAIN)) is None: if DOMAIN not in hass.data:
component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass) hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
component: EntityComponent = hass.data[DOMAIN]
hass.data[REG_KEY] = GroupIntegrationRegistry() hass.data[REG_KEY] = GroupIntegrationRegistry()
@ -229,7 +232,11 @@ async def async_setup(hass, config):
async def reload_service_handler(service: ServiceCall) -> None: async def reload_service_handler(service: ServiceCall) -> None:
"""Remove all user-defined groups and load new ones from config.""" """Remove all user-defined groups and load new ones from config."""
auto = list(filter(lambda e: not e.user_defined, component.entities)) auto = [
cast(Group, e)
for e in component.entities
if not cast(Group, e).user_defined
]
if (conf := await component.async_prepare_reload()) is None: if (conf := await component.async_prepare_reload()) is None:
return return
@ -254,7 +261,7 @@ async def async_setup(hass, config):
"""Handle dynamic group service functions.""" """Handle dynamic group service functions."""
object_id = service.data[ATTR_OBJECT_ID] object_id = service.data[ATTR_OBJECT_ID]
entity_id = f"{DOMAIN}.{object_id}" entity_id = f"{DOMAIN}.{object_id}"
group = component.get_entity(entity_id) group: Group | None = cast(Union[Group, None], component.get_entity(entity_id))
# new group # new group
if service.service == SERVICE_SET and group is None: if service.service == SERVICE_SET and group is None: