siren.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. Setup for Tuya siren devices
  3. """
  4. from homeassistant.components.siren import (
  5. SirenEntity,
  6. SirenEntityFeature,
  7. )
  8. from .device import TuyaLocalDevice
  9. from .helpers.config import async_tuya_setup_platform
  10. from .helpers.device_config import TuyaEntityConfig
  11. from .helpers.mixin import TuyaLocalEntity
  12. async def async_setup_entry(hass, config_entry, async_add_entities):
  13. config = {**config_entry.data, **config_entry.options}
  14. await async_tuya_setup_platform(
  15. hass,
  16. async_add_entities,
  17. config,
  18. "siren",
  19. TuyaLocalSiren,
  20. )
  21. class TuyaLocalSiren(TuyaLocalEntity, SirenEntity):
  22. """Representation of a Tuya siren"""
  23. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  24. """
  25. Initialize the siren.
  26. Args:
  27. device (TuyaLocalDevice): The device API instance.
  28. config (TuyaEntityConfig): The config for this entity.
  29. """
  30. super().__init__()
  31. dps_map = self._init_begin(device, config)
  32. self._tone_dp = dps_map.get("tone", None)
  33. self._volume_dp = dps_map.get("volume_level", None)
  34. self._duration_dp = dps_map.get("duration", None)
  35. self._init_end(dps_map)
  36. # All control of features is through the turn_on service, so we need to
  37. # support that, even if the siren does not support direct control
  38. support = 0
  39. if self._tone_dp:
  40. support |= (
  41. SirenEntityFeature.TONES
  42. | SirenEntityFeature.TURN_ON
  43. | SirenEntityFeature.TURN_OFF
  44. )
  45. self._attr_available_tones = [
  46. x for x in self._tone_dp.values(device) if x != "off"
  47. ]
  48. self._default_tone = self._tone_dp.default
  49. if self._volume_dp:
  50. support |= SirenEntityFeature.VOLUME_SET
  51. if self._duration_dp:
  52. support |= SirenEntityFeature.DURATION
  53. self._attr_supported_features = support
  54. @property
  55. def is_on(self):
  56. """Return whether the siren is on."""
  57. if self._tone_dp:
  58. return self._tone_dp.get_value(self._device) != "off"
  59. async def async_turn_on(self, **kwargs) -> None:
  60. tone = kwargs.get("tone", None)
  61. duration = kwargs.get("duration", None)
  62. volume = kwargs.get("volume", None)
  63. set_dps = {}
  64. if self._tone_dp:
  65. if tone is None:
  66. tone = self._tone_dp.get_value(self._device)
  67. if tone == "off":
  68. tone = self._default_tone
  69. set_dps = {
  70. **set_dps,
  71. **self._tone_dp.get_values_to_set(self._device, tone),
  72. }
  73. if duration is not None and self._duration_dp:
  74. set_dps = {
  75. **set_dps,
  76. **self._duration_dp.get_values_to_set(self._device, duration),
  77. }
  78. if volume is not None and self._volume_dp:
  79. # Volume is a float, range 0.0-1.0 in Home Assistant
  80. # In tuya it is likely an integer or a fixed list of values.
  81. # For integer, expect scale and step to do the conversion,
  82. # for fixed values, we need to snap to closest value.
  83. if self._volume_dp.values(self._device) is not None:
  84. volume = min(
  85. self._volume_dp.values(self._device),
  86. key=lambda x: abs(x - volume),
  87. )
  88. set_dps = {
  89. **set_dps,
  90. **self._volume_dp.get_values_to_set(self._device, volume),
  91. }
  92. await self._device.async_set_properties(set_dps)
  93. async def async_turn_off(self) -> None:
  94. """Turn off the siren"""
  95. if self._tone_dp:
  96. await self._tone_dp.async_set_value(self._device, "off")