Add missing type hints in entity_platform (#50453)

This commit is contained in:
Ruslan Sayfutdinov 2021-05-12 15:28:44 +01:00 committed by GitHub
parent 70961c79a0
commit 0cb08f9516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 27 deletions

View File

@ -246,9 +246,7 @@ class EntityComponent:
platform_type, platform, scan_interval, entity_namespace platform_type, platform, scan_interval, entity_namespace
) )
await self._platforms[key].async_setup( # type: ignore await self._platforms[key].async_setup(platform_config, discovery_info)
platform_config, discovery_info
)
async def _async_reset(self) -> None: async def _async_reset(self) -> None:
"""Remove entities and reset the entity component to initial values. """Remove entities and reset the entity component to initial values.

View File

@ -33,17 +33,19 @@ from homeassistant.exceptions import (
PlatformNotReady, PlatformNotReady,
RequiredParameterMissing, RequiredParameterMissing,
) )
from homeassistant.helpers import ( from homeassistant.setup import async_start_setup
from homeassistant.util.async_ import run_callback_threadsafe
from . import (
config_validation as cv, config_validation as cv,
device_registry as dev_reg, device_registry as dev_reg,
entity_registry as ent_reg, entity_registry as ent_reg,
service, service,
) )
from homeassistant.setup import async_start_setup from .device_registry import DeviceRegistry
from homeassistant.util.async_ import run_callback_threadsafe from .entity_registry import DISABLED_INTEGRATION, EntityRegistry
from .entity_registry import DISABLED_INTEGRATION
from .event import async_call_later, async_track_time_interval from .event import async_call_later, async_track_time_interval
from .typing import ConfigType, DiscoveryInfoType
if TYPE_CHECKING: if TYPE_CHECKING:
from .entity import Entity from .entity import Entity
@ -148,7 +150,11 @@ class EntityPlatform:
return self.parallel_updates return self.parallel_updates
async def async_setup(self, platform_config, discovery_info=None): # type: ignore[no-untyped-def] async def async_setup(
self,
platform_config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the platform from a config file.""" """Set up the platform from a config file."""
platform = self.platform platform = self.platform
hass = self.hass hass = self.hass
@ -206,9 +212,9 @@ class EntityPlatform:
platform = self.platform platform = self.platform
@callback @callback
def async_create_setup_task(): # type: ignore[no-untyped-def] def async_create_setup_task() -> Coroutine:
"""Get task to set up platform.""" """Get task to set up platform."""
return platform.async_setup_entry( # type: ignore return platform.async_setup_entry( # type: ignore[no-any-return,union-attr]
self.hass, config_entry, self._async_schedule_add_entities self.hass, config_entry, self._async_schedule_add_entities
) )
@ -273,7 +279,7 @@ class EntityPlatform:
wait_time, wait_time,
) )
async def setup_again(*_): # type: ignore[no-untyped-def] async def setup_again(*_args: Any) -> None:
"""Run setup again.""" """Run setup again."""
self._async_cancel_retry_setup = None self._async_cancel_retry_setup = None
await self._async_setup_platform(async_create_setup_task, tries) await self._async_setup_platform(async_create_setup_task, tries)
@ -360,7 +366,7 @@ class EntityPlatform:
device_registry = dev_reg.async_get(hass) device_registry = dev_reg.async_get(hass)
entity_registry = ent_reg.async_get(hass) entity_registry = ent_reg.async_get(hass)
tasks = [ tasks = [
self._async_add_entity( # type: ignore self._async_add_entity(
entity, update_before_add, entity_registry, device_registry entity, update_before_add, entity_registry, device_registry
) )
for entity in new_entities for entity in new_entities
@ -400,9 +406,13 @@ class EntityPlatform:
self.scan_interval, self.scan_interval,
) )
async def _async_add_entity( # type: ignore[no-untyped-def] # noqa: C901 async def _async_add_entity( # noqa: C901
self, entity, update_before_add, entity_registry, device_registry self,
): entity: Entity,
update_before_add: bool,
entity_registry: EntityRegistry,
device_registry: DeviceRegistry,
) -> None:
"""Add an entity to the platform.""" """Add an entity to the platform."""
if entity is None: if entity is None:
raise ValueError("Entity cannot be None") raise ValueError("Entity cannot be None")
@ -424,6 +434,7 @@ class EntityPlatform:
requested_entity_id = None requested_entity_id = None
suggested_object_id: str | None = None suggested_object_id: str | None = None
generate_new_entity_id = False
# Get entity_id from unique ID registration # Get entity_id from unique ID registration
if entity.unique_id is not None: if entity.unique_id is not None:
@ -431,7 +442,7 @@ class EntityPlatform:
requested_entity_id = entity.entity_id requested_entity_id = entity.entity_id
suggested_object_id = split_entity_id(entity.entity_id)[1] suggested_object_id = split_entity_id(entity.entity_id)[1]
else: else:
suggested_object_id = entity.name suggested_object_id = entity.name # type: ignore[unreachable]
if self.entity_namespace is not None: if self.entity_namespace is not None:
suggested_object_id = f"{self.entity_namespace} {suggested_object_id}" suggested_object_id = f"{self.entity_namespace} {suggested_object_id}"
@ -461,10 +472,10 @@ class EntityPlatform:
"suggested_area", "suggested_area",
): ):
if key in device_info: if key in device_info:
processed_dev_info[key] = device_info[key] processed_dev_info[key] = device_info[key] # type: ignore[misc]
try: try:
device = device_registry.async_get_or_create(**processed_dev_info) device = device_registry.async_get_or_create(**processed_dev_info) # type: ignore[arg-type]
device_id = device.id device_id = device.id
except RequiredParameterMissing: except RequiredParameterMissing:
pass pass
@ -510,10 +521,10 @@ class EntityPlatform:
): ):
# If entity already registered, convert entity id to suggestion # If entity already registered, convert entity id to suggestion
suggested_object_id = split_entity_id(entity.entity_id)[1] suggested_object_id = split_entity_id(entity.entity_id)[1]
entity.entity_id = None generate_new_entity_id = True
# Generate entity ID # Generate entity ID
if entity.entity_id is None: if entity.entity_id is None or generate_new_entity_id:
suggested_object_id = ( suggested_object_id = (
suggested_object_id or entity.name or DEVICE_DEFAULT_NAME suggested_object_id or entity.name or DEVICE_DEFAULT_NAME
) )
@ -565,7 +576,11 @@ class EntityPlatform:
# has a chance to finish. # has a chance to finish.
self.hass.states.async_reserve(entity.entity_id) self.hass.states.async_reserve(entity.entity_id)
entity.async_on_remove(lambda: self.entities.pop(entity_id)) def remove_entity_cb() -> None:
"""Remove entity from entities list."""
self.entities.pop(entity_id)
entity.async_on_remove(remove_entity_cb)
await entity.add_to_platform_finish() await entity.add_to_platform_finish()

View File

@ -10,7 +10,7 @@ timer.
from __future__ import annotations from __future__ import annotations
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Iterable from collections.abc import Iterable, Mapping
import logging import logging
from typing import TYPE_CHECKING, Any, Callable, cast from typing import TYPE_CHECKING, Any, Callable, cast
@ -96,7 +96,7 @@ class RegistryEntry:
) )
), ),
) )
capabilities: dict[str, Any] | None = attr.ib(default=None) capabilities: Mapping[str, Any] | None = attr.ib(default=None)
supported_features: int = attr.ib(default=0) supported_features: int = attr.ib(default=0)
device_class: str | None = attr.ib(default=None) device_class: str | None = attr.ib(default=None)
unit_of_measurement: str | None = attr.ib(default=None) unit_of_measurement: str | None = attr.ib(default=None)
@ -232,7 +232,7 @@ class EntityRegistry:
config_entry: ConfigEntry | None = None, config_entry: ConfigEntry | None = None,
device_id: str | None = None, device_id: str | None = None,
area_id: str | None = None, area_id: str | None = None,
capabilities: dict[str, Any] | None = None, capabilities: Mapping[str, Any] | None = None,
supported_features: int | None = None, supported_features: int | None = None,
device_class: str | None = None, device_class: str | None = None,
unit_of_measurement: str | None = None, unit_of_measurement: str | None = None,
@ -392,7 +392,7 @@ class EntityRegistry:
area_id: str | None | UndefinedType = UNDEFINED, area_id: str | None | UndefinedType = UNDEFINED,
new_unique_id: str | UndefinedType = UNDEFINED, new_unique_id: str | UndefinedType = UNDEFINED,
disabled_by: str | None | UndefinedType = UNDEFINED, disabled_by: str | None | UndefinedType = UNDEFINED,
capabilities: dict[str, Any] | None | UndefinedType = UNDEFINED, capabilities: Mapping[str, Any] | None | UndefinedType = UNDEFINED,
supported_features: int | UndefinedType = UNDEFINED, supported_features: int | UndefinedType = UNDEFINED,
device_class: str | None | UndefinedType = UNDEFINED, device_class: str | None | UndefinedType = UNDEFINED,
unit_of_measurement: str | None | UndefinedType = UNDEFINED, unit_of_measurement: str | None | UndefinedType = UNDEFINED,

View File

@ -124,7 +124,7 @@ async def _async_reconfig_platform(
) -> None: ) -> None:
"""Reconfigure an already loaded platform.""" """Reconfigure an already loaded platform."""
await platform.async_reset() await platform.async_reset()
tasks = [platform.async_setup(p_config) for p_config in platform_configs] # type: ignore tasks = [platform.async_setup(p_config) for p_config in platform_configs]
await asyncio.gather(*tasks) await asyncio.gather(*tasks)