-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.py
executable file
·180 lines (150 loc) · 5.14 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
"""Support for NeoSmartBlinds covers."""
import logging
import requests
import voluptuous as vol
from homeassistant.components.cover import (
PLATFORM_SCHEMA,
CoverEntity,
SUPPORT_OPEN,
SUPPORT_CLOSE,
SUPPORT_STOP,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_NAME,
CONF_HOST,
CONF_ID,
CONF_DEVICES,
)
from homeassistant.helpers import config_validation as cv
_LOGGER = logging.getLogger(__name__)
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
# api call is http://{ip address}:8838/neo/v1/transmit?command={blind id}-{command}&id={controller id (24 chars)}'
# commands are up (UP), dn (DOWN) and sp (STOP)
############
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_ID): cv.string,
vol.Optional(CONF_DEVICES, default={}): {
cv.string: vol.Schema({vol.Required(CONF_NAME): cv.string,})
},
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the NEOSMARTBLINDS global variables."""
controller_host = config.get(CONF_HOST)
controller_id = config.get(CONF_ID)
"""Set up the NEOSMARTBLINDS covers."""
covers = []
devices = config.get(CONF_DEVICES)
if devices:
for _, entity_info in devices.items():
name = entity_info.get(CONF_NAME)
id = _
_LOGGER.info("Adding %s neosmartblinds cover", name, " ", id)
cover = NeoSmartBlindsCover(
hass,
id=id,
name=name,
controller_host=controller_host,
controller_id=controller_id,
)
covers.append(cover)
if not covers:
_LOGGER.error("No covers added")
return False
add_entities(covers)
#########################
class NeoSmartBlindsCover(CoverEntity):
"""Representation of NEOSMARTBLINDS cover."""
def __init__(self, hass, name, controller_host, id, controller_id):
"""Initialize the cover."""
self._id = id
self._hass = hass
self._name = name
self._controller_host = controller_host
self._controller_id = controller_id
self._available = True
self._device_class = None
self._is_closed = None
self._state = None
@property
def should_poll(self):
"""Return the polling state. No polling available from controller."""
return False
@property
def name(self):
"""Return the name of the cover."""
return self._name
@property
def available(self):
"""Return True if entity is available."""
return self._available
@property
def is_closed(self):
"""Return None as no state from controller."""
return None
@property
def id(self):
"""Return the id of the cover."""
return self._id
@property
def name(self):
"""Return the name of the cover."""
return self._name
@property
def unique_id(self):
"""Return a unique id for the entity"""
return "neosmartblinds" + "." + self._id
@property
def should_poll(self):
"""Return the polling state. No polling available from controller."""
return False
@property
def available(self):
"""Return True if entity is available."""
return self._available
@property
def is_closed(self):
"""Return None as no state from controller."""
return None
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._device_class
@property
def supported_features(self):
"""Flag supported features."""
return supported_features
def close_cover(self, **kwargs):
"""Close the cover."""
self.move_cover(self, command="dn")
def open_cover(self, **kwargs):
"""Open the cover."""
self.move_cover(self, command="up")
def stop_cover(self, **kwargs):
"""Stop the cover."""
self.move_cover(self, command="sp")
@staticmethod
def move_cover(self, command):
"""Build the uri and execute the actual commands."""
httpuri = f"http://{self._controller_host}:8838/neo/v1/transmit?command={self._id}-{command}&id={self._controller_id}"
request = requests.get(httpuri)
if request.status_code != 200:
if request.status_code == "400":
response = "400: Blind ID incorrect"
elif request.status_code == "401":
response = "401: Controller ID incorrect"
else:
response = request.status_code
_LOGGER.debug(
"Error executing: %s %s-%s %s - return code: %s",
self._controller_host,
self._id,
command,
self._controller_id,
response,
)
self._available = False
return True