Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Wyoming switch entities."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING, Any
6 
7 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import STATE_ON, EntityCategory
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers import restore_state
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DOMAIN
15 from .entity import WyomingSatelliteEntity
16 
17 if TYPE_CHECKING:
18  from .models import DomainDataItem
19 
20 
22  hass: HomeAssistant,
23  config_entry: ConfigEntry,
24  async_add_entities: AddEntitiesCallback,
25 ) -> None:
26  """Set up VoIP switch entities."""
27  item: DomainDataItem = hass.data[DOMAIN][config_entry.entry_id]
28 
29  # Setup is only forwarded for satellites
30  assert item.device is not None
31 
33 
34 
36  WyomingSatelliteEntity, restore_state.RestoreEntity, SwitchEntity
37 ):
38  """Entity to represent if satellite is muted."""
39 
40  entity_description = SwitchEntityDescription(
41  key="mute",
42  translation_key="mute",
43  entity_category=EntityCategory.CONFIG,
44  )
45 
46  async def async_added_to_hass(self) -> None:
47  """Call when entity about to be added to hass."""
48  await super().async_added_to_hass()
49 
50  state = await self.async_get_last_state()
51 
52  # Default to off
53  self._attr_is_on_attr_is_on = (state is not None) and (state.state == STATE_ON)
54  self._device_device.set_is_muted(self._attr_is_on_attr_is_on)
55 
56  async def async_turn_on(self, **kwargs: Any) -> None:
57  """Turn on."""
58  self._attr_is_on_attr_is_on = True
59  self.async_write_ha_stateasync_write_ha_state()
60  self._device_device.set_is_muted(True)
61 
62  async def async_turn_off(self, **kwargs: Any) -> None:
63  """Turn off."""
64  self._attr_is_on_attr_is_on = False
65  self.async_write_ha_stateasync_write_ha_state()
66  self._device_device.set_is_muted(False)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:25