mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Helpers type hint improvements (#38522)
This commit is contained in:
parent
15db2225da
commit
991bf126d4
@ -5,8 +5,6 @@ from homeassistant import config_entries
|
|||||||
|
|
||||||
from .typing import HomeAssistantType
|
from .typing import HomeAssistantType
|
||||||
|
|
||||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
||||||
|
|
||||||
DiscoveryFunctionType = Callable[[], Union[Awaitable[bool], bool]]
|
DiscoveryFunctionType = Callable[[], Union[Awaitable[bool], bool]]
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ class EntityComponent:
|
|||||||
scan_interval=getattr(platform, "SCAN_INTERVAL", None),
|
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:
|
async def async_unload_entry(self, config_entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
|
@ -4,10 +4,17 @@ from contextvars import ContextVar
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
from types import ModuleType
|
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.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.exceptions import HomeAssistantError, PlatformNotReady
|
||||||
from homeassistant.helpers import config_validation as cv, service
|
from homeassistant.helpers import config_validation as cv, service
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
@ -19,7 +26,7 @@ from .event import async_call_later, async_track_time_interval
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .entity import Entity
|
from .entity import Entity
|
||||||
|
|
||||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
# mypy: allow-untyped-defs
|
||||||
|
|
||||||
SLOW_SETUP_WARNING = 10
|
SLOW_SETUP_WARNING = 10
|
||||||
SLOW_SETUP_MAX_WAIT = 60
|
SLOW_SETUP_MAX_WAIT = 60
|
||||||
@ -53,7 +60,7 @@ class EntityPlatform:
|
|||||||
self.platform = platform
|
self.platform = platform
|
||||||
self.scan_interval = scan_interval
|
self.scan_interval = scan_interval
|
||||||
self.entity_namespace = entity_namespace
|
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.entities: Dict[str, Entity] = {} # pylint: disable=used-before-assignment
|
||||||
self._tasks: List[asyncio.Future] = []
|
self._tasks: List[asyncio.Future] = []
|
||||||
# Method to cancel the state change listener
|
# Method to cancel the state change listener
|
||||||
@ -119,10 +126,10 @@ class EntityPlatform:
|
|||||||
return
|
return
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_create_setup_task():
|
def async_create_setup_task() -> Coroutine:
|
||||||
"""Get task to set up platform."""
|
"""Get task to set up platform."""
|
||||||
if getattr(platform, "async_setup_platform", None):
|
if getattr(platform, "async_setup_platform", None):
|
||||||
return platform.async_setup_platform(
|
return platform.async_setup_platform( # type: ignore
|
||||||
hass,
|
hass,
|
||||||
platform_config,
|
platform_config,
|
||||||
self._async_schedule_add_entities,
|
self._async_schedule_add_entities,
|
||||||
@ -133,7 +140,7 @@ class EntityPlatform:
|
|||||||
# we don't want to track this task in case it blocks startup.
|
# we don't want to track this task in case it blocks startup.
|
||||||
return hass.loop.run_in_executor(
|
return hass.loop.run_in_executor(
|
||||||
None,
|
None,
|
||||||
platform.setup_platform,
|
platform.setup_platform, # type: ignore
|
||||||
hass,
|
hass,
|
||||||
platform_config,
|
platform_config,
|
||||||
self._schedule_add_entities,
|
self._schedule_add_entities,
|
||||||
@ -142,7 +149,7 @@ class EntityPlatform:
|
|||||||
|
|
||||||
await self._async_setup_platform(async_create_setup_task)
|
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."""
|
"""Set up the platform from a config entry."""
|
||||||
# Store it so that we can save config entry ID in entity registry
|
# Store it so that we can save config entry ID in entity registry
|
||||||
self.config_entry = config_entry
|
self.config_entry = config_entry
|
||||||
@ -151,13 +158,15 @@ class EntityPlatform:
|
|||||||
@callback
|
@callback
|
||||||
def async_create_setup_task():
|
def async_create_setup_task():
|
||||||
"""Get task to set up platform."""
|
"""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
|
self.hass, config_entry, self._async_schedule_add_entities
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self._async_setup_platform(async_create_setup_task)
|
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.
|
"""Set up a platform via config file or config entry.
|
||||||
|
|
||||||
async_create_setup_task creates a coroutine that sets up platform.
|
async_create_setup_task creates a coroutine that sets up platform.
|
||||||
@ -340,7 +349,7 @@ class EntityPlatform:
|
|||||||
return
|
return
|
||||||
|
|
||||||
requested_entity_id = None
|
requested_entity_id = None
|
||||||
suggested_object_id = None
|
suggested_object_id: Optional[str] = None
|
||||||
|
|
||||||
# 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:
|
||||||
@ -354,7 +363,7 @@ class EntityPlatform:
|
|||||||
suggested_object_id = f"{self.entity_namespace} {suggested_object_id}"
|
suggested_object_id = f"{self.entity_namespace} {suggested_object_id}"
|
||||||
|
|
||||||
if self.config_entry is not None:
|
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:
|
else:
|
||||||
config_entry_id = None
|
config_entry_id = None
|
||||||
|
|
||||||
@ -512,7 +521,9 @@ class EntityPlatform:
|
|||||||
self._async_unsub_polling()
|
self._async_unsub_polling()
|
||||||
self._async_unsub_polling = None
|
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.
|
"""Extract all known and available entities from a service call.
|
||||||
|
|
||||||
Will return an empty list if entities specified but unknown.
|
Will return an empty list if entities specified but unknown.
|
||||||
@ -535,9 +546,9 @@ class EntityPlatform:
|
|||||||
if isinstance(schema, dict):
|
if isinstance(schema, dict):
|
||||||
schema = cv.make_entity_service_schema(schema)
|
schema = cv.make_entity_service_schema(schema)
|
||||||
|
|
||||||
async def handle_service(call):
|
async def handle_service(call: ServiceCall) -> None:
|
||||||
"""Handle the service."""
|
"""Handle the service."""
|
||||||
await service.entity_service_call(
|
await service.entity_service_call( # type: ignore
|
||||||
self.hass,
|
self.hass,
|
||||||
[
|
[
|
||||||
plf
|
plf
|
||||||
|
Loading…
x
Reference in New Issue
Block a user