Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Freedompro switch."""
2 
3 import json
4 from typing import Any
5 
6 from pyfreedompro import put_state
7 
8 from homeassistant.components.switch import SwitchEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_API_KEY
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers import aiohttp_client
13 from homeassistant.helpers.device_registry import DeviceInfo
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.update_coordinator import CoordinatorEntity
16 
17 from .const import DOMAIN
18 from .coordinator import FreedomproDataUpdateCoordinator
19 
20 
22  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
23 ) -> None:
24  """Set up Freedompro switch."""
25  api_key: str = entry.data[CONF_API_KEY]
26  coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
28  Device(hass, api_key, device, coordinator)
29  for device in coordinator.data
30  if device["type"] in ("switch", "outlet")
31  )
32 
33 
34 class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], SwitchEntity):
35  """Representation of a Freedompro switch."""
36 
37  _attr_has_entity_name = True
38  _attr_name = None
39 
40  def __init__(
41  self,
42  hass: HomeAssistant,
43  api_key: str,
44  device: dict[str, Any],
45  coordinator: FreedomproDataUpdateCoordinator,
46  ) -> None:
47  """Initialize the Freedompro switch."""
48  super().__init__(coordinator)
49  self._session_session = aiohttp_client.async_get_clientsession(hass)
50  self._api_key_api_key_api_key = api_key
51  self._attr_unique_id_attr_unique_id = device["uid"]
52  self._attr_device_info_attr_device_info = DeviceInfo(
53  identifiers={
54  (DOMAIN, device["uid"]),
55  },
56  manufacturer="Freedompro",
57  model=device["type"],
58  name=device["name"],
59  )
60  self._attr_is_on_attr_is_on = False
61 
62  @callback
63  def _handle_coordinator_update(self) -> None:
64  """Handle updated data from the coordinator."""
65  device = next(
66  (
67  device
68  for device in self.coordinator.data
69  if device["uid"] == self.unique_idunique_id
70  ),
71  None,
72  )
73  if device is not None and "state" in device:
74  state = device["state"]
75  if "on" in state:
76  self._attr_is_on_attr_is_on = state["on"]
78 
79  async def async_added_to_hass(self) -> None:
80  """When entity is added to hass."""
81  await super().async_added_to_hass()
82  self._handle_coordinator_update_handle_coordinator_update()
83 
84  async def async_turn_on(self, **kwargs: Any) -> None:
85  """Async function to set on to switch."""
86  payload = {"on": True}
87  await put_state(
88  self._session_session,
89  self._api_key_api_key_api_key,
90  self.unique_idunique_id,
91  json.dumps(payload),
92  )
93  await self.coordinator.async_request_refresh()
94 
95  async def async_turn_off(self, **kwargs: Any) -> None:
96  """Async function to set off to switch."""
97  payload = {"on": False}
98  await put_state(
99  self._session_session,
100  self._api_key_api_key_api_key,
101  self.unique_idunique_id,
102  json.dumps(payload),
103  )
104  await self.coordinator.async_request_refresh()
None async_turn_off(self, **Any kwargs)
Definition: switch.py:95
None async_turn_on(self, **Any kwargs)
Definition: switch.py:84
None __init__(self, HomeAssistant hass, str api_key, dict[str, Any] device, FreedomproDataUpdateCoordinator coordinator)
Definition: switch.py:46
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23