Add TagProtocol for type checking (#81086)
* Add TagProtocol for type checking * Adjust type hints
This commit is contained in:
parent
e9117cd1db
commit
1826795d37
3 changed files with 25 additions and 5 deletions
|
@ -5,7 +5,7 @@ from collections.abc import Callable
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
from typing import Any, Generic, NamedTuple, TypeVar, cast, overload
|
from typing import TYPE_CHECKING, Any, Generic, NamedTuple, TypeVar, cast, overload
|
||||||
|
|
||||||
from aioesphomeapi import (
|
from aioesphomeapi import (
|
||||||
APIClient,
|
APIClient,
|
||||||
|
@ -55,6 +55,9 @@ from .domain_data import DOMAIN, DomainData
|
||||||
# Import config flow so that it's added to the registry
|
# Import config flow so that it's added to the registry
|
||||||
from .entry_data import RuntimeEntryData
|
from .entry_data import RuntimeEntryData
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from homeassistant.components.tag import TagProtocol
|
||||||
|
|
||||||
CONF_NOISE_PSK = "noise_psk"
|
CONF_NOISE_PSK = "noise_psk"
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_R = TypeVar("_R")
|
_R = TypeVar("_R")
|
||||||
|
@ -133,7 +136,7 @@ async def async_setup_entry( # noqa: C901
|
||||||
if service_name == "tag_scanned" and device_id is not None:
|
if service_name == "tag_scanned" and device_id is not None:
|
||||||
# Importing tag via hass.components in case it is overridden
|
# Importing tag via hass.components in case it is overridden
|
||||||
# in a custom_components (custom_components.tag)
|
# in a custom_components (custom_components.tag)
|
||||||
tag = hass.components.tag
|
tag: TagProtocol = hass.components.tag
|
||||||
tag_id = service_data["tag_id"]
|
tag_id = service_data["tag_id"]
|
||||||
hass.async_create_task(tag.async_scan_tag(tag_id, device_id))
|
hass.async_create_task(tag.async_scan_tag(tag_id, device_id))
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Protocol
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
@ -106,9 +107,21 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class TagProtocol(Protocol):
|
||||||
|
"""Protocol for type checking."""
|
||||||
|
|
||||||
|
async def async_scan_tag(
|
||||||
|
self, tag_id: str, device_id: str | None, context: Context | None = None
|
||||||
|
) -> None:
|
||||||
|
"""Handle when a tag is scanned."""
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
async def async_scan_tag(
|
async def async_scan_tag(
|
||||||
hass: HomeAssistant, tag_id: str, device_id: str, context: Context | None = None
|
hass: HomeAssistant,
|
||||||
|
tag_id: str,
|
||||||
|
device_id: str | None,
|
||||||
|
context: Context | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Handle when a tag is scanned."""
|
"""Handle when a tag is scanned."""
|
||||||
if DOMAIN not in hass.config.components:
|
if DOMAIN not in hass.config.components:
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
"""Support for tag triggers."""
|
"""Support for tag triggers."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import CONF_PLATFORM
|
from homeassistant.const import CONF_PLATFORM
|
||||||
|
@ -26,8 +28,10 @@ async def async_attach_trigger(
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Listen for tag_scanned events based on configuration."""
|
"""Listen for tag_scanned events based on configuration."""
|
||||||
trigger_data = trigger_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
tag_ids = set(config[TAG_ID])
|
tag_ids: set[str] = set(config[TAG_ID])
|
||||||
device_ids = set(config[DEVICE_ID]) if DEVICE_ID in config else None
|
device_ids: set[str] | None = (
|
||||||
|
set(config[DEVICE_ID]) if DEVICE_ID in config else None
|
||||||
|
)
|
||||||
|
|
||||||
job = HassJob(action)
|
job = HassJob(action)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue