Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Netatmo/BTicino/Legrande switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pyatmo import modules as NaModules
9 
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import CONF_URL_CONTROL, NETATMO_CREATE_SWITCH
17 from .data_handler import HOME, SIGNAL_NAME, NetatmoDevice
18 from .entity import NetatmoModuleEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
24  hass: HomeAssistant,
25  entry: ConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the Netatmo switch platform."""
29 
30  @callback
31  def _create_entity(netatmo_device: NetatmoDevice) -> None:
32  entity = NetatmoSwitch(netatmo_device)
33  _LOGGER.debug("Adding switch %s", entity)
34  async_add_entities([entity])
35 
36  entry.async_on_unload(
37  async_dispatcher_connect(hass, NETATMO_CREATE_SWITCH, _create_entity)
38  )
39 
40 
42  """Representation of a Netatmo switch device."""
43 
44  _attr_name = None
45  _attr_configuration_url = CONF_URL_CONTROL
46  device: NaModules.Switch
47 
48  def __init__(
49  self,
50  netatmo_device: NetatmoDevice,
51  ) -> None:
52  """Initialize the Netatmo device."""
53  super().__init__(netatmo_device)
54  self._signal_name_signal_name = f"{HOME}-{self.home.entity_id}"
55  self._publishers.extend(
56  [
57  {
58  "name": HOME,
59  "home_id": self.homehome.entity_id,
60  SIGNAL_NAME: self._signal_name_signal_name,
61  },
62  ]
63  )
64  self._attr_unique_id_attr_unique_id = f"{self.device.entity_id}-{self.device_type}"
65  self._attr_is_on_attr_is_on = self.devicedevice.on
66 
67  @callback
68  def async_update_callback(self) -> None:
69  """Update the entity's state."""
70  self._attr_is_on_attr_is_on = self.devicedevice.on
71 
72  async def async_turn_on(self, **kwargs: Any) -> None:
73  """Turn the zone on."""
74  await self.devicedevice.async_on()
75  self._attr_is_on_attr_is_on = True
76  self.async_write_ha_stateasync_write_ha_state()
77 
78  async def async_turn_off(self, **kwargs: Any) -> None:
79  """Turn the zone off."""
80  await self.devicedevice.async_off()
81  self._attr_is_on_attr_is_on = False
82  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, NetatmoDevice netatmo_device)
Definition: switch.py:51
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:27
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103