light.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Platform to control Tuya lights.
  3. Initially based on the secondary panel lighting control on some climate
  4. devices, so only providing simple on/off control.
  5. """
  6. from homeassistant.components.light import LightEntity
  7. from ..device import TuyaLocalDevice
  8. from ..helpers.device_config import TuyaEntityConfig
  9. class TuyaLocalLight(LightEntity):
  10. """Representation of a Tuya WiFi-connected light."""
  11. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  12. """
  13. Initialize the light.
  14. Args:
  15. device (TuyaLocalDevice): The device API instance.
  16. config (TuyaEntityConfig): The configuration for this entity.
  17. """
  18. self._device = device
  19. self._config = config
  20. self._attr_dps = []
  21. dps_map = {c.name: c for c in config.dps()}
  22. self._switch_dps = dps_map.pop("switch")
  23. for d in dps_map.values():
  24. if not d.hidden:
  25. self._attr_dps.append(d)
  26. @property
  27. def should_poll(self):
  28. """Return the polling state."""
  29. return True
  30. @property
  31. def name(self):
  32. """Return the name of the light."""
  33. return self._device.name
  34. @property
  35. def friendly_name(self):
  36. """Return the friendly name for this entity."""
  37. return self._config.name
  38. @property
  39. def unique_id(self):
  40. """Return the unique id for this heater LED display."""
  41. return self._device.unique_id
  42. @property
  43. def device_info(self):
  44. """Return device information about this heater LED display."""
  45. return self._device.device_info
  46. @property
  47. def icon(self):
  48. """Return the icon to use in the frontend for this device."""
  49. icon = self._config.icon(self._device)
  50. if icon:
  51. return icon
  52. else:
  53. return super().icon
  54. @property
  55. def is_on(self):
  56. """Return the current state."""
  57. return self._switch_dps.get_value(self._device)
  58. @property
  59. def device_state_attributes(self):
  60. """Get additional attributes that the integration itself does not support."""
  61. attr = {}
  62. for a in self._attr_dps:
  63. attr[a.name] = a.get_value(self._device)
  64. return attr
  65. async def async_turn_on(self):
  66. await self._switch_dps.async_set_value(self._device, True)
  67. async def async_turn_off(self):
  68. await self._switch_dps.async_set_value(self._device, False)
  69. async def async_toggle(self):
  70. dps_display_on = self.is_on
  71. await (self.async_turn_on() if not dps_display_on else self.async_turn_off())
  72. async def async_update(self):
  73. await self._device.async_refresh()