light.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. """
  2. Setup for different kinds of Tuya light devices
  3. """
  4. import logging
  5. from struct import pack, unpack
  6. import homeassistant.util.color as color_util
  7. from homeassistant.components.light import (
  8. ATTR_BRIGHTNESS,
  9. ATTR_COLOR_TEMP_KELVIN,
  10. ATTR_EFFECT,
  11. ATTR_HS_COLOR,
  12. ATTR_WHITE,
  13. EFFECT_OFF,
  14. ColorMode,
  15. LightEntity,
  16. LightEntityFeature,
  17. )
  18. from .device import TuyaLocalDevice
  19. from .entity import TuyaLocalEntity
  20. from .helpers.config import async_tuya_setup_platform
  21. from .helpers.device_config import TuyaEntityConfig
  22. _LOGGER = logging.getLogger(__name__)
  23. async def async_setup_entry(hass, config_entry, async_add_entities):
  24. config = {**config_entry.data, **config_entry.options}
  25. await async_tuya_setup_platform(
  26. hass,
  27. async_add_entities,
  28. config,
  29. "light",
  30. TuyaLocalLight,
  31. )
  32. class TuyaLocalLight(TuyaLocalEntity, LightEntity):
  33. """Representation of a Tuya WiFi-connected light."""
  34. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  35. """
  36. Initialize the light.
  37. Args:
  38. device (TuyaLocalDevice): The device API instance.
  39. config (TuyaEntityConfig): The configuration for this entity.
  40. """
  41. super().__init__()
  42. dps_map = self._init_begin(device, config)
  43. self._switch_dps = dps_map.pop("switch", None)
  44. self._brightness_dps = dps_map.pop("brightness", None)
  45. self._color_mode_dps = dps_map.pop("color_mode", None)
  46. self._color_temp_dps = dps_map.pop("color_temp", None)
  47. self._rgbhsv_dps = dps_map.pop("rgbhsv", None)
  48. self._named_color_dps = dps_map.pop("named_color", None)
  49. self._effect_dps = dps_map.pop("effect", None)
  50. self._init_end(dps_map)
  51. # Set min and max color temp
  52. if self._color_temp_dps:
  53. range_set = False
  54. m = self._color_temp_dps._find_map_for_dps(0, self._device)
  55. if m:
  56. tr = m.get("target_range")
  57. if tr:
  58. self._attr_min_color_temp_kelvin = tr.get("min")
  59. self._attr_max_color_temp_kelvin = tr.get("max")
  60. range_set = True
  61. if not range_set:
  62. r = self._color_temp_dps.range(self._device)
  63. if r:
  64. # For lights that use K natively, use range
  65. self._attr_min_color_temp_kelvin = r[0]
  66. self._attr_max_color_temp_kelvin = r[1]
  67. @property
  68. def supported_color_modes(self):
  69. """Return the supported color modes for this light."""
  70. if self._color_mode_dps:
  71. return {
  72. ColorMode(mode)
  73. for mode in self._color_mode_dps.values(self._device)
  74. if mode and hasattr(ColorMode, mode.upper())
  75. }
  76. else:
  77. try:
  78. mode = ColorMode(self.color_mode)
  79. if mode and mode != ColorMode.UNKNOWN:
  80. return {mode}
  81. except ValueError:
  82. _LOGGER.warning(
  83. "%s/%s: Unrecognised color mode %s ignored",
  84. self._config._device.config,
  85. self.name or "light",
  86. self.color_mode,
  87. )
  88. @property
  89. def supported_features(self):
  90. """Return the supported features for this light."""
  91. if self.effect_list:
  92. return LightEntityFeature.EFFECT
  93. else:
  94. return LightEntityFeature(0)
  95. @property
  96. def color_mode(self):
  97. """Return the color mode of the light"""
  98. from_dp = self.raw_color_mode
  99. if from_dp:
  100. return from_dp
  101. if self._rgbhsv_dps:
  102. return ColorMode.HS
  103. elif self._named_color_dps:
  104. return ColorMode.HS
  105. elif self._color_temp_dps:
  106. return ColorMode.COLOR_TEMP
  107. elif self._brightness_dps:
  108. return ColorMode.BRIGHTNESS
  109. elif self._switch_dps:
  110. return ColorMode.ONOFF
  111. else:
  112. return ColorMode.UNKNOWN
  113. @property
  114. def raw_color_mode(self):
  115. """Return the color_mode as set from the dps."""
  116. if self._color_mode_dps:
  117. mode = self._color_mode_dps.get_value(self._device)
  118. if mode and hasattr(ColorMode, mode.upper()):
  119. return ColorMode(mode)
  120. @property
  121. def color_temp_kelvin(self):
  122. """Return the color temperature in kelvin."""
  123. if self._color_temp_dps and self.color_mode != ColorMode.HS:
  124. return self._color_temp_dps.get_value(self._device)
  125. @property
  126. def is_on(self):
  127. """Return the current state."""
  128. if self._switch_dps:
  129. return self._switch_dps.get_value(self._device)
  130. elif self._brightness_dps:
  131. b = self.brightness
  132. return isinstance(b, int) and b > 0
  133. elif self._effect_dps and "off" in self._effect_dps.values(self._device):
  134. return self._effect_dps.get_value(self._device) != "off"
  135. else:
  136. # There shouldn't be lights without control, but if there are,
  137. # assume always on if they are responding
  138. return self.available
  139. def _brightness_control_by_hsv(self, target_mode=None):
  140. """Return whether brightness is controlled by HSV."""
  141. v_available = self._rgbhsv_dps and "v" in self._rgbhsv_dps.format["names"]
  142. b_available = self._brightness_dps is not None
  143. current_raw_mode = target_mode or self.raw_color_mode
  144. current_mode = target_mode or self.color_mode
  145. if current_raw_mode == ColorMode.HS and v_available:
  146. return True
  147. if current_raw_mode is None and current_mode == ColorMode.HS and v_available:
  148. return True
  149. if b_available:
  150. return False
  151. return v_available
  152. @property
  153. def brightness(self):
  154. """Get the current brightness of the light"""
  155. if self._brightness_control_by_hsv():
  156. return self._hsv_brightness
  157. return self._white_brightness
  158. @property
  159. def _white_brightness(self):
  160. if self._brightness_dps:
  161. r = self._brightness_dps.range(self._device)
  162. val = self._brightness_dps.get_value(self._device)
  163. if r and val:
  164. val = color_util.value_to_brightness(r, val)
  165. return val
  166. @property
  167. def _unpacked_rgbhsv(self):
  168. """Get the unpacked rgbhsv data"""
  169. if self._rgbhsv_dps:
  170. color = self._rgbhsv_dps.decoded_value(self._device)
  171. fmt = self._rgbhsv_dps.format
  172. if fmt and color:
  173. vals = unpack(fmt.get("format"), color)
  174. idx = 0
  175. rgbhsv = {}
  176. for v in vals:
  177. # HA range: s = 0-100, rgbv = 0-255, h = 0-360
  178. n = fmt["names"][idx]
  179. r = fmt["ranges"][idx]
  180. mx = r["max"]
  181. scale = 1
  182. if n == "h":
  183. scale = 360 / mx
  184. elif n == "s":
  185. scale = 100 / mx
  186. elif n in ["v", "r", "g", "b"]:
  187. scale = 255 / mx
  188. rgbhsv[n] = round(scale * v)
  189. idx += 1
  190. return rgbhsv
  191. elif self._named_color_dps:
  192. colour = self._named_color_dps.get_value(self._device)
  193. if colour:
  194. rgb = color_util.color_name_to_rgb(colour)
  195. return {"r": rgb[0], "g": rgb[1], "b": rgb[2]}
  196. @property
  197. def _hsv_brightness(self):
  198. """Get the colour mode brightness from the light"""
  199. rgbhsv = self._unpacked_rgbhsv
  200. if rgbhsv:
  201. return rgbhsv.get("v", self._white_brightness)
  202. return self._white_brightness
  203. @property
  204. def hs_color(self):
  205. """Get the current hs color of the light"""
  206. rgbhsv = self._unpacked_rgbhsv
  207. if rgbhsv:
  208. if "h" in rgbhsv and "s" in rgbhsv:
  209. hs = (rgbhsv["h"], rgbhsv["s"])
  210. else:
  211. r = rgbhsv.get("r")
  212. g = rgbhsv.get("g")
  213. b = rgbhsv.get("b")
  214. hs = color_util.color_RGB_to_hs(r, g, b)
  215. return hs
  216. @property
  217. def effect_list(self):
  218. """Return the list of valid effects for the light"""
  219. if self._effect_dps:
  220. return self._effect_dps.values(self._device)
  221. elif self._color_mode_dps:
  222. effects = [
  223. effect
  224. for effect in self._color_mode_dps.values(self._device)
  225. if effect and not hasattr(ColorMode, effect.upper())
  226. ]
  227. effects.append(EFFECT_OFF)
  228. return effects
  229. @property
  230. def effect(self):
  231. """Return the current effect setting of this light"""
  232. if self._effect_dps:
  233. return self._effect_dps.get_value(self._device)
  234. elif self._color_mode_dps:
  235. mode = self._color_mode_dps.get_value(self._device)
  236. if mode and not hasattr(ColorMode, mode.upper()):
  237. return mode
  238. return EFFECT_OFF
  239. def named_color_from_hsv(self, hs, brightness):
  240. """Get the named color from the rgb value"""
  241. if self._named_color_dps:
  242. palette = self._named_color_dps.values(self._device)
  243. xy = color_util.color_hs_to_xy(*hs)
  244. distance = float("inf")
  245. best_match = None
  246. for entry in palette:
  247. rgb = color_util.color_name_to_rgb(entry)
  248. xy_entry = color_util.color_RGB_to_xy(*rgb)
  249. d = color_util.get_distance_between_two_points(
  250. color_util.XYPoint(*xy),
  251. color_util.XYPoint(*xy_entry),
  252. )
  253. if d < distance:
  254. distance = d
  255. best_match = entry
  256. return best_match
  257. async def async_turn_on(self, **params):
  258. settings = {}
  259. color_mode = None
  260. _LOGGER.debug("Light turn_on: %s", params)
  261. if self._color_mode_dps and ATTR_WHITE in params:
  262. if self.color_mode != ColorMode.WHITE:
  263. color_mode = ColorMode.WHITE
  264. if ATTR_BRIGHTNESS not in params and self._brightness_dps:
  265. bright = params.get(ATTR_WHITE)
  266. r = self._brightness_dps.range(self._device)
  267. if r:
  268. # ensure full range is used
  269. if bright == 1 and r[0] != 0:
  270. bright = r[0]
  271. else:
  272. bright = color_util.brightness_to_value(r, bright)
  273. _LOGGER.info(
  274. "%s setting white brightness to %d", self._config.config_id, bright
  275. )
  276. settings = {
  277. **settings,
  278. **self._brightness_dps.get_values_to_set(
  279. self._device,
  280. bright,
  281. settings,
  282. ),
  283. }
  284. elif self._color_temp_dps and ATTR_COLOR_TEMP_KELVIN in params:
  285. if self.color_mode != ColorMode.COLOR_TEMP:
  286. color_mode = ColorMode.COLOR_TEMP
  287. color_temp = params.get(ATTR_COLOR_TEMP_KELVIN)
  288. # Light groups use the widest range from the lights in the
  289. # group, so we are expected to silently handle out of range values
  290. if color_temp < self.min_color_temp_kelvin:
  291. color_temp = self.min_color_temp_kelvin
  292. if color_temp > self.max_color_temp_kelvin:
  293. color_temp = self.max_color_temp_kelvin
  294. _LOGGER.info(
  295. "%s setting color temp to %d", self._config.config_id, color_temp
  296. )
  297. settings = {
  298. **settings,
  299. **self._color_temp_dps.get_values_to_set(
  300. self._device,
  301. color_temp,
  302. settings,
  303. ),
  304. }
  305. elif self._rgbhsv_dps and (
  306. ATTR_HS_COLOR in params
  307. or (ATTR_BRIGHTNESS in params and self._brightness_control_by_hsv())
  308. ):
  309. if self.color_mode != ColorMode.HS:
  310. color_mode = ColorMode.HS
  311. hs = params.get(ATTR_HS_COLOR, self.hs_color or (0, 0))
  312. brightness = params.get(ATTR_BRIGHTNESS, self.brightness or 255)
  313. fmt = self._rgbhsv_dps.format
  314. if hs and fmt:
  315. rgb = color_util.color_hsv_to_RGB(*hs, brightness / 2.55)
  316. rgbhsv = {
  317. "r": rgb[0],
  318. "g": rgb[1],
  319. "b": rgb[2],
  320. "h": hs[0],
  321. "s": hs[1],
  322. "v": brightness,
  323. }
  324. _LOGGER.debug(
  325. "Setting color as R:%d,G:%d,B:%d,H:%d,S:%d,V:%d",
  326. rgb[0],
  327. rgb[1],
  328. rgb[2],
  329. hs[0],
  330. hs[1],
  331. brightness,
  332. )
  333. current = self._unpacked_rgbhsv
  334. ordered = []
  335. idx = 0
  336. for n in fmt["names"]:
  337. if n in rgbhsv:
  338. r = fmt["ranges"][idx]
  339. scale = 1
  340. if n == "s":
  341. scale = r["max"] / 100
  342. elif n == "h":
  343. scale = r["max"] / 360
  344. else:
  345. scale = r["max"] / 255
  346. val = round(rgbhsv[n] * scale)
  347. if val < r["min"]:
  348. _LOGGER.warning(
  349. "%s/%s: Color data %s=%d constrained to be above %d",
  350. self._config._device.config,
  351. self.name or "light",
  352. n,
  353. val,
  354. r["min"],
  355. )
  356. val = r["min"]
  357. else:
  358. val = current[n]
  359. ordered.append(val)
  360. idx += 1
  361. binary = pack(fmt["format"], *ordered)
  362. encoded = self._rgbhsv_dps.encode_value(binary)
  363. _LOGGER.info("%s setting color to %s", self._config.config_id, encoded)
  364. settings = {
  365. **settings,
  366. **self._rgbhsv_dps.get_values_to_set(
  367. self._device,
  368. encoded,
  369. settings,
  370. ),
  371. }
  372. elif self._named_color_dps and ATTR_HS_COLOR in params:
  373. if self.color_mode != ColorMode.HS:
  374. color_mode = ColorMode.HS
  375. hs = params.get(ATTR_HS_COLOR, self.hs_color or (0, 0))
  376. brightness = params.get(ATTR_BRIGHTNESS, self.brightness or 255)
  377. best_match = self.named_color_from_hsv(hs, brightness)
  378. _LOGGER.debug("Setting color to %s", best_match)
  379. if best_match:
  380. _LOGGER.info(
  381. "%s setting named color to %s", self._config.config_id, best_match
  382. )
  383. settings = {
  384. **settings,
  385. **self._named_color_dps.get_values_to_set(
  386. self._device,
  387. best_match,
  388. settings,
  389. ),
  390. }
  391. if self._color_mode_dps:
  392. if color_mode:
  393. _LOGGER.info(
  394. "%s auto setting color mode to %s",
  395. self._config.config_id,
  396. color_mode,
  397. )
  398. settings = {
  399. **settings,
  400. **self._color_mode_dps.get_values_to_set(
  401. self._device,
  402. color_mode,
  403. settings,
  404. ),
  405. }
  406. elif not self._effect_dps:
  407. effect = params.get(ATTR_EFFECT)
  408. if effect and effect != self.effect:
  409. if effect == EFFECT_OFF:
  410. # Turn off the effect. Ideally this should keep the
  411. # previous mode, but since the mode is shared with
  412. # effect, use the default, or first in the list
  413. effect = (
  414. self._color_mode_dps.default
  415. or self._color_mode_dps.values(self._device)[0]
  416. )
  417. _LOGGER.info(
  418. "%s emulating effect using color mode of %s",
  419. self._config.config_id,
  420. effect,
  421. )
  422. settings = {
  423. **settings,
  424. **self._color_mode_dps.get_values_to_set(
  425. self._device,
  426. effect,
  427. settings,
  428. ),
  429. }
  430. if (
  431. ATTR_BRIGHTNESS in params
  432. and not self._brightness_control_by_hsv(color_mode)
  433. and self._brightness_dps
  434. ):
  435. bright = params.get(ATTR_BRIGHTNESS)
  436. r = self._brightness_dps.range(self._device)
  437. if r:
  438. # ensure full range is used
  439. if bright == 1 and r[0] != 0:
  440. bright = r[0]
  441. else:
  442. bright = color_util.brightness_to_value(r, bright)
  443. _LOGGER.info("%s setting brightness to %d", self._config.config_id, bright)
  444. settings = {
  445. **settings,
  446. **self._brightness_dps.get_values_to_set(
  447. self._device,
  448. bright,
  449. settings,
  450. ),
  451. }
  452. if self._effect_dps:
  453. effect = params.get(ATTR_EFFECT, None)
  454. if effect:
  455. _LOGGER.info("%s setting effect to %s", self._config.config_id, effect)
  456. settings = {
  457. **settings,
  458. **self._effect_dps.get_values_to_set(
  459. self._device,
  460. effect,
  461. settings,
  462. ),
  463. }
  464. if (
  465. self._switch_dps
  466. and not self._switch_dps.readonly
  467. and not self.is_on
  468. and self._switch_dps.id not in settings
  469. ):
  470. _LOGGER.info("%s turning light on", self._config.config_id)
  471. settings = settings | self._switch_dps.get_values_to_set(
  472. self._device, True, settings
  473. )
  474. elif (
  475. self._brightness_dps
  476. and not self.is_on
  477. and self._brightness_dps.id not in settings
  478. ):
  479. bright = 255
  480. r = self._brightness_dps.range(self._device)
  481. if r:
  482. bright = color_util.brightness_to_value(r, bright)
  483. _LOGGER.info(
  484. "%s turning light on to brightness %d",
  485. self._config.config_id,
  486. bright,
  487. )
  488. settings = settings | self._brightness_dps.get_values_to_set(
  489. self._device, bright, settings
  490. )
  491. elif (
  492. self._effect_dps
  493. and not self.is_on
  494. and "off" in self._effect_dps.values(self._device)
  495. and self._effect_dps.id not in settings
  496. ):
  497. # Special case for lights with effect that has off state, but no switch or brightness
  498. on_value = self._effect_dps.default
  499. if on_value is None and "on" in self._effect_dps.values(self._device):
  500. on_value = "on"
  501. if on_value:
  502. _LOGGER.info(
  503. "%s turning light on using %s effect",
  504. self._config.config_id,
  505. on_value,
  506. )
  507. settings = settings | self._effect_dps.get_values_to_set(
  508. self._device, on_value, settings
  509. )
  510. if settings:
  511. await self._device.async_set_properties(settings)
  512. async def async_turn_off(self):
  513. if self._switch_dps and not self._switch_dps.readonly:
  514. _LOGGER.info("%s turning light off", self._config.config_id)
  515. await self._switch_dps.async_set_value(self._device, False)
  516. elif self._brightness_dps:
  517. _LOGGER.info(
  518. "%s turning light off by setting brightness to 0",
  519. self._config.config_id,
  520. )
  521. await self._brightness_dps.async_set_value(self._device, 0)
  522. elif self._effect_dps and "off" in self._effect_dps.values(self._device):
  523. # off by effect
  524. _LOGGER.info("%s turning light off using effect", self._config.config_id)
  525. await self._effect_dps.async_set_value(self._device, "off")
  526. else:
  527. raise NotImplementedError()