Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for AVM FRITZ!SmartHome switch devices."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.core import HomeAssistant, callback
9 from homeassistant.exceptions import HomeAssistantError
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DOMAIN
13 from .coordinator import FritzboxConfigEntry
14 from .entity import FritzBoxDeviceEntity
15 
16 
18  hass: HomeAssistant,
19  entry: FritzboxConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up the FRITZ!SmartHome switch from ConfigEntry."""
23  coordinator = entry.runtime_data
24 
25  @callback
26  def _add_entities(devices: set[str] | None = None) -> None:
27  """Add devices."""
28  if devices is None:
29  devices = coordinator.new_devices
30  if not devices:
31  return
33  FritzboxSwitch(coordinator, ain)
34  for ain in devices
35  if coordinator.data.devices[ain].has_switch
36  )
37 
38  entry.async_on_unload(coordinator.async_add_listener(_add_entities))
39 
40  _add_entities(set(coordinator.data.devices))
41 
42 
44  """The switch class for FRITZ!SmartHome switches."""
45 
46  @property
47  def is_on(self) -> bool:
48  """Return true if the switch is on."""
49  return self.datadatadatadatadata.switch_state # type: ignore [no-any-return]
50 
51  async def async_turn_on(self, **kwargs: Any) -> None:
52  """Turn the switch on."""
53  self.check_lock_statecheck_lock_state()
54  await self.hasshasshass.async_add_executor_job(self.datadatadatadatadata.set_switch_state_on)
55  await self.coordinator.async_refresh()
56 
57  async def async_turn_off(self, **kwargs: Any) -> None:
58  """Turn the switch off."""
59  self.check_lock_statecheck_lock_state()
60  await self.hasshasshass.async_add_executor_job(self.datadatadatadatadata.set_switch_state_off)
61  await self.coordinator.async_refresh()
62 
63  def check_lock_state(self) -> None:
64  """Raise an Error if manual switching via FRITZ!Box user interface is disabled."""
65  if self.datadatadatadatadata.lock:
66  raise HomeAssistantError(
67  translation_domain=DOMAIN,
68  translation_key="manual_switching_disabled",
69  )
None async_setup_entry(HomeAssistant hass, FritzboxConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:21