Import tag from homeassistant.components (#82161)

This commit is contained in:
epenet 2022-11-16 07:09:46 +01:00 committed by GitHub
parent 52298a251b
commit 00f4933e72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 36 deletions

View file

@ -5,7 +5,7 @@ from collections.abc import Callable
import functools import functools
import logging import logging
import math import math
from typing import TYPE_CHECKING, Any, Generic, NamedTuple, TypeVar, cast, overload from typing import Any, Generic, NamedTuple, TypeVar, cast, overload
from aioesphomeapi import ( from aioesphomeapi import (
APIClient, APIClient,
@ -27,7 +27,7 @@ from aioesphomeapi import (
import voluptuous as vol import voluptuous as vol
from homeassistant import const from homeassistant import const
from homeassistant.components import zeroconf from homeassistant.components import tag, zeroconf
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_ID, ATTR_DEVICE_ID,
@ -55,9 +55,6 @@ 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")
@ -134,11 +131,8 @@ async def async_setup_entry( # noqa: C901
# Call native tag scan # Call native tag scan
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
# in a custom_components (custom_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(hass, tag_id, device_id))
return return
hass.bus.async_fire( hass.bus.async_fire(

View file

@ -7,14 +7,13 @@ from functools import wraps
from http import HTTPStatus from http import HTTPStatus
import logging import logging
import secrets import secrets
from typing import TYPE_CHECKING
from aiohttp.web import HTTPBadRequest, Request, Response, json_response from aiohttp.web import HTTPBadRequest, Request, Response, json_response
from nacl.exceptions import CryptoError from nacl.exceptions import CryptoError
from nacl.secret import SecretBox from nacl.secret import SecretBox
import voluptuous as vol import voluptuous as vol
from homeassistant.components import camera, cloud, notify as hass_notify from homeassistant.components import camera, cloud, notify as hass_notify, tag
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASSES as BINARY_SENSOR_CLASSES, DEVICE_CLASSES as BINARY_SENSOR_CLASSES,
) )
@ -114,10 +113,6 @@ from .helpers import (
webhook_response, webhook_response,
) )
if TYPE_CHECKING:
from homeassistant.components.tag import TagProtocol
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DELAY_SAVE = 10 DELAY_SAVE = 10
@ -688,10 +683,8 @@ async def webhook_get_config(hass, config_entry, data):
@validate_schema({vol.Required("tag_id"): cv.string}) @validate_schema({vol.Required("tag_id"): cv.string})
async def webhook_scan_tag(hass, config_entry, data): async def webhook_scan_tag(hass, config_entry, data):
"""Handle a fire event webhook.""" """Handle a fire event webhook."""
# Importing tag via hass.components in case it is overridden
# in a custom_components (custom_components.tag)
tag: TagProtocol = hass.components.tag
await tag.async_scan_tag( await tag.async_scan_tag(
hass,
data["tag_id"], data["tag_id"],
config_entry.data[ATTR_DEVICE_ID], config_entry.data[ATTR_DEVICE_ID],
registration_context(config_entry.data), registration_context(config_entry.data),

View file

@ -3,10 +3,10 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
import functools import functools
from typing import TYPE_CHECKING
import voluptuous as vol import voluptuous as vol
from homeassistant.components import tag
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICE, CONF_PLATFORM, CONF_VALUE_TEMPLATE from homeassistant.const import CONF_DEVICE, CONF_PLATFORM, CONF_VALUE_TEMPLATE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -28,9 +28,6 @@ from .models import MqttValueTemplate, ReceiveMessage, ReceivePayloadType
from .subscription import EntitySubscription from .subscription import EntitySubscription
from .util import get_mqtt_data, valid_subscribe_topic from .util import get_mqtt_data, valid_subscribe_topic
if TYPE_CHECKING:
from homeassistant.components.tag import TagProtocol
LOG_NAME = "Tag" LOG_NAME = "Tag"
TAG = "tag" TAG = "tag"
@ -139,10 +136,7 @@ class MQTTTagScanner(MqttDiscoveryDeviceUpdate):
if not tag_id: # No output from template, ignore if not tag_id: # No output from template, ignore
return return
# Importing tag via hass.components in case it is overridden await tag.async_scan_tag(self.hass, tag_id, self.device_id)
# in a custom_components (custom_components.tag)
tag: TagProtocol = self.hass.components.tag
await tag.async_scan_tag(tag_id, self.device_id)
self._sub_state = subscription.async_prepare_subscribe_topics( self._sub_state = subscription.async_prepare_subscribe_topics(
self.hass, self.hass,

View file

@ -2,7 +2,6 @@
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
@ -107,15 +106,6 @@ 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, hass: HomeAssistant,