Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Tasmota switches."""
2 
3 from typing import Any
4 
5 from hatasmota import relay as tasmota_relay
6 from hatasmota.entity import TasmotaEntity as HATasmotaEntity
7 from hatasmota.models import DiscoveryHashType
8 
9 from homeassistant.components import switch
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 DATA_REMOVE_DISCOVER_COMPONENT
17 from .discovery import TASMOTA_DISCOVERY_ENTITY_NEW
18 from .entity import TasmotaAvailability, TasmotaDiscoveryUpdate, TasmotaOnOffEntity
19 
20 
22  hass: HomeAssistant,
23  config_entry: ConfigEntry,
24  async_add_entities: AddEntitiesCallback,
25 ) -> None:
26  """Set up Tasmota switch dynamically through discovery."""
27 
28  @callback
29  def async_discover(
30  tasmota_entity: HATasmotaEntity, discovery_hash: DiscoveryHashType
31  ) -> None:
32  """Discover and add a Tasmota switch."""
34  [
36  tasmota_entity=tasmota_entity, discovery_hash=discovery_hash
37  )
38  ]
39  )
40 
41  hass.data[DATA_REMOVE_DISCOVER_COMPONENT.format(switch.DOMAIN)] = (
43  hass,
44  TASMOTA_DISCOVERY_ENTITY_NEW.format(switch.DOMAIN),
45  async_discover,
46  )
47  )
48 
49 
51  TasmotaAvailability,
52  TasmotaDiscoveryUpdate,
53  TasmotaOnOffEntity,
54  SwitchEntity,
55 ):
56  """Representation of a Tasmota switch."""
57 
58  _tasmota_entity: tasmota_relay.TasmotaRelay
59 
60  async def async_turn_on(self, **kwargs: Any) -> None:
61  """Turn the device on."""
62  await self._tasmota_entity_tasmota_entity.set_state(True)
63 
64  async def async_turn_off(self, **kwargs: Any) -> None:
65  """Turn the device off."""
66  await self._tasmota_entity_tasmota_entity.set_state(False)
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
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:25
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103