Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switches on Zigbee Home Automation networks."""
2 
3 from __future__ import annotations
4 
5 import functools
6 import logging
7 from typing import Any
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .entity import ZHAEntity
17 from .helpers import (
18  SIGNAL_ADD_ENTITIES,
19  async_add_entities as zha_async_add_entities,
20  convert_zha_error_to_ha_error,
21  get_zha_data,
22 )
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
28  hass: HomeAssistant,
29  config_entry: ConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Set up the Zigbee Home Automation switch from config entry."""
33  zha_data = get_zha_data(hass)
34  entities_to_create = zha_data.platforms[Platform.SWITCH]
35 
37  hass,
38  SIGNAL_ADD_ENTITIES,
39  functools.partial(
40  zha_async_add_entities, async_add_entities, Switch, entities_to_create
41  ),
42  )
43  config_entry.async_on_unload(unsub)
44 
45 
47  """ZHA switch."""
48 
49  @property
50  def is_on(self) -> bool:
51  """Return if the switch is on based on the statemachine."""
52  return self.entity_data.entity.is_on
53 
54  @convert_zha_error_to_ha_error
55  async def async_turn_on(self, **kwargs: Any) -> None:
56  """Turn the entity on."""
57  await self.entity_data.entity.async_turn_on()
58  self.async_write_ha_stateasync_write_ha_state()
59 
60  @convert_zha_error_to_ha_error
61  async def async_turn_off(self, **kwargs: Any) -> None:
62  """Turn the entity off."""
63  await self.entity_data.entity.async_turn_off()
64  self.async_write_ha_stateasync_write_ha_state()
None async_turn_off(self, **Any kwargs)
Definition: switch.py:61
None async_turn_on(self, **Any kwargs)
Definition: switch.py:55
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:31
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103