Type hint improvements (#32905)

* Complete helpers.entity_component type hints

* Add discovery info type
This commit is contained in:
Ville Skyttä authored and GitHub committed 2020-03-18 19:27:25 +02:00
1 parent 7c79adad8f
commit 05abf37046
13 files changed
+120 -76

No files matched your search

+46 -35
View File
@@ -1,18 +1,24 @@
"""Class to manage the entities for a single platform."""
import asyncio
from contextvars import ContextVar
from datetime import datetime
from typing import Optional
from datetime import datetime, timedelta
from logging import Logger
from types import ModuleType
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, cast
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import callback, split_entity_id, valid_entity_id
from homeassistant.core import CALLBACK_TYPE, callback, split_entity_id, valid_entity_id
from homeassistant.exceptions import HomeAssistantError, PlatformNotReady
from homeassistant.helpers import config_validation as cv, service
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util.async_ import run_callback_threadsafe
from .entity_registry import DISABLED_INTEGRATION
from .event import async_call_later, async_track_time_interval
if TYPE_CHECKING:
from .entity import Entity
# mypy: allow-untyped-defs, no-check-untyped-defs
SLOW_SETUP_WARNING = 10
@@ -26,23 +32,15 @@ class EntityPlatform:
def __init__(
self,
*,
hass,
logger,
domain,
platform_name,
platform,
scan_interval,
entity_namespace,
hass: HomeAssistantType,
logger: Logger,
domain: str,
platform_name: str,
platform: Optional[ModuleType],
scan_interval: timedelta,
entity_namespace: Optional[str],
):
"""Initialize the entity platform.
hass: HomeAssistant
logger: Logger
domain: str
platform_name: str
scan_interval: timedelta
entity_namespace: str
"""
"""Initialize the entity platform."""
self.hass = hass
self.logger = logger
self.domain = domain
@@ -51,13 +49,13 @@ class EntityPlatform:
self.scan_interval = scan_interval
self.entity_namespace = entity_namespace
self.config_entry = None
self.entities = {}
self._tasks = []
self.entities: Dict[str, Entity] = {} # pylint: disable=used-before-assignment
self._tasks: List[asyncio.Future] = []
# Method to cancel the state change listener
self._async_unsub_polling = None
self._async_unsub_polling: Optional[CALLBACK_TYPE] = None
# Method to cancel the retry of setup
self._async_cancel_retry_setup = None
self._process_updates = None
self._async_cancel_retry_setup: Optional[CALLBACK_TYPE] = None
self._process_updates: Optional[asyncio.Lock] = None
# Platform is None for the EntityComponent "catch-all" EntityPlatform
# which powers entity_component.add_entities
@@ -224,7 +222,9 @@ class EntityPlatform:
finally:
warn_task.cancel()
def _schedule_add_entities(self, new_entities, update_before_add=False):
def _schedule_add_entities(
self, new_entities: Iterable["Entity"], update_before_add: bool = False
) -> None:
"""Schedule adding entities for a single platform, synchronously."""
run_callback_threadsafe(
self.hass.loop,
@@ -234,17 +234,24 @@ class EntityPlatform:
).result()
@callback
def _async_schedule_add_entities(self, new_entities, update_before_add=False):
def _async_schedule_add_entities(
self, new_entities: Iterable["Entity"], update_before_add: bool = False
) -> None:
"""Schedule adding entities for a single platform async."""
self._tasks.append(
self.hass.async_add_job(
self.async_add_entities(
new_entities, update_before_add=update_before_add
)
cast(
asyncio.Future,
self.hass.async_add_job(
self.async_add_entities( # type: ignore
new_entities, update_before_add=update_before_add
),
),
)
)
def add_entities(self, new_entities, update_before_add=False):
def add_entities(
self, new_entities: Iterable["Entity"], update_before_add: bool = False
) -> None:
"""Add entities for a single platform."""
# That avoid deadlocks
if update_before_add:
@@ -258,7 +265,9 @@ class EntityPlatform:
self.hass.loop,
).result()
async def async_add_entities(self, new_entities, update_before_add=False):
async def async_add_entities(
self, new_entities: Iterable["Entity"], update_before_add: bool = False
) -> None:
"""Add entities for a single platform async.
This method must be run in the event loop.
@@ -272,7 +281,7 @@ class EntityPlatform:
device_registry = await hass.helpers.device_registry.async_get_registry()
entity_registry = await hass.helpers.entity_registry.async_get_registry()
tasks = [
self._async_add_entity(
self._async_add_entity( # type: ignore
entity, update_before_add, entity_registry, device_registry
)
for entity in new_entities
@@ -290,7 +299,9 @@ class EntityPlatform:
return
self._async_unsub_polling = async_track_time_interval(
self.hass, self._update_entity_states, self.scan_interval
self.hass,
self._update_entity_states, # type: ignore
self.scan_interval,
)
async def _async_add_entity(
@@ -515,7 +526,7 @@ class EntityPlatform:
for entity in self.entities.values():
if not entity.should_poll:
continue
tasks.append(entity.async_update_ha_state(True))
tasks.append(entity.async_update_ha_state(True)) # type: ignore
if tasks:
await asyncio.wait(tasks)