Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for the Opple light."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pyoppleio.OppleLightDevice import OppleLightDevice
9 import voluptuous as vol
10 
12  ATTR_BRIGHTNESS,
13  ATTR_COLOR_TEMP,
14  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
15  ColorMode,
16  LightEntity,
17 )
18 from homeassistant.const import CONF_HOST, CONF_NAME
19 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 from homeassistant.util.color import (
24  color_temperature_kelvin_to_mired as kelvin_to_mired,
25  color_temperature_mired_to_kelvin as mired_to_kelvin,
26 )
27 
28 _LOGGER = logging.getLogger(__name__)
29 
30 DEFAULT_NAME = "opple light"
31 
32 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
33  {
34  vol.Required(CONF_HOST): cv.string,
35  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
36  }
37 )
38 
39 
41  hass: HomeAssistant,
42  config: ConfigType,
43  add_entities: AddEntitiesCallback,
44  discovery_info: DiscoveryInfoType | None = None,
45 ) -> None:
46  """Set up the Opple light platform."""
47  name = config[CONF_NAME]
48  host = config[CONF_HOST]
49  entity = OppleLight(name, host)
50 
51  add_entities([entity])
52 
53  _LOGGER.debug("Init light %s %s", host, entity.unique_id)
54 
55 
57  """Opple light device."""
58 
59  _attr_color_mode = ColorMode.COLOR_TEMP
60  _attr_supported_color_modes = {ColorMode.COLOR_TEMP}
61 
62  def __init__(self, name, host):
63  """Initialize an Opple light."""
64 
65  self._device_device = OppleLightDevice(host)
66 
67  self._name_name = name
68  self._is_on_is_on = None
69  self._brightness_brightness = None
70  self._color_temp_color_temp = None
71 
72  @property
73  def available(self) -> bool:
74  """Return True if light is available."""
75  return self._device_device.is_online
76 
77  @property
78  def unique_id(self):
79  """Return unique ID for light."""
80  return self._device_device.mac
81 
82  @property
83  def name(self):
84  """Return the display name of this light."""
85  return self._name_name
86 
87  @property
88  def is_on(self):
89  """Return true if light is on."""
90  return self._is_on_is_on
91 
92  @property
93  def brightness(self):
94  """Return the brightness of the light."""
95  return self._brightness_brightness
96 
97  @property
98  def color_temp(self):
99  """Return the color temperature of this light."""
100  return kelvin_to_mired(self._color_temp_color_temp)
101 
102  @property
103  def min_mireds(self):
104  """Return minimum supported color temperature."""
105  return 175
106 
107  @property
108  def max_mireds(self):
109  """Return maximum supported color temperature."""
110  return 333
111 
112  def turn_on(self, **kwargs: Any) -> None:
113  """Instruct the light to turn on."""
114  _LOGGER.debug("Turn on light %s %s", self._device_device.ip, kwargs)
115  if not self.is_onis_onis_on:
116  self._device_device.power_on = True
117 
118  if ATTR_BRIGHTNESS in kwargs and self.brightnessbrightnessbrightness != kwargs[ATTR_BRIGHTNESS]:
119  self._device_device.brightness = kwargs[ATTR_BRIGHTNESS]
120 
121  if ATTR_COLOR_TEMP in kwargs and self.color_tempcolor_tempcolor_temp != kwargs[ATTR_COLOR_TEMP]:
122  color_temp = mired_to_kelvin(kwargs[ATTR_COLOR_TEMP])
123  self._device_device.color_temperature = color_temp
124 
125  def turn_off(self, **kwargs: Any) -> None:
126  """Instruct the light to turn off."""
127  self._device_device.power_on = False
128  _LOGGER.debug("Turn off light %s", self._device_device.ip)
129 
130  def update(self) -> None:
131  """Synchronize state with light."""
132  prev_available = self.availableavailableavailable
133  self._device_device.update()
134 
135  if (
136  prev_available == self.availableavailableavailable
137  and self._is_on_is_on == self._device_device.power_on
138  and self._brightness_brightness == self._device_device.brightness
139  and self._color_temp_color_temp == self._device_device.color_temperature
140  ):
141  return
142 
143  if not self.availableavailableavailable:
144  _LOGGER.debug("Light %s is offline", self._device_device.ip)
145  return
146 
147  self._is_on_is_on = self._device_device.power_on
148  self._brightness_brightness = self._device_device.brightness
149  self._color_temp_color_temp = self._device_device.color_temperature
150 
151  if not self.is_onis_onis_on:
152  _LOGGER.debug("Update light %s success: power off", self._device_device.ip)
153  else:
154  _LOGGER.debug(
155  "Update light %s success: power on brightness %s color temperature %s",
156  self._device_device.ip,
157  self._brightness_brightness,
158  self._color_temp_color_temp,
159  )
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:45