|
| 1 | +"""Support for Comelit VEDO system.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from aiocomelit.api import ComelitVedoAreaObject |
| 7 | +from aiocomelit.const import ALARM_AREAS, AlarmAreaState |
| 8 | + |
| 9 | +from homeassistant.components.alarm_control_panel import ( |
| 10 | + AlarmControlPanelEntity, |
| 11 | + AlarmControlPanelEntityFeature, |
| 12 | + CodeFormat, |
| 13 | +) |
| 14 | +from homeassistant.config_entries import ConfigEntry |
| 15 | +from homeassistant.const import ( |
| 16 | + STATE_ALARM_ARMED_AWAY, |
| 17 | + STATE_ALARM_ARMED_HOME, |
| 18 | + STATE_ALARM_ARMED_NIGHT, |
| 19 | + STATE_ALARM_ARMING, |
| 20 | + STATE_ALARM_DISARMED, |
| 21 | + STATE_ALARM_DISARMING, |
| 22 | + STATE_ALARM_TRIGGERED, |
| 23 | +) |
| 24 | +from homeassistant.core import HomeAssistant |
| 25 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 26 | +from homeassistant.helpers.typing import StateType |
| 27 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 28 | + |
| 29 | +from .const import DOMAIN |
| 30 | +from .coordinator import ComelitVedoSystem |
| 31 | + |
| 32 | +_LOGGER = logging.getLogger(__name__) |
| 33 | + |
| 34 | +AWAY = "away" |
| 35 | +DISABLE = "disable" |
| 36 | +HOME = "home" |
| 37 | +HOME_P1 = "home_p1" |
| 38 | +HOME_P2 = "home_p2" |
| 39 | +NIGHT = "night" |
| 40 | + |
| 41 | +ALARM_ACTIONS: dict[str, str] = { |
| 42 | + DISABLE: "dis", # Disarm |
| 43 | + HOME: "p1", # Arm P1 |
| 44 | + NIGHT: "p12", # Arm P1+P2 |
| 45 | + AWAY: "tot", # Arm P1+P2 + IR / volumetric |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +ALARM_AREA_ARMED_STATUS: dict[str, int] = { |
| 50 | + HOME_P1: 1, |
| 51 | + HOME_P2: 2, |
| 52 | + NIGHT: 3, |
| 53 | + AWAY: 4, |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +async def async_setup_entry( |
| 58 | + hass: HomeAssistant, |
| 59 | + config_entry: ConfigEntry, |
| 60 | + async_add_entities: AddEntitiesCallback, |
| 61 | +) -> None: |
| 62 | + """Set up the Comelit VEDO system alarm control panel devices.""" |
| 63 | + |
| 64 | + coordinator: ComelitVedoSystem = hass.data[DOMAIN][config_entry.entry_id] |
| 65 | + |
| 66 | + async_add_entities( |
| 67 | + ComelitAlarmEntity(coordinator, device, config_entry.entry_id) |
| 68 | + for device in coordinator.data[ALARM_AREAS].values() |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +class ComelitAlarmEntity(CoordinatorEntity[ComelitVedoSystem], AlarmControlPanelEntity): |
| 73 | + """Representation of a Ness alarm panel.""" |
| 74 | + |
| 75 | + _attr_has_entity_name = True |
| 76 | + _attr_name = None |
| 77 | + _attr_code_format = CodeFormat.NUMBER |
| 78 | + _attr_code_arm_required = False |
| 79 | + _attr_supported_features = ( |
| 80 | + AlarmControlPanelEntityFeature.ARM_AWAY |
| 81 | + | AlarmControlPanelEntityFeature.ARM_HOME |
| 82 | + ) |
| 83 | + |
| 84 | + def __init__( |
| 85 | + self, |
| 86 | + coordinator: ComelitVedoSystem, |
| 87 | + area: ComelitVedoAreaObject, |
| 88 | + config_entry_entry_id: str, |
| 89 | + ) -> None: |
| 90 | + """Initialize the alarm panel.""" |
| 91 | + self._api = coordinator.api |
| 92 | + self._area_index = area.index |
| 93 | + super().__init__(coordinator) |
| 94 | + # Use config_entry.entry_id as base for unique_id |
| 95 | + # because no serial number or mac is available |
| 96 | + self._attr_unique_id = f"{config_entry_entry_id}-{area.index}" |
| 97 | + self._attr_device_info = coordinator.platform_device_info(area, "area") |
| 98 | + if area.p2: |
| 99 | + self._attr_supported_features |= AlarmControlPanelEntityFeature.ARM_NIGHT |
| 100 | + |
| 101 | + @property |
| 102 | + def _area(self) -> ComelitVedoAreaObject: |
| 103 | + """Return area object.""" |
| 104 | + return self.coordinator.data[ALARM_AREAS][self._area_index] |
| 105 | + |
| 106 | + @property |
| 107 | + def available(self) -> bool: |
| 108 | + """Return True if alarm is available.""" |
| 109 | + if self._area.human_status in [AlarmAreaState.ANOMALY, AlarmAreaState.UNKNOWN]: |
| 110 | + return False |
| 111 | + return super().available |
| 112 | + |
| 113 | + @property |
| 114 | + def state(self) -> StateType: |
| 115 | + """Return the state of the alarm.""" |
| 116 | + |
| 117 | + _LOGGER.debug( |
| 118 | + "Area %s status is: %s. Armed is %s", |
| 119 | + self._area.name, |
| 120 | + self._area.human_status, |
| 121 | + self._area.armed, |
| 122 | + ) |
| 123 | + if self._area.human_status == AlarmAreaState.ARMED: |
| 124 | + if self._area.armed == ALARM_AREA_ARMED_STATUS[AWAY]: |
| 125 | + return STATE_ALARM_ARMED_AWAY |
| 126 | + if self._area.armed == ALARM_AREA_ARMED_STATUS[NIGHT]: |
| 127 | + return STATE_ALARM_ARMED_NIGHT |
| 128 | + return STATE_ALARM_ARMED_HOME |
| 129 | + |
| 130 | + { |
| 131 | + AlarmAreaState.DISARMED: STATE_ALARM_DISARMED, |
| 132 | + AlarmAreaState.ENTRY_DELAY: STATE_ALARM_DISARMING, |
| 133 | + AlarmAreaState.EXIT_DELAY: STATE_ALARM_ARMING, |
| 134 | + AlarmAreaState.TRIGGERED: STATE_ALARM_TRIGGERED, |
| 135 | + }.get(self._area.human_status) |
| 136 | + |
| 137 | + return None |
| 138 | + |
| 139 | + async def async_alarm_disarm(self, code: str | None = None) -> None: |
| 140 | + """Send disarm command.""" |
| 141 | + if code != str(self._api.device_pin): |
| 142 | + return |
| 143 | + await self._api.set_zone_status(self._area.index, ALARM_ACTIONS[DISABLE]) |
| 144 | + |
| 145 | + async def async_alarm_arm_away(self, code: str | None = None) -> None: |
| 146 | + """Send arm away command.""" |
| 147 | + await self._api.set_zone_status(self._area.index, ALARM_ACTIONS[AWAY]) |
| 148 | + |
| 149 | + async def async_alarm_arm_home(self, code: str | None = None) -> None: |
| 150 | + """Send arm home command.""" |
| 151 | + await self._api.set_zone_status(self._area.index, ALARM_ACTIONS[HOME]) |
| 152 | + |
| 153 | + async def async_alarm_arm_night(self, code: str | None = None) -> None: |
| 154 | + """Send arm night command.""" |
| 155 | + await self._api.set_zone_status(self._area.index, ALARM_ACTIONS[NIGHT]) |
0 commit comments