Helpers type hint improvements (#38522)

This commit is contained in:
Ville Skyttä 2020-08-13 00:01:10 +03:00 committed by GitHub
parent 15db2225da
commit 991bf126d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 18 deletions

View File

@ -5,8 +5,6 @@ from homeassistant import config_entries
from .typing import HomeAssistantType
# mypy: allow-untyped-defs, no-check-untyped-defs
DiscoveryFunctionType = Callable[[], Union[Awaitable[bool], bool]]

View File

@ -164,7 +164,7 @@ class EntityComponent:
scan_interval=getattr(platform, "SCAN_INTERVAL", None),
)
return await self._platforms[key].async_setup_entry(config_entry) # type: ignore
return await self._platforms[key].async_setup_entry(config_entry)
async def async_unload_entry(self, config_entry: ConfigEntry) -> bool:
"""Unload a config entry."""

View File

@ -4,10 +4,17 @@ from contextvars import ContextVar
from datetime import datetime, timedelta
from logging import Logger
from types import ModuleType
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional
from typing import TYPE_CHECKING, Callable, Coroutine, Dict, Iterable, List, Optional
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import CALLBACK_TYPE, callback, split_entity_id, valid_entity_id
from homeassistant.core import (
CALLBACK_TYPE,
ServiceCall,
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
@ -19,7 +26,7 @@ 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
# mypy: allow-untyped-defs
SLOW_SETUP_WARNING = 10
SLOW_SETUP_MAX_WAIT = 60
@ -53,7 +60,7 @@ class EntityPlatform:
self.platform = platform
self.scan_interval = scan_interval
self.entity_namespace = entity_namespace
self.config_entry = None
self.config_entry: Optional[ConfigEntry] = None
self.entities: Dict[str, Entity] = {} # pylint: disable=used-before-assignment
self._tasks: List[asyncio.Future] = []
# Method to cancel the state change listener
@ -119,10 +126,10 @@ class EntityPlatform:
return
@callback
def async_create_setup_task():
def async_create_setup_task() -> Coroutine:
"""Get task to set up platform."""
if getattr(platform, "async_setup_platform", None):
return platform.async_setup_platform(
return platform.async_setup_platform( # type: ignore
hass,
platform_config,
self._async_schedule_add_entities,
@ -133,7 +140,7 @@ class EntityPlatform:
# we don't want to track this task in case it blocks startup.
return hass.loop.run_in_executor(
None,
platform.setup_platform,
platform.setup_platform, # type: ignore
hass,
platform_config,
self._schedule_add_entities,
@ -142,7 +149,7 @@ class EntityPlatform:
await self._async_setup_platform(async_create_setup_task)
async def async_setup_entry(self, config_entry):
async def async_setup_entry(self, config_entry: ConfigEntry) -> bool:
"""Set up the platform from a config entry."""
# Store it so that we can save config entry ID in entity registry
self.config_entry = config_entry
@ -151,13 +158,15 @@ class EntityPlatform:
@callback
def async_create_setup_task():
"""Get task to set up platform."""
return platform.async_setup_entry(
return platform.async_setup_entry( # type: ignore
self.hass, config_entry, self._async_schedule_add_entities
)
return await self._async_setup_platform(async_create_setup_task)
async def _async_setup_platform(self, async_create_setup_task, tries=0):
async def _async_setup_platform(
self, async_create_setup_task: Callable[[], Coroutine], tries: int = 0
) -> bool:
"""Set up a platform via config file or config entry.
async_create_setup_task creates a coroutine that sets up platform.
@ -340,7 +349,7 @@ class EntityPlatform:
return
requested_entity_id = None
suggested_object_id = None
suggested_object_id: Optional[str] = None
# Get entity_id from unique ID registration
if entity.unique_id is not None:
@ -354,7 +363,7 @@ class EntityPlatform:
suggested_object_id = f"{self.entity_namespace} {suggested_object_id}"
if self.config_entry is not None:
config_entry_id = self.config_entry.entry_id
config_entry_id: Optional[str] = self.config_entry.entry_id
else:
config_entry_id = None
@ -512,7 +521,9 @@ class EntityPlatform:
self._async_unsub_polling()
self._async_unsub_polling = None
async def async_extract_from_service(self, service_call, expand_group=True):
async def async_extract_from_service(
self, service_call: ServiceCall, expand_group: bool = True
) -> List["Entity"]:
"""Extract all known and available entities from a service call.
Will return an empty list if entities specified but unknown.
@ -535,9 +546,9 @@ class EntityPlatform:
if isinstance(schema, dict):
schema = cv.make_entity_service_schema(schema)
async def handle_service(call):
async def handle_service(call: ServiceCall) -> None:
"""Handle the service."""
await service.entity_service_call(
await service.entity_service_call( # type: ignore
self.hass,
[
plf