forked from rzulian/lutron_custom_component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cover.py
executable file
·152 lines (121 loc) · 4.58 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
"""Support for Lutron motorized blind."""
# motor doesn't support OUTPUT,ID,1,0 or OUTPUT,ID,1,100 for some reason
# you have to use OUTPUT,ID,2 open, 3 close, 4 stop
# you cannot get the status because the stop command is setting level to 100
# you need travel time to set open and close the blind
import voluptuous as vol
import logging
import asyncio
import time
from homeassistant.components.cover import (
PLATFORM_SCHEMA,
ATTR_POSITION,
SUPPORT_CLOSE,
SUPPORT_OPEN,
SUPPORT_STOP,
SUPPORT_SET_POSITION,
CoverEntity,
)
from homeassistant.const import CONF_NAME
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Lutron shades."""
devs = []
for (area_name, device) in hass.data[LUTRON_DEVICES]["cover"]:
if device.type == "MOTOR":
dev = LutronMotorBlind(area_name, device, hass.data[LUTRON_CONTROLLER])
else:
dev = LutronCover(area_name, device, hass.data[LUTRON_CONTROLLER])
devs.append(dev)
add_entities(devs, True)
return True
class LutronMotorBlind(LutronDevice, CoverEntity):
"""Representation of a Lutron motorized blind."""
def __init__(self, area_name, lutron_device, controller):
"""Initializes the Lutron Blind class."""
super().__init__(area_name, lutron_device, controller)
self._position = self._lutron_device.last_level()
self._is_opening = False
self._is_closing = False
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
@property
def is_closed(self):
"""Return if the cover is closed."""
return self._position == 0
@property
def is_closing(self):
"""Return if the cover is closed."""
return self._is_closing
@property
def is_opening(self):
"""Return if the cover is closed."""
return self._is_opening
async def async_close_cover(self, **kwargs):
"""Close the cover."""
self._position = 0
self._is_closing = True
self._lutron_device.start_lowering()
async def async_open_cover(self, **kwargs):
"""Open the cover."""
self._position = 100
self._is_opening = True
self._lutron_device.start_raising()
async def async_stop_cover(self, **kwargs):
"""stop the cover."""
self._lutron_device.stop()
self._position = 50
self._is_opening = False
self._is_closing = False
def update(self):
"""Call when forcing a refresh of the device."""
# Reading the property (rather than last_level()) fetches value
level = self._position
_LOGGER.debug("Lutron ID: %d updated to %f", self._lutron_device.id, level)
@property
def extra_state_attributes(self):
"""Return the state attributes."""
attr = {}
attr["Lutron Integration ID"] = self._lutron_device.id
return attr
class LutronCover(LutronDevice, CoverEntity):
"""Representation of a Lutron shade."""
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION
@property
def is_closed(self):
"""Return if the cover is closed."""
return self._lutron_device.last_level() < 1
@property
def current_cover_position(self):
"""Return the current position of cover."""
return self._lutron_device.last_level()
def close_cover(self, **kwargs):
"""Close the cover."""
self._lutron_device.level = 0
def open_cover(self, **kwargs):
"""Open the cover."""
self._lutron_device.level = 100
def set_cover_position(self, **kwargs):
"""Move the shade to a specific position."""
if ATTR_POSITION in kwargs:
position = kwargs[ATTR_POSITION]
self._lutron_device.level = position
def update(self):
"""Call when forcing a refresh of the device."""
# Reading the property (rather than last_level()) fetches value
level = self._lutron_device.level
_LOGGER.debug("Lutron ID: %d updated to %f", self._lutron_device.id, level)
@property
def extra_state_attributes(self):
"""Return the state attributes."""
attr = {}
attr["Lutron Integration ID"] = self._lutron_device.id
return attr