mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 05:06:13 +01:00
Use shorthand attributes in wirelesstag (#161214)
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
"""Support for Wireless Sensor Tags."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from requests.exceptions import ConnectTimeout, HTTPError
|
||||
import voluptuous as vol
|
||||
from wirelesstagpy import SensorTag, WirelessTags
|
||||
from wirelesstagpy.binaryevent import BinaryEvent
|
||||
from wirelesstagpy.exceptions import WirelessTagsException
|
||||
|
||||
from homeassistant.components import persistent_notification
|
||||
@@ -21,6 +25,9 @@ from .const import (
|
||||
WIRELESSTAG_DATA,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .switch import WirelessTagSwitch
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
NOTIFICATION_ID = "wirelesstag_notification"
|
||||
@@ -56,22 +63,24 @@ class WirelessTagPlatform:
|
||||
self.tags = self.api.load_tags()
|
||||
return self.tags
|
||||
|
||||
def arm(self, switch):
|
||||
def arm(self, switch: WirelessTagSwitch) -> None:
|
||||
"""Arm entity sensor monitoring."""
|
||||
func_name = f"arm_{switch.entity_description.key}"
|
||||
if (arm_func := getattr(self.api, func_name)) is not None:
|
||||
arm_func(switch.tag_id, switch.tag_manager_mac)
|
||||
|
||||
def disarm(self, switch):
|
||||
def disarm(self, switch: WirelessTagSwitch) -> None:
|
||||
"""Disarm entity sensor monitoring."""
|
||||
func_name = f"disarm_{switch.entity_description.key}"
|
||||
if (disarm_func := getattr(self.api, func_name)) is not None:
|
||||
disarm_func(switch.tag_id, switch.tag_manager_mac)
|
||||
|
||||
def start_monitoring(self):
|
||||
def start_monitoring(self) -> None:
|
||||
"""Start monitoring push events."""
|
||||
|
||||
def push_callback(tags_spec, event_spec):
|
||||
def push_callback(
|
||||
tags_spec: dict[str, SensorTag], event_spec: dict[str, list[BinaryEvent]]
|
||||
) -> None:
|
||||
"""Handle push update."""
|
||||
_LOGGER.debug(
|
||||
"Push notification arrived: %s, events: %s", tags_spec, event_spec
|
||||
|
||||
@@ -77,8 +77,8 @@ class WirelessTagBinarySensor(WirelessTagBaseSensor, BinarySensorEntity):
|
||||
"""Initialize a binary sensor for a Wireless Sensor Tags."""
|
||||
super().__init__(api, tag)
|
||||
self._sensor_type = sensor_type
|
||||
self._name = f"{self._tag.name} {self.event.human_readable_name}"
|
||||
self._attr_device_class = SENSOR_TYPES[sensor_type]
|
||||
self._attr_name = f"{self._tag.name} {self.event.human_readable_name}"
|
||||
self._attr_unique_id = f"{self._uuid}_{self._sensor_type}"
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
@@ -95,7 +95,7 @@ class WirelessTagBinarySensor(WirelessTagBaseSensor, BinarySensorEntity):
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self._state == STATE_ON
|
||||
|
||||
@@ -117,7 +117,7 @@ class WirelessTagBinarySensor(WirelessTagBaseSensor, BinarySensorEntity):
|
||||
return self.principal_value
|
||||
|
||||
@callback
|
||||
def _on_binary_event_callback(self, new_tag):
|
||||
def _on_binary_event_callback(self, new_tag: SensorTag) -> None:
|
||||
"""Update state from arrived push notification."""
|
||||
self._tag = new_tag
|
||||
self._state = self.updated_state_value()
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
import logging
|
||||
|
||||
from wirelesstagpy import SensorTag
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_BATTERY_LEVEL,
|
||||
ATTR_VOLTAGE,
|
||||
@@ -11,6 +13,8 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from . import WirelessTagPlatform
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -25,21 +29,16 @@ ATTR_TAG_POWER_CONSUMPTION = "power_consumption"
|
||||
class WirelessTagBaseSensor(Entity):
|
||||
"""Base class for HA implementation for Wireless Sensor Tag."""
|
||||
|
||||
def __init__(self, api, tag):
|
||||
def __init__(self, api: WirelessTagPlatform, tag: SensorTag) -> None:
|
||||
"""Initialize a base sensor for Wireless Sensor Tag platform."""
|
||||
self._api = api
|
||||
self._tag = tag
|
||||
self._uuid = self._tag.uuid
|
||||
self.tag_id = self._tag.tag_id
|
||||
self.tag_manager_mac = self._tag.tag_manager_mac
|
||||
self._name = self._tag.name
|
||||
self._attr_name = self._tag.name
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def principal_value(self):
|
||||
"""Return base value.
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
from wirelesstagpy import SensorTag
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
||||
@@ -20,6 +21,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import WirelessTagPlatform
|
||||
from .const import DOMAIN, SIGNAL_TAG_UPDATE, WIRELESSTAG_DATA
|
||||
from .entity import WirelessTagBaseSensor
|
||||
from .util import async_migrate_unique_id
|
||||
@@ -97,13 +99,18 @@ class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity):
|
||||
|
||||
entity_description: SensorEntityDescription
|
||||
|
||||
def __init__(self, api, tag, description):
|
||||
def __init__(
|
||||
self,
|
||||
api: WirelessTagPlatform,
|
||||
tag: SensorTag,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a WirelessTag sensor."""
|
||||
super().__init__(api, tag)
|
||||
|
||||
self._sensor_type = description.key
|
||||
self.entity_description = description
|
||||
self._name = self._tag.name
|
||||
self._attr_name = self._tag.name
|
||||
self._attr_unique_id = f"{self._uuid}_{self._sensor_type}"
|
||||
|
||||
# I want to see entity_id as:
|
||||
@@ -148,7 +155,7 @@ class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity):
|
||||
return self._tag.sensor[self._sensor_type]
|
||||
|
||||
@callback
|
||||
def _update_tag_info_callback(self, new_tag):
|
||||
def _update_tag_info_callback(self, new_tag: SensorTag) -> None:
|
||||
"""Handle push notification sent by tag manager."""
|
||||
_LOGGER.debug("Entity to update state: %s with new tag: %s", self, new_tag)
|
||||
self._tag = new_tag
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
from wirelesstagpy import SensorTag
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
|
||||
@@ -17,6 +18,7 @@ from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import WirelessTagPlatform
|
||||
from .const import WIRELESSTAG_DATA
|
||||
from .entity import WirelessTagBaseSensor
|
||||
from .util import async_migrate_unique_id
|
||||
@@ -82,11 +84,16 @@ async def async_setup_platform(
|
||||
class WirelessTagSwitch(WirelessTagBaseSensor, SwitchEntity):
|
||||
"""A switch implementation for Wireless Sensor Tags."""
|
||||
|
||||
def __init__(self, api, tag, description: SwitchEntityDescription) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
api: WirelessTagPlatform,
|
||||
tag: SensorTag,
|
||||
description: SwitchEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a switch for Wireless Sensor Tag."""
|
||||
super().__init__(api, tag)
|
||||
self.entity_description = description
|
||||
self._name = f"{self._tag.name} {description.name}"
|
||||
self._attr_name = f"{self._tag.name} {description.name}"
|
||||
self._attr_unique_id = f"{self._uuid}_{description.key}"
|
||||
|
||||
def turn_on(self, **kwargs: Any) -> None:
|
||||
@@ -98,7 +105,7 @@ class WirelessTagSwitch(WirelessTagBaseSensor, SwitchEntity):
|
||||
self._api.disarm(self)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return True if entity is on."""
|
||||
return self._state
|
||||
|
||||
|
||||
Reference in New Issue
Block a user