Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Goal Zero Yeti Switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from .coordinator import GoalZeroConfigEntry
12 from .entity import GoalZeroEntity
13 
14 SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
16  key="v12PortStatus",
17  translation_key="v12_port_status",
18  ),
20  key="usbPortStatus",
21  translation_key="usb_port_status",
22  ),
24  key="acPortStatus",
25  translation_key="ac_port_status",
26  ),
27 )
28 
29 
31  hass: HomeAssistant,
32  entry: GoalZeroConfigEntry,
33  async_add_entities: AddEntitiesCallback,
34 ) -> None:
35  """Set up the Goal Zero Yeti switch."""
37  GoalZeroSwitch(entry.runtime_data, description) for description in SWITCH_TYPES
38  )
39 
40 
42  """Representation of a Goal Zero Yeti switch."""
43 
44  @property
45  def is_on(self) -> bool:
46  """Return state of the switch."""
47  return cast(bool, self._api_api.data[self.entity_descriptionentity_description.key] == 1)
48 
49  async def async_turn_off(self, **kwargs: Any) -> None:
50  """Turn off the switch."""
51  payload = {self.entity_descriptionentity_description.key: 0}
52  await self._api_api.post_state(payload=payload)
53  self.coordinator.async_set_updated_data(None)
54 
55  async def async_turn_on(self, **kwargs: Any) -> None:
56  """Turn on the switch."""
57  payload = {self.entity_descriptionentity_description.key: 1}
58  await self._api_api.post_state(payload=payload)
59  self.coordinator.async_set_updated_data(None)
None async_setup_entry(HomeAssistant hass, GoalZeroConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:34