forked from cadavre/miio_gateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm_control_panel.py
150 lines (120 loc) · 5.13 KB
/
alarm_control_panel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import logging
import homeassistant.components.alarm_control_panel as alarm
from . import DOMAIN, XiaomiGwDevice
from homeassistant.const import (
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_NIGHT,
STATE_ALARM_DISARMED, STATE_ALARM_TRIGGERED)
from homeassistant.components.alarm_control_panel.const import (
SUPPORT_ALARM_ARM_AWAY, SUPPORT_ALARM_ARM_HOME,
SUPPORT_ALARM_ARM_NIGHT, SUPPORT_ALARM_TRIGGER)
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
_LOGGER.info("Setting up alarm")
devices = []
gateway = hass.data[DOMAIN]
devices.append(XiaomiGatewayAlarm(gateway))
add_entities(devices)
class XiaomiGatewayAlarm(XiaomiGwDevice, alarm.AlarmControlPanelEntity):
def __init__(self, gw):
XiaomiGwDevice.__init__(self, gw, "alarm_control_panel", None, "miio.gateway", "Gateway Alarm")
# Default to ARMED_AWAY if no volume data was set
self._state_by_volume = STATE_ALARM_ARMED_AWAY
self._volume = 80
# How to alarm
self._ringtone = 1
self._color = "ff0000"
self.update_device_params()
def update_device_params(self):
if self._gw.is_available():
self._send_to_hub({ "method": "get_prop", "params": ["arming"] }, self._init_set_arming)
self._send_to_hub({ "method": "get_prop", "params": ["alarming_volume"] }, self._init_set_volume)
def _init_set_arming(self, result):
if result is not None:
_LOGGER.info("SETTING ARMED: " + str(result))
if result == "on":
self._state = self._state_by_volume
elif result == "off":
self._state = STATE_ALARM_DISARMED
def _init_set_volume(self, result):
if result is not None:
_LOGGER.info("SETTING ARMED VOL: " + str(result))
self._volume = int(result)
self._state_by_volume = self._get_state_by_volume(self._volume)
if self._is_armed():
self._state = self._state_by_volume
def alarm_disarm(self, code=None):
"""Send disarm command."""
self._disarm()
self._state = STATE_ALARM_DISARMED
self.schedule_update_ha_state()
def alarm_arm_away(self, code=None):
"""Send arm away command."""
self._volume = 80
self._arm()
self._state = STATE_ALARM_ARMED_AWAY
self.schedule_update_ha_state()
def alarm_arm_home(self, code=None):
"""Send arm home command."""
self._volume = 25
self._arm()
self._state = STATE_ALARM_ARMED_HOME
self.schedule_update_ha_state()
def alarm_arm_night(self, code=None):
"""Send arm night command."""
self._volume = 15
self._arm()
self._state = STATE_ALARM_ARMED_NIGHT
self.schedule_update_ha_state()
def alarm_trigger(self, code=None):
"""Trigger the alarm."""
self._siren()
self._blink()
self._state = STATE_ALARM_TRIGGERED
self.schedule_update_ha_state()
def _arm(self):
self._send_to_hub({ "method": "set_alarming_volume", "params": [self._volume] })
self._send_to_hub({ "method": "set_sound_playing", "params": ["off"] })
self._send_to_hub({ "method": "set_arming", "params": ["on"] })
def _disarm(self):
self._send_to_hub({ "method": "set_sound_playing", "params": ["off"] })
self._send_to_hub({ "method": "set_arming", "params": ["off"] })
def _siren(self):
# TODO playlist
self._send_to_hub({ "method": "play_music_new", "params": [str(self._ringtone), self._volume] })
def _blink(self):
# TODO blink
argbhex = [int("01" + self._color, 16), int("64" + self._color, 16)]
self._send_to_hub({ "method": "set_rgb", "params": [argbhex[1]] })
def _is_armed(self):
if self._state is not None or self._state != STATE_ALARM_TRIGGERED or self._state != STATE_ALARM_DISARMED:
return True
return False
def _get_state_by_volume(self, volume):
if volume < 20:
return STATE_ALARM_ARMED_NIGHT
elif volume < 30:
return STATE_ALARM_ARMED_HOME
else:
return STATE_ALARM_ARMED_AWAY
@property
def state(self):
return self._state
@property
def supported_features(self) -> int:
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT | SUPPORT_ALARM_TRIGGER
def parse_incoming_data(self, model, sid, event, params):
arming = params.get("arming")
if arming is not None:
if arming == "on":
self._state = self._get_state_by_volume(self._volume)
elif arming == "off":
self._state = STATE_ALARM_DISARMED
return True
alarming_volume = params.get("alarming_volume")
if alarming_volume is not None:
self._volume = int(alarming_volume)
self._state_by_volume = self._get_state_by_volume(self._volume)
if self._is_armed():
self._state = self._state_by_volume
return True
return False