Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for MySensors switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import STATE_OFF, STATE_ON, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.dispatcher import async_dispatcher_connect
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import setup_mysensors_platform
15 from .const import MYSENSORS_DISCOVERY, DiscoveryInfo, SensorType
16 from .entity import MySensorsChildEntity
17 from .helpers import on_unload
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up this platform for a specific ConfigEntry(==Gateway)."""
26  device_class_map: dict[SensorType, type[MySensorsSwitch]] = {
27  "S_DOOR": MySensorsSwitch,
28  "S_MOTION": MySensorsSwitch,
29  "S_SMOKE": MySensorsSwitch,
30  "S_LIGHT": MySensorsSwitch,
31  "S_LOCK": MySensorsSwitch,
32  "S_BINARY": MySensorsSwitch,
33  "S_SPRINKLER": MySensorsSwitch,
34  "S_WATER_LEAK": MySensorsSwitch,
35  "S_SOUND": MySensorsSwitch,
36  "S_VIBRATION": MySensorsSwitch,
37  "S_MOISTURE": MySensorsSwitch,
38  "S_WATER_QUALITY": MySensorsSwitch,
39  }
40 
41  async def async_discover(discovery_info: DiscoveryInfo) -> None:
42  """Discover and add a MySensors switch."""
44  hass,
45  Platform.SWITCH,
46  discovery_info,
47  device_class_map,
48  async_add_entities=async_add_entities,
49  )
50 
51  on_unload(
52  hass,
53  config_entry.entry_id,
55  hass,
56  MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.SWITCH),
57  async_discover,
58  ),
59  )
60 
61 
63  """Representation of the value of a MySensors Switch child node."""
64 
65  @property
66  def is_on(self) -> bool:
67  """Return True if switch is on."""
68  return self._values.get(self.value_type) == STATE_ON
69 
70  async def async_turn_on(self, **kwargs: Any) -> None:
71  """Turn the switch on."""
72  self.gateway.set_child_value(
73  self.node_id, self.child_id, self.value_type, 1, ack=1
74  )
75  if self.assumed_stateassumed_state:
76  # Optimistically assume that switch has changed state
77  self._values[self.value_type] = STATE_ON
78  self.async_write_ha_stateasync_write_ha_state()
79 
80  async def async_turn_off(self, **kwargs: Any) -> None:
81  """Turn the switch off."""
82  self.gateway.set_child_value(
83  self.node_id, self.child_id, self.value_type, 0, ack=1
84  )
85  if self.assumed_stateassumed_state:
86  # Optimistically assume that switch has changed state
87  self._values[self.value_type] = STATE_OFF
88  self.async_write_ha_stateasync_write_ha_state()
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None on_unload(HomeAssistant hass, GatewayId gateway_id, Callable fnct)
Definition: helpers.py:45
None async_discover(DiscoveryInfo discovery_info)
Definition: sensor.py:217
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:24
list[MySensorsChildEntity]|None setup_mysensors_platform(HomeAssistant hass, Platform domain, DiscoveryInfo discovery_info, type[MySensorsChildEntity]|Mapping[SensorType, type[MySensorsChildEntity]] device_class,(tuple|None) device_args=None, Callable|None async_add_entities=None)
Definition: __init__.py:112
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103