light.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. for d in config.dps():
  22. if d.name == "switch":
  23. self._switch_dps = d
  24. else:
  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. if self.is_on:
  50. return "mdi:led-on"
  51. else:
  52. return "mdi:led-off"
  53. @property
  54. def is_on(self):
  55. """Return the current state."""
  56. return self._switch_dps.get_value(self._device)
  57. @property
  58. def device_state_attributes(self):
  59. """Get additional attributes that the integration itself does not support."""
  60. attr = {}
  61. for a in self._attr_dps:
  62. attr[a.name] = a.get_value(self._device)
  63. return attr
  64. async def async_turn_on(self):
  65. await self._switch_dps.async_set_value(self._device, True)
  66. async def async_turn_off(self):
  67. await self._switch_dps.async_set_value(self._device, False)
  68. async def async_toggle(self):
  69. dps_display_on = self.is_on
  70. await (self.async_turn_on() if not dps_display_on else self.async_turn_off())
  71. async def async_update(self):
  72. await self._device.async_refresh()