Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """BleBox switch implementation."""
2 
3 from datetime import timedelta
4 from typing import Any
5 
7 
8 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import BleBoxConfigEntry
13 from .entity import BleBoxEntity
14 
15 SCAN_INTERVAL = timedelta(seconds=5)
16 
17 
19  hass: HomeAssistant,
20  config_entry: BleBoxConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up a BleBox switch entity."""
24  entities = [
25  BleBoxSwitchEntity(feature)
26  for feature in config_entry.runtime_data.features.get("switches", [])
27  ]
28  async_add_entities(entities, True)
29 
30 
31 class BleBoxSwitchEntity(BleBoxEntity[blebox_uniapi.switch.Switch], SwitchEntity):
32  """Representation of a BleBox switch feature."""
33 
34  _attr_device_class = SwitchDeviceClass.SWITCH
35 
36  @property
37  def is_on(self):
38  """Return whether switch is on."""
39  return self._feature.is_on
40 
41  async def async_turn_on(self, **kwargs: Any) -> None:
42  """Turn on the switch."""
43  await self._feature.async_turn_on()
44 
45  async def async_turn_off(self, **kwargs: Any) -> None:
46  """Turn off the switch."""
47  await self._feature.async_turn_off()
None async_setup_entry(HomeAssistant hass, BleBoxConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:22