Use EntityDescription - wirelesstag (#55065)

This commit is contained in:
Marc Mueller 2021-08-23 20:21:00 +02:00 committed by GitHub
parent ce5c76869d
commit 45a32362af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,31 +1,47 @@
"""Switch implementation for Wireless Sensor Tags (wirelesstag.net).""" """Switch implementation for Wireless Sensor Tags (wirelesstag.net)."""
from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity from homeassistant.components.switch import (
PLATFORM_SCHEMA,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.const import CONF_MONITORED_CONDITIONS from homeassistant.const import CONF_MONITORED_CONDITIONS
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from . import DOMAIN as WIRELESSTAG_DOMAIN, WirelessTagBaseSensor from . import DOMAIN as WIRELESSTAG_DOMAIN, WirelessTagBaseSensor
ARM_TEMPERATURE = "temperature" SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
ARM_HUMIDITY = "humidity" SwitchEntityDescription(
ARM_MOTION = "motion" key="temperature",
ARM_LIGHT = "light" name="Arm Temperature",
ARM_MOISTURE = "moisture" ),
SwitchEntityDescription(
key="humidity",
name="Arm Humidity",
),
SwitchEntityDescription(
key="motion",
name="Arm Motion",
),
SwitchEntityDescription(
key="light",
name="Arm Light",
),
SwitchEntityDescription(
key="moisture",
name="Arm Moisture",
),
)
# Switch types: Name, tag sensor type SWITCH_KEYS: list[str] = [desc.key for desc in SWITCH_TYPES]
SWITCH_TYPES = {
ARM_TEMPERATURE: ["Arm Temperature", "temperature"],
ARM_HUMIDITY: ["Arm Humidity", "humidity"],
ARM_MOTION: ["Arm Motion", "motion"],
ARM_LIGHT: ["Arm Light", "light"],
ARM_MOISTURE: ["Arm Moisture", "moisture"],
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All( vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
cv.ensure_list, [vol.In(SWITCH_TYPES)] cv.ensure_list, [vol.In(SWITCH_KEYS)]
) )
} }
) )
@ -35,25 +51,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up switches for a Wireless Sensor Tags.""" """Set up switches for a Wireless Sensor Tags."""
platform = hass.data.get(WIRELESSTAG_DOMAIN) platform = hass.data.get(WIRELESSTAG_DOMAIN)
switches = []
tags = platform.load_tags() tags = platform.load_tags()
for switch_type in config.get(CONF_MONITORED_CONDITIONS): monitored_conditions = config[CONF_MONITORED_CONDITIONS]
for tag in tags.values(): entities = [
if switch_type in tag.allowed_monitoring_types: WirelessTagSwitch(platform, tag, description)
switches.append(WirelessTagSwitch(platform, tag, switch_type)) for tag in tags.values()
for description in SWITCH_TYPES
if description.key in monitored_conditions
and description.key in tag.allowed_monitoring_types
]
add_entities(switches, True) add_entities(entities, True)
class WirelessTagSwitch(WirelessTagBaseSensor, SwitchEntity): class WirelessTagSwitch(WirelessTagBaseSensor, SwitchEntity):
"""A switch implementation for Wireless Sensor Tags.""" """A switch implementation for Wireless Sensor Tags."""
def __init__(self, api, tag, switch_type): def __init__(self, api, tag, description: SwitchEntityDescription):
"""Initialize a switch for Wireless Sensor Tag.""" """Initialize a switch for Wireless Sensor Tag."""
super().__init__(api, tag) super().__init__(api, tag)
self._switch_type = switch_type self.entity_description = description
self.sensor_type = SWITCH_TYPES[self._switch_type][1] self._name = f"{self._tag.name} {description.name}"
self._name = f"{self._tag.name} {SWITCH_TYPES[self._switch_type][0]}"
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn on the switch.""" """Turn on the switch."""
@ -75,5 +93,5 @@ class WirelessTagSwitch(WirelessTagBaseSensor, SwitchEntity):
@property @property
def principal_value(self): def principal_value(self):
"""Provide actual value of switch.""" """Provide actual value of switch."""
attr_name = f"is_{self.sensor_type}_sensor_armed" attr_name = f"is_{self.entity_description.key}_sensor_armed"
return getattr(self._tag, attr_name, False) return getattr(self._tag, attr_name, False)