siren.py 4.5 KB

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