-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcover.py
191 lines (156 loc) · 6.68 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
"""Platform for Pixie Plus switch integration."""
from __future__ import annotations
from datetime import timedelta
import logging
import time
import json
import async_timeout
from . import pixiepluslogin
import asyncio
from homeassistant.components.cover import CoverEntity, CoverEntityFeature
from homeassistant.core import HomeAssistant
from homeassistant.core import callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant import config_entries
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from typing import Any
from .const import DOMAIN, hardware_list, is_cover, has_two_entities
_LOGGER = logging.getLogger(__name__)
"""
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""
async def async_setup_entry(
hass: HomeAssistant,
# config: ConfigType,
config_entry: config_entries.ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Pixie Plus switch platform."""
# Assigning configuration variables from HA config
# The configuration check takes care they are present.
# passing the Pixie Plus devices list with data about all the devices - list of dictionaries, eacy dictionary is a device
coordinator = hass.data[DOMAIN][config_entry.entry_id]
config = hass.data[DOMAIN]
cover_exists = 0
for ent in coordinator.data:
if (str(ent["type"]).zfill(2) + str(ent["stype"]).zfill(2)) in is_cover:
cover_exists = 1
# adding entities
if cover_exists == 1:
if (
"cover" in config
): # checking that user added cover config in configuration.yaml
cover_config = config["cover"]
async_add_entities(
PixiePlusCover(coordinator, idx, cover_config)
for idx, ent in enumerate(coordinator.data)
if (str(ent["type"]).zfill(2) + str(ent["stype"]).zfill(2)) in is_cover
)
else:
_LOGGER.info(
"Unable to add cover entities because they are not defined in configuration.yaml - please see documentation"
)
class PixiePlusCover(CoordinatorEntity, CoverEntity):
"""Representation of a Pixie Plus Cover."""
def __init__(self, coordinator, idx, cover_config):
"""Initialize a Pixie Plus Cover."""
super().__init__(coordinator)
self.idx = idx
self._mac = self.coordinator.data[self.idx]["mac"]
self._id = self.coordinator.data[self.idx]["id"]
self._type = self.coordinator.data[self.idx]["type"]
self._stype = self.coordinator.data[self.idx]["stype"]
self._email = self.coordinator.data[self.idx]["email"]
self._applicationid = self.coordinator.data[self.idx]["applicationid"]
self._installationid = self.coordinator.data[self.idx]["installationid"]
self._clientkey = self.coordinator.data[self.idx]["clientkey"]
self._userid = self.coordinator.data[self.idx]["userid"]
self._homeid = self.coordinator.data[self.idx]["homeid"]
self._livegroup_objectid = self.coordinator.data[self.idx]["livegroup_objectid"]
self._sessiontoken = self.coordinator.data[self.idx]["sessiontoken"]
self._attr_has_entity_name = True
self._model_no = str(self._type).zfill(2) + str(self._stype).zfill(2)
self._attr_unique_id = self._mac
self._attr_name = None
self._cover_name = (
self.coordinator.data[self.idx]["name"].replace(" ", "_").lower()
)
self._attr_supported_features = []
if self._cover_name in cover_config:
if "open" in cover_config[self._cover_name]:
self._cover_open = cover_config[self._cover_name]["open"]
self._attr_supported_features = CoverEntityFeature.OPEN
else:
self._cover_open = ""
if "close" in cover_config[self._cover_name]:
self._cover_close = cover_config[self._cover_name]["close"]
if self._attr_supported_features:
self._attr_supported_features |= CoverEntityFeature.CLOSE
else:
self._attr_supported_features = CoverEntityFeature.CLOSE
else:
self._cover_close = ""
if "stop" in cover_config[self._cover_name]:
self._cover_stop = cover_config[self._cover_name]["stop"]
if self._attr_supported_features:
self._attr_supported_features |= CoverEntityFeature.STOP
else:
self._attr_supported_features = CoverEntityFeature.STOP
else:
self._cover_stop = ""
else:
_LOGGER.error(
f"Unable to setup cover {self._cover_name} because there is no matching cover entry in configuration.yaml. See documentation and check spelling or letter case"
)
response = pixiepluslogin.initiate_cover(self)
_LOGGER.debug(
f"cover after self attribution with following response: {response}"
)
@property
def device_info(self):
name = self.coordinator.data[self.idx]["name"]
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self._mac)
},
"name": name,
"manufacturer": "SAL - Pixie Plus",
"model": hardware_list[self._model_no],
"via_device": (DOMAIN, "Pixie Plus Hub"),
}
@property
def device_class(self):
return None
@property
def is_closed(self):
return True
@property
def assumed_state(self):
return True
async def async_open_cover(self, **kwargs: Any) -> None:
# Instructs the cover to open.
other = ()
await pixiepluslogin.change_light(self, "open", other)
async def async_close_cover(self, **kwargs: Any) -> None:
"""Instruct the cover to close."""
other = ()
await pixiepluslogin.change_light(self, "close", other)
async def async_stop_cover(self, **kwargs: Any) -> None:
"""Instruct the cover to stop."""
other = ()
await pixiepluslogin.change_light(self, "stop", other)