Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Vera switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import pyvera as veraApi
8 
9 from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .common import ControllerData, get_controller_data
16 from .entity import VeraEntity
17 
18 
20  hass: HomeAssistant,
21  entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up the sensor config entry."""
25  controller_data = get_controller_data(hass, entry)
27  [
28  VeraSwitch(device, controller_data)
29  for device in controller_data.devices[Platform.SWITCH]
30  ],
31  True,
32  )
33 
34 
35 class VeraSwitch(VeraEntity[veraApi.VeraSwitch], SwitchEntity):
36  """Representation of a Vera Switch."""
37 
38  _attr_is_on = False
39 
40  def __init__(
41  self, vera_device: veraApi.VeraSwitch, controller_data: ControllerData
42  ) -> None:
43  """Initialize the Vera device."""
44  VeraEntity.__init__(self, vera_device, controller_data)
45  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.vera_id)
46 
47  def turn_on(self, **kwargs: Any) -> None:
48  """Turn device on."""
49  self.vera_device.switch_on()
50  self._attr_is_on_attr_is_on_attr_is_on = True
51  self.schedule_update_ha_stateschedule_update_ha_state()
52 
53  def turn_off(self, **kwargs: Any) -> None:
54  """Turn device off."""
55  self.vera_device.switch_off()
56  self._attr_is_on_attr_is_on_attr_is_on = False
57  self.schedule_update_ha_stateschedule_update_ha_state()
58 
59  def update(self) -> None:
60  """Update device state."""
61  super().update()
62  self._attr_is_on_attr_is_on_attr_is_on = self.vera_device.is_switched_on()
None __init__(self, veraApi.VeraSwitch vera_device, ControllerData controller_data)
Definition: switch.py:42
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
ControllerData get_controller_data(HomeAssistant hass, ConfigEntry config_entry)
Definition: common.py:40
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23