-
Notifications
You must be signed in to change notification settings - Fork 9
/
cover.py
206 lines (172 loc) · 6.15 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
"""Support for Becker RF covers."""
import logging
import voluptuous as vol
from homeassistant.components.cover import (
PLATFORM_SCHEMA,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_STOP,
CoverEntity,
)
from homeassistant.const import (
CONF_COVERS,
CONF_DEVICE,
CONF_FRIENDLY_NAME,
CONF_VALUE_TEMPLATE,
STATE_CLOSED,
STATE_OPEN,
)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from . import extract_entities, initialise_templates
from .const import (
CONF_CHANNEL,
DEVICE_CLASS,
CLOSED_POSITION,
OPEN_POSITION,
VENTILATION_POSITION,
INTERMEDIATE_POSITION,
)
from .rf_device import PyBecker
_LOGGER = logging.getLogger(__name__)
COVER_FEATURES = (
SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP | SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT
)
_VALID_STATES = [
STATE_OPEN,
STATE_CLOSED,
"true",
"false",
]
COVER_SCHEMA = vol.Schema(
{
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
vol.Required(CONF_CHANNEL): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA),
vol.Optional(CONF_DEVICE): cv.string,
}
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the becker platform."""
covers = []
stick_path = config.get(CONF_DEVICE)
PyBecker.setup(stick_path)
for init_call_count in range(2):
_LOGGER.debug("Init call to cover channel 1 #%d" % init_call_count)
await PyBecker.becker.stop("1")
for device, device_config in config[CONF_COVERS].items():
friendly_name = device_config.get(CONF_FRIENDLY_NAME, device)
channel = device_config.get(CONF_CHANNEL)
state_template = device_config.get(CONF_VALUE_TEMPLATE)
if channel is None:
_LOGGER.error("Must specify %s", CONF_CHANNEL)
continue
templates = {
CONF_VALUE_TEMPLATE: state_template,
}
initialise_templates(hass, templates)
entity_ids = extract_entities(device, "cover", None, templates)
covers.append(
BeckerEntity(
PyBecker.becker, friendly_name, channel, state_template, entity_ids
)
)
async_add_entities(covers)
class BeckerEntity(CoverEntity, RestoreEntity):
"""Representation of a Becker cover entity."""
def __init__(self, becker, name, channel, state_template, entity_ids, position=0):
"""Init the Becker device."""
self._becker = becker
self._name = name
self._channel = channel
self._template = state_template
self._entities = entity_ids
self._position = position
self._state = True
async def async_added_to_hass(self):
"""Register callbacks."""
await super().async_added_to_hass()
old_state = await self.async_get_last_state()
if old_state is not None:
self._state = old_state.state == STATE_OPEN
@property
def name(self):
"""Return the name of the device as reported by tellcore."""
return self._name
@property
def unique_id(self):
"""Return the unique id of the device - the channel."""
return self._channel
@property
def current_cover_position(self):
"""Return current position of cover. None is unknown, 0 is closed, 100 is fully open."""
return self._position
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS
@property
def supported_features(self):
"""Flag supported features."""
return COVER_FEATURES
@property
def is_closed(self):
"""Return true if cover is closed, else False."""
return self._position == CLOSED_POSITION
async def async_open_cover(self, **kwargs):
"""Set the cover to the open position."""
if self._template is None:
self._position = OPEN_POSITION
await self._becker.move_up(self._channel)
async def async_open_cover_tilt(self, **kwargs):
"""Open the cover tilt."""
if self._position == CLOSED_POSITION:
self._position = VENTILATION_POSITION
elif self._position == VENTILATION_POSITION:
self._position = OPEN_POSITION
await self._becker.move_up_intermediate(self._channel)
async def async_close_cover(self, **kwargs):
"""Set the cover to the closed position."""
if self._template is None:
self._position = CLOSED_POSITION
await self._becker.move_down(self._channel)
async def async_close_cover_tilt(self, **kwargs):
"""Close the cover tilt."""
if self._position == OPEN_POSITION:
self._position = INTERMEDIATE_POSITION
elif self._position == INTERMEDIATE_POSITION:
self._position = CLOSED_POSITION
await self._becker.move_down_intermediate(self._channel)
async def async_stop_cover(self, **kwargs):
"""Set the cover to the stopped position."""
if self._template is None:
self._position = 50
await self._becker.stop(self._channel)
async def async_update(self):
"""Update the position of the cover."""
if self._template is not None:
try:
state = self._template.async_render().lower()
if state in _VALID_STATES:
if state in ("true", STATE_OPEN):
self._position = 100
else:
self._position = 0
else:
_LOGGER.error(
"Received invalid cover is_on state: %s. Expected: %s",
state,
", ".join(_VALID_STATES),
)
self._position = None
except TemplateError as ex:
_LOGGER.error(ex)
self._position = None