alarm_control_panel.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. Setup for different kinds of Tuya alarm control panels.
  3. """
  4. import logging
  5. from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity
  6. from homeassistant.components.alarm_control_panel.const import (
  7. AlarmControlPanelEntityFeature as Feature,
  8. )
  9. from homeassistant.components.alarm_control_panel.const import (
  10. AlarmControlPanelState,
  11. )
  12. from .device import TuyaLocalDevice
  13. from .entity import TuyaLocalEntity
  14. from .helpers.config import async_tuya_setup_platform
  15. from .helpers.device_config import TuyaEntityConfig
  16. _LOGGER = logging.getLogger(__name__)
  17. async def async_setup_entry(hass, config_entry, async_add_entities):
  18. config = {**config_entry.data, **config_entry.options}
  19. await async_tuya_setup_platform(
  20. hass,
  21. async_add_entities,
  22. config,
  23. "alarm_control_panel",
  24. TuyaLocalAlarmControlPanel,
  25. )
  26. class TuyaLocalAlarmControlPanel(TuyaLocalEntity, AlarmControlPanelEntity):
  27. """Representation of a Tuya Alarm Control Panel"""
  28. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  29. """
  30. Initialise the alarm control panel.
  31. Args:
  32. device (TuyaLocalDevice): the device API instance
  33. config (TuyaEntityConfig): the configuration for this entity
  34. """
  35. super().__init__()
  36. dps_map = self._init_begin(device, config)
  37. self._alarm_state_dp = dps_map.get("alarm_state")
  38. self._trigger_dp = dps_map.get("trigger")
  39. self._init_end(dps_map)
  40. if not self._alarm_state_dp:
  41. raise AttributeError(f"{config.config_id} is missing an alarm_state dp")
  42. alarm_states = self._alarm_state_dp.values(device)
  43. if AlarmControlPanelState.ARMED_HOME in alarm_states:
  44. self._attr_supported_features |= Feature.ARM_HOME
  45. if AlarmControlPanelState.ARMED_AWAY in alarm_states:
  46. self._attr_supported_features |= Feature.ARM_AWAY
  47. if AlarmControlPanelState.ARMED_NIGHT in alarm_states:
  48. self._attr_supported_features |= Feature.ARM_NIGHT
  49. if AlarmControlPanelState.ARMED_VACATION in alarm_states:
  50. self._attr_supported_features |= Feature.ARM_VACATION
  51. if AlarmControlPanelState.ARMED_CUSTOM_BYPASS in alarm_states:
  52. self._attr_supported_features |= Feature.ARM_CUSTOM_BYPASS
  53. if self._trigger_dp or AlarmControlPanelState.TRIGGERED in alarm_states:
  54. self._attr_supported_features |= Feature.TRIGGER
  55. # Code support not implemented
  56. self._attr_code_format = None
  57. self._attr_code_arm_required = False
  58. @property
  59. def alarm_state(self):
  60. """Return the current alarm state."""
  61. if self._trigger_dp and self._trigger_dp.get_value(self._device):
  62. return AlarmControlPanelState.TRIGGERED
  63. return AlarmControlPanelState(
  64. self._alarm_state_dp.get_value(self._device),
  65. )
  66. async def _alarm_send_command(self, cmd: str):
  67. if cmd in self._alarm_state_dp.values(self._device):
  68. await self._alarm_state_dp.async_set_value(self._device, cmd)
  69. else:
  70. raise NotImplementedError()
  71. async def async_alarm_disarm(self, code=None):
  72. """Send disarm command"""
  73. await self._alarm_send_command(AlarmControlPanelState.DISARMED)
  74. async def async_alarm_arm_home(self, code=None):
  75. await self._alarm_send_command(AlarmControlPanelState.ARMED_HOME)
  76. async def async_alarm_arm_away(self, code=None):
  77. """Send away command"""
  78. await self._alarm_send_command(AlarmControlPanelState.ARMED_AWAY)
  79. async def async_alarm_arm_night(self, code=None):
  80. """Send away command"""
  81. await self._alarm_send_command(AlarmControlPanelState.ARMED_NIGHT)
  82. async def async_alarm_arm_vacation(self, code=None):
  83. """Send away command"""
  84. await self._alarm_send_command(AlarmControlPanelState.ARMED_VACATION)
  85. async def async_alarm_arm_custom_bypass(self, code=None):
  86. await self._alarm_send_command(AlarmControlPanelState.ARMED_CUSTOM_BYPASS)
  87. async def async_alarm_trigger(self, code=None):
  88. if self._trigger_dp:
  89. await self._trigger_dp.async_set_value(self._device, True)
  90. else:
  91. await self._alarm_send_command(AlarmControlPanelState.TRIGGERED)