|
| 1 | +from homeassistant.components.alarm_control_panel import ( |
| 2 | + AlarmControlPanelEntity, |
| 3 | + AlarmControlPanelEntityFeature, |
| 4 | + AlarmControlPanelState, |
| 5 | +) |
| 6 | + |
| 7 | +from .core.const import DOMAIN |
| 8 | +from .core.entity import XEntity |
| 9 | +from .core.ewelink import SIGNAL_ADD_ENTITIES, XRegistry |
| 10 | + |
| 11 | +PARALLEL_UPDATES = 0 # fix entity_platform parallel_updates Semaphore |
| 12 | + |
| 13 | + |
| 14 | +async def async_setup_entry(hass, config_entry, add_entities): |
| 15 | + ewelink: XRegistry = hass.data[DOMAIN][config_entry.entry_id] |
| 16 | + ewelink.dispatcher_connect( |
| 17 | + SIGNAL_ADD_ENTITIES, |
| 18 | + lambda x: add_entities( |
| 19 | + [e for e in x if isinstance(e, AlarmControlPanelEntity)] |
| 20 | + ), |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +STATES = { |
| 25 | + 0: AlarmControlPanelState.DISARMED, |
| 26 | + 1: AlarmControlPanelState.ARMED_HOME, |
| 27 | + 2: AlarmControlPanelState.ARMED_AWAY, |
| 28 | + 3: AlarmControlPanelState.ARMED_NIGHT, |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +class XAlarmPanel(XEntity, AlarmControlPanelEntity): |
| 33 | + param = "securityType" |
| 34 | + uid = "alarm" |
| 35 | + |
| 36 | + _attr_code_arm_required = False |
| 37 | + _attr_supported_features = ( |
| 38 | + AlarmControlPanelEntityFeature.ARM_HOME |
| 39 | + | AlarmControlPanelEntityFeature.ARM_AWAY |
| 40 | + | AlarmControlPanelEntityFeature.ARM_NIGHT |
| 41 | + ) |
| 42 | + |
| 43 | + def set_state(self, params: dict): |
| 44 | + if self.param in params: |
| 45 | + self._attr_alarm_state = STATES.get(params[self.param]) |
| 46 | + |
| 47 | + async def async_alarm_disarm(self, code=None): |
| 48 | + await self.ewelink.send(self.device, {self.param: 0}) |
| 49 | + |
| 50 | + async def async_alarm_arm_home(self, code=None): |
| 51 | + await self.ewelink.send(self.device, {self.param: 1, "currentType": 1}) |
| 52 | + |
| 53 | + async def async_alarm_arm_away(self, code=None): |
| 54 | + await self.ewelink.send(self.device, {self.param: 2, "currentType": 2}) |
| 55 | + |
| 56 | + async def async_alarm_arm_night(self, code=None): |
| 57 | + await self.ewelink.send(self.device, {self.param: 3, "currentType": 3}) |
0 commit comments