Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch implementation for Wireless Sensor Tags (wirelesstag.net)."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
11  SwitchEntity,
12  SwitchEntityDescription,
13 )
14 from homeassistant.const import CONF_MONITORED_CONDITIONS, Platform
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 from .const import DOMAIN
21 from .entity import WirelessTagBaseSensor
22 from .util import async_migrate_unique_id
23 
24 SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
26  key="temperature",
27  name="Arm Temperature",
28  ),
30  key="humidity",
31  name="Arm Humidity",
32  ),
34  key="motion",
35  name="Arm Motion",
36  ),
38  key="light",
39  name="Arm Light",
40  ),
42  key="moisture",
43  name="Arm Moisture",
44  ),
45 )
46 
47 SWITCH_KEYS: list[str] = [desc.key for desc in SWITCH_TYPES]
48 
49 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
50  {
51  vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
52  cv.ensure_list, [vol.In(SWITCH_KEYS)]
53  )
54  }
55 )
56 
57 
59  hass: HomeAssistant,
60  config: ConfigType,
61  async_add_entities: AddEntitiesCallback,
62  discovery_info: DiscoveryInfoType | None = None,
63 ) -> None:
64  """Set up switches for a Wireless Sensor Tags."""
65  platform = hass.data[DOMAIN]
66 
67  tags = platform.load_tags()
68  monitored_conditions = config[CONF_MONITORED_CONDITIONS]
69  entities = []
70  for tag in tags.values():
71  for description in SWITCH_TYPES:
72  if (
73  description.key in monitored_conditions
74  and description.key in tag.allowed_monitoring_types
75  ):
76  async_migrate_unique_id(hass, tag, Platform.SWITCH, description.key)
77  entities.append(WirelessTagSwitch(platform, tag, description))
78 
79  async_add_entities(entities, True)
80 
81 
83  """A switch implementation for Wireless Sensor Tags."""
84 
85  def __init__(self, api, tag, description: SwitchEntityDescription) -> None:
86  """Initialize a switch for Wireless Sensor Tag."""
87  super().__init__(api, tag)
88  self.entity_descriptionentity_description = description
89  self._name_name_name = f"{self._tag.name} {description.name}"
90  self._attr_unique_id_attr_unique_id = f"{self._uuid}_{description.key}"
91 
92  def turn_on(self, **kwargs: Any) -> None:
93  """Turn on the switch."""
94  self._api_api.arm(self)
95 
96  def turn_off(self, **kwargs: Any) -> None:
97  """Turn on the switch."""
98  self._api_api.disarm(self)
99 
100  @property
101  def is_on(self) -> bool:
102  """Return True if entity is on."""
103  return self._state_state
104 
106  """Provide formatted value."""
107  return self.principal_valueprincipal_valueprincipal_value
108 
109  @property
110  def principal_value(self):
111  """Provide actual value of switch."""
112  attr_name = f"is_{self.entity_description.key}_sensor_armed"
113  return getattr(self._tag_tag, attr_name, False)
None __init__(self, api, tag, SwitchEntityDescription description)
Definition: switch.py:85
None async_migrate_unique_id(HomeAssistant hass, ConfigEntry config_entry, Appliance device)
Definition: __init__.py:84
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:63