siren.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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._switch_dp = dps_map.get("switch", None)
  36. self._init_end(dps_map)
  37. # All control of features is through the turn_on service, so we need to
  38. # support that, even if the siren does not support direct control
  39. support = 0
  40. if self._tone_dp:
  41. support |= (
  42. SirenEntityFeature.TONES
  43. | SirenEntityFeature.TURN_ON
  44. | SirenEntityFeature.TURN_OFF
  45. )
  46. self._attr_available_tones = [
  47. x for x in self._tone_dp.values(device) if x != "off"
  48. ]
  49. self._default_tone = self._tone_dp.default
  50. if self._volume_dp:
  51. support |= SirenEntityFeature.VOLUME_SET
  52. if self._duration_dp:
  53. support |= SirenEntityFeature.DURATION
  54. self._attr_supported_features = support
  55. @property
  56. def is_on(self):
  57. """Return whether the siren is on."""
  58. if self._switch_dp:
  59. return self._switch_dp.get_value(self._device)
  60. if self._tone_dp:
  61. return self._tone_dp.get_value(self._device) != "off"
  62. async def async_turn_on(self, **kwargs) -> None:
  63. tone = kwargs.get("tone", None)
  64. duration = kwargs.get("duration", None)
  65. volume = kwargs.get("volume", None)
  66. set_dps = {}
  67. if self._tone_dp:
  68. if tone is None and not self._switch_dp:
  69. tone = self._tone_dp.get_value(self._device)
  70. if tone == "off":
  71. tone = self._default_tone
  72. set_dps = {
  73. **set_dps,
  74. **self._tone_dp.get_values_to_set(self._device, tone),
  75. }
  76. if duration is not None and self._duration_dp:
  77. set_dps = {
  78. **set_dps,
  79. **self._duration_dp.get_values_to_set(self._device, duration),
  80. }
  81. if volume is not None and self._volume_dp:
  82. # Volume is a float, range 0.0-1.0 in Home Assistant
  83. # In tuya it is likely an integer or a fixed list of values.
  84. # For integer, expect scale and step to do the conversion,
  85. # for fixed values, we need to snap to closest value.
  86. if self._volume_dp.values(self._device) is not None:
  87. volume = min(
  88. self._volume_dp.values(self._device),
  89. key=lambda x: abs(x - volume),
  90. )
  91. set_dps = {
  92. **set_dps,
  93. **self._volume_dp.get_values_to_set(self._device, volume),
  94. }
  95. if self._switch_dp and not self.is_on:
  96. set_dps = {
  97. **set_dps,
  98. **self._switch_dp.get_values_to_set(self._device, True),
  99. }
  100. await self._device.async_set_properties(set_dps)
  101. async def async_turn_off(self) -> None:
  102. """Turn off the siren"""
  103. if self._switch_dp:
  104. await self._switch_dp.async_set_value(self._device, False)
  105. elif self._tone_dp:
  106. await self._tone_dp.async_set_value(self._device, "off")