siren.py 3.7 KB

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