Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Freedompro light."""
2 
3 from __future__ import annotations
4 
5 import json
6 from typing import Any
7 
8 from pyfreedompro import put_state
9 
11  ATTR_BRIGHTNESS,
12  ATTR_HS_COLOR,
13  ColorMode,
14  LightEntity,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import CONF_API_KEY
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.helpers import aiohttp_client
20 from homeassistant.helpers.device_registry import DeviceInfo
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.update_coordinator import CoordinatorEntity
23 
24 from .const import DOMAIN
25 from .coordinator import FreedomproDataUpdateCoordinator
26 
27 
29  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
30 ) -> None:
31  """Set up Freedompro light."""
32  api_key: str = entry.data[CONF_API_KEY]
33  coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
35  Device(hass, api_key, device, coordinator)
36  for device in coordinator.data
37  if device["type"] == "lightbulb"
38  )
39 
40 
41 class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], LightEntity):
42  """Representation of a Freedompro light."""
43 
44  _attr_has_entity_name = True
45  _attr_name = None
46  _attr_is_on = False
47  _attr_brightness = 0
48 
49  def __init__(
50  self,
51  hass: HomeAssistant,
52  api_key: str,
53  device: dict[str, Any],
54  coordinator: FreedomproDataUpdateCoordinator,
55  ) -> None:
56  """Initialize the Freedompro light."""
57  super().__init__(coordinator)
58  self._session_session = aiohttp_client.async_get_clientsession(hass)
59  self._api_key_api_key_api_key = api_key
60  self._attr_unique_id_attr_unique_id = device["uid"]
61  self._attr_device_info_attr_device_info = DeviceInfo(
62  identifiers={(DOMAIN, device["uid"])},
63  manufacturer="Freedompro",
64  model=device["type"],
65  name=device["name"],
66  )
67  color_mode = ColorMode.ONOFF
68  if "hue" in device["characteristics"]:
69  color_mode = ColorMode.HS
70  elif "brightness" in device["characteristics"]:
71  color_mode = ColorMode.BRIGHTNESS
72  self._attr_color_mode_attr_color_mode = color_mode
73  self._attr_supported_color_modes_attr_supported_color_modes = {color_mode}
74 
75  @callback
76  def _handle_coordinator_update(self) -> None:
77  """Handle updated data from the coordinator."""
78  device = next(
79  (
80  device
81  for device in self.coordinator.data
82  if device["uid"] == self._attr_unique_id_attr_unique_id
83  ),
84  None,
85  )
86  if device is not None and "state" in device:
87  state = device["state"]
88  if "on" in state:
89  self._attr_is_on_attr_is_on_attr_is_on = state["on"]
90  if "brightness" in state:
91  self._attr_brightness_attr_brightness_attr_brightness = round(state["brightness"] / 100 * 255)
92  if "hue" in state and "saturation" in state:
93  self._attr_hs_color_attr_hs_color = (state["hue"], state["saturation"])
95 
96  async def async_added_to_hass(self) -> None:
97  """When entity is added to hass."""
98  await super().async_added_to_hass()
99  self._handle_coordinator_update_handle_coordinator_update()
100 
101  async def async_turn_on(self, **kwargs: Any) -> None:
102  """Async function to set on to light."""
103  payload: dict[str, Any] = {"on": True}
104  if ATTR_BRIGHTNESS in kwargs:
105  payload["brightness"] = round(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
106  if ATTR_HS_COLOR in kwargs:
107  payload["saturation"] = round(kwargs[ATTR_HS_COLOR][1])
108  payload["hue"] = round(kwargs[ATTR_HS_COLOR][0])
109  await put_state(
110  self._session_session,
111  self._api_key_api_key_api_key,
112  self._attr_unique_id_attr_unique_id,
113  json.dumps(payload),
114  )
115  await self.coordinator.async_request_refresh()
116 
117  async def async_turn_off(self, **kwargs: Any) -> None:
118  """Async function to set off to light."""
119  payload = {"on": False}
120  await put_state(
121  self._session_session,
122  self._api_key_api_key_api_key,
123  self._attr_unique_id_attr_unique_id,
124  json.dumps(payload),
125  )
126  await self.coordinator.async_request_refresh()
None async_turn_on(self, **Any kwargs)
Definition: light.py:101
None async_turn_off(self, **Any kwargs)
Definition: light.py:117
None __init__(self, HomeAssistant hass, str api_key, dict[str, Any] device, FreedomproDataUpdateCoordinator coordinator)
Definition: light.py:55
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:30