siren.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 .helpers.config import async_tuya_setup_platform
  12. from .helpers.device_config import TuyaEntityConfig
  13. from .helpers.mixin import TuyaLocalEntity
  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 = 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. self._attr_supported_features = support
  57. @property
  58. def is_on(self):
  59. """Return whether the siren is on."""
  60. if self._switch_dp:
  61. return self._switch_dp.get_value(self._device)
  62. if self._tone_dp:
  63. return self._tone_dp.get_value(self._device) != "off"
  64. async def async_turn_on(self, **kwargs) -> None:
  65. tone = kwargs.get(ATTR_TONE, None)
  66. duration = kwargs.get(ATTR_DURATION, None)
  67. volume = kwargs.get(ATTR_VOLUME_LEVEL, None)
  68. set_dps = {}
  69. if self._tone_dp:
  70. if tone is None and not self._switch_dp:
  71. tone = self._tone_dp.get_value(self._device)
  72. if tone == "off":
  73. tone = self._default_tone
  74. set_dps = {
  75. **set_dps,
  76. **self._tone_dp.get_values_to_set(self._device, tone),
  77. }
  78. if duration is not None and self._duration_dp:
  79. set_dps = {
  80. **set_dps,
  81. **self._duration_dp.get_values_to_set(self._device, duration),
  82. }
  83. if volume is not None and self._volume_dp:
  84. # Volume is a float, range 0.0-1.0 in Home Assistant
  85. # In tuya it is likely an integer or a fixed list of values.
  86. # For integer, expect scale and step to do the conversion,
  87. # for fixed values, we need to snap to closest value.
  88. if self._volume_dp.values(self._device):
  89. volume = min(
  90. self._volume_dp.values(self._device),
  91. key=lambda x: abs(x - volume),
  92. )
  93. set_dps = {
  94. **set_dps,
  95. **self._volume_dp.get_values_to_set(self._device, volume),
  96. }
  97. if self._switch_dp and not self.is_on:
  98. set_dps = {
  99. **set_dps,
  100. **self._switch_dp.get_values_to_set(self._device, True),
  101. }
  102. await self._device.async_set_properties(set_dps)
  103. async def async_turn_off(self) -> None:
  104. """Turn off the siren"""
  105. if self._switch_dp:
  106. await self._switch_dp.async_set_value(self._device, False)
  107. elif self._tone_dp:
  108. await self._tone_dp.async_set_value(self._device, "off")