forked from szymi-/hass_shutterbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.py
233 lines (188 loc) · 7.06 KB
/
cover.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import json
import logging
from enum import Enum
import async_timeout
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.components.cover import (
CoverDevice,
DEVICE_CLASS_SHUTTER,
PLATFORM_SCHEMA,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP,
)
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TIMEOUT
_LOGGER = logging.getLogger(__name__)
DEFAULT_TIMEOUT = 10
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_TIMEOUT): cv.positive_int,
})
class ControlType(Enum):
SEGMENTED_SHUTTER = 1
APPLIANCE_WITHOUT_POSITIONING = 2
TILT_SHUTTER = 3
WINDOW_OPENER = 4
MATERIAL_SHUTTER = 5
AWNING = 6
SCREEN = 7
async def _get(hass, host, url, timeout):
websession = async_get_clientsession(hass)
resource = "http://{}{}".format(host, url)
try:
with async_timeout.timeout(timeout, loop=hass.loop):
req = await websession.get(resource)
text_response = await req.text()
json_response = json.loads(text_response)
return json_response
except Exception as e:
_LOGGER.warning(
"Unable to get shutter response. "
"Host name: {}. URL: {}. Original exception: {}".format(
host, url, e
)
)
async def _get_shutter_info(hass, host, timeout):
url = "/api/device/state"
response = await _get(hass, host, url, timeout)
return response.get('device')
async def _get_shutter_settings(hass, host, timeout):
url = "/api/settings/state"
response = await _get(hass, host, url, timeout)
return response.get('settings')
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
host = config.get(CONF_HOST)
name = config.get(CONF_NAME)
timeout = config.get(CONF_TIMEOUT) or DEFAULT_TIMEOUT
if not name:
shutter_info = await _get_shutter_info(hass, host, timeout)
name = shutter_info.get('deviceName')
shutter_settings = await _get_shutter_settings(hass, host, timeout)
control_type = ControlType(shutter_settings['shutter']['controlType'])
if control_type not in [ControlType.SEGMENTED_SHUTTER, ControlType.TILT_SHUTTER]:
_LOGGER.exception("Only segmented shutter and tilt shutter modes are currently supported")
else:
blebox = {
ControlType.SEGMENTED_SHUTTER.name: ShutterBoxSegmentedShutter(
host, name=name, timeout=timeout
),
ControlType.TILT_SHUTTER.name: ShutterBoxTiltShutter(
host, name=name, timeout=timeout
),
}[control_type.name]
async_add_devices([blebox])
class ShutterBoxSegmentedShutter(CoverDevice):
_default_name = "ShutterBoxSegmentedShutter"
def __init__(self, host, name=None, timeout=DEFAULT_TIMEOUT):
self._host = host
self._name = name
self._timeout = timeout
self._state = None
self._available = False
@property
def device_class(self):
return DEVICE_CLASS_SHUTTER
@property
def name(self):
return self._name or self._default_name
@property
def is_opening(self):
if self._state:
return self._state.get('state') == 1
return False
@property
def is_closing(self):
if self._state:
return self._state.get('state') == 0
return False
@property
def is_closed(self):
return self._check_is_closed()
def _check_is_closed(self):
if self._state:
# Blebox built in state indicator doesn't seem reliable
current_pos = self._state.get('currentPos')
return current_pos.get('position') >= 95
return False
@property
def current_cover_position(self):
if self._state:
position = self._state.get('currentPos').get('position')
if position:
return self._invert_position(position)
return None
async def async_open_cover(self, **kwargs):
await self._send_shutter_command("u")
async def async_close_cover(self, **kwargs):
await self._send_shutter_command("d")
async def async_stop_cover(self, **kwargs):
await self._send_shutter_command("s")
async def async_set_cover_position(self, **kwargs):
position = self._invert_position(kwargs['position'])
await self._send_shutter_command("p", position)
async def async_update(self):
shutter_state = await self._get_shutter_state()
if shutter_state:
self._state = shutter_state
self._available = True
else:
self._state = None
self._available = False
async def _get_shutter_state(self):
url = "/api/shutter/state"
response = await _get(self.hass, self._host, url, self._timeout)
if response:
return response.get('shutter')
return None
async def _send_shutter_command(self, command, parameter=None):
url = "/s/{}".format(command)
if parameter is not None:
url = "{}/{}".format(url, parameter)
response = await _get(self.hass, self._host, url, self._timeout)
if response:
return response.get('shutter')
return None
def _invert_position(self, raw_position):
return -raw_position + 100
def _get_supported_features(self):
return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP | SUPPORT_SET_POSITION
@property
def supported_features(self):
return self._get_supported_features()
class ShutterBoxTiltShutter(ShutterBoxSegmentedShutter):
_default_name = "ShutterBoxTiltShutter"
@property
def current_cover_tilt_position(self):
if self._state:
position = self._state.get('currentPos').get('tilt')
if position:
return self._invert_position(position)
return None
async def async_open_cover_tilt(self, **kwargs):
await self._send_shutter_command("t", 0)
async def async_close_cover_tilt(self, **kwargs):
await self._send_shutter_command("t", 100)
async def async_set_cover_tilt_position(self, **kwargs):
position = self._invert_position(kwargs['tilt_position'])
await self._send_shutter_command("t", position)
def _get_supported_features(self):
supported_features = super()._get_supported_features()
supported_features |= (
SUPPORT_OPEN_TILT
| SUPPORT_CLOSE_TILT
| SUPPORT_SET_TILT_POSITION
)
return supported_features
def _check_is_closed(self):
if self._state:
# Blebox built in state indicator doesn't seem reliable
current_pos = self._state.get('currentPos')
return current_pos.get('position') >= 95 and current_pos.get('tilt') >= 95
return False