mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Use HassKey in wirelesstag (#161211)
This commit is contained in:
@@ -4,7 +4,7 @@ import logging
|
||||
|
||||
from requests.exceptions import ConnectTimeout, HTTPError
|
||||
import voluptuous as vol
|
||||
from wirelesstagpy import WirelessTags
|
||||
from wirelesstagpy import SensorTag, WirelessTags
|
||||
from wirelesstagpy.exceptions import WirelessTagsException
|
||||
|
||||
from homeassistant.components import persistent_notification
|
||||
@@ -14,7 +14,12 @@ from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import dispatcher_send
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE, SIGNAL_TAG_UPDATE
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
SIGNAL_BINARY_EVENT_UPDATE,
|
||||
SIGNAL_TAG_UPDATE,
|
||||
WIRELESSTAG_DATA,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -39,14 +44,14 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
class WirelessTagPlatform:
|
||||
"""Principal object to manage all registered in HA tags."""
|
||||
|
||||
def __init__(self, hass, api):
|
||||
def __init__(self, hass: HomeAssistant, api: WirelessTags) -> None:
|
||||
"""Designated initializer for wirelesstags platform."""
|
||||
self.hass = hass
|
||||
self.api = api
|
||||
self.tags = {}
|
||||
self.tags: dict[str, SensorTag] = {}
|
||||
self._local_base_url = None
|
||||
|
||||
def load_tags(self):
|
||||
def load_tags(self) -> dict[str, SensorTag]:
|
||||
"""Load tags from remote server."""
|
||||
self.tags = self.api.load_tags()
|
||||
return self.tags
|
||||
@@ -104,9 +109,9 @@ class WirelessTagPlatform:
|
||||
|
||||
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the Wireless Sensor Tag component."""
|
||||
conf = config[DOMAIN]
|
||||
username = conf.get(CONF_USERNAME)
|
||||
password = conf.get(CONF_PASSWORD)
|
||||
conf: ConfigType = config[DOMAIN]
|
||||
username: str = conf[CONF_USERNAME]
|
||||
password: str = conf[CONF_PASSWORD]
|
||||
|
||||
try:
|
||||
wirelesstags = WirelessTags(username=username, password=password)
|
||||
@@ -114,7 +119,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
platform = WirelessTagPlatform(hass, wirelesstags)
|
||||
platform.load_tags()
|
||||
platform.start_monitoring()
|
||||
hass.data[DOMAIN] = platform
|
||||
hass.data[WIRELESSTAG_DATA] = platform
|
||||
except (ConnectTimeout, HTTPError, WirelessTagsException) as ex:
|
||||
_LOGGER.error("Unable to connect to wirelesstag.net service: %s", str(ex))
|
||||
persistent_notification.create(
|
||||
|
||||
@@ -16,7 +16,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE
|
||||
from .const import SIGNAL_BINARY_EVENT_UPDATE, WIRELESSTAG_DATA
|
||||
from .entity import WirelessTagBaseSensor
|
||||
from .util import async_migrate_unique_id
|
||||
|
||||
@@ -82,7 +82,7 @@ async def async_setup_platform(
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the platform for a WirelessTags."""
|
||||
platform = hass.data[DOMAIN]
|
||||
platform = hass.data[WIRELESSTAG_DATA]
|
||||
|
||||
sensors = []
|
||||
tags = platform.tags
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
"""Support for Wireless Sensor Tags."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from . import WirelessTagPlatform
|
||||
|
||||
DOMAIN = "wirelesstag"
|
||||
WIRELESSTAG_DATA: HassKey[WirelessTagPlatform] = HassKey(DOMAIN)
|
||||
|
||||
# Template for signal - first parameter is tag_id,
|
||||
# second, tag manager mac address
|
||||
|
||||
@@ -20,7 +20,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DOMAIN, SIGNAL_TAG_UPDATE
|
||||
from .const import DOMAIN, SIGNAL_TAG_UPDATE, WIRELESSTAG_DATA
|
||||
from .entity import WirelessTagBaseSensor
|
||||
from .util import async_migrate_unique_id
|
||||
|
||||
@@ -78,7 +78,7 @@ async def async_setup_platform(
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the sensor platform."""
|
||||
platform = hass.data[DOMAIN]
|
||||
platform = hass.data[WIRELESSTAG_DATA]
|
||||
sensors = []
|
||||
tags = platform.tags
|
||||
for tag in tags.values():
|
||||
|
||||
@@ -17,7 +17,7 @@ from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import WIRELESSTAG_DATA
|
||||
from .entity import WirelessTagBaseSensor
|
||||
from .util import async_migrate_unique_id
|
||||
|
||||
@@ -62,7 +62,7 @@ async def async_setup_platform(
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up switches for a Wireless Sensor Tags."""
|
||||
platform = hass.data[DOMAIN]
|
||||
platform = hass.data[WIRELESSTAG_DATA]
|
||||
|
||||
tags = platform.load_tags()
|
||||
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||
|
||||
Reference in New Issue
Block a user