forked from yutoyazaki/hass-nature-remo
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path__init__.py
188 lines (156 loc) · 6.03 KB
/
__init__.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
"""The Nature Remo integration."""
import logging
import voluptuous as vol
from datetime import timedelta
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.entity import Entity
from homeassistant.const import CONF_ACCESS_TOKEN
_LOGGER = logging.getLogger(__name__)
_RESOURCE = "https://api.nature.global/1/"
DOMAIN = "nature_remo"
CONF_COOL_TEMP = "cool_temperature"
CONF_HEAT_TEMP = "heat_temperature"
DEFAULT_COOL_TEMP = 28
DEFAULT_HEAT_TEMP = 20
DEFAULT_UPDATE_INTERVAL = timedelta(seconds=60)
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_ACCESS_TOKEN): cv.string,
vol.Optional(CONF_COOL_TEMP, default=DEFAULT_COOL_TEMP): vol.Coerce(
int
),
vol.Optional(CONF_HEAT_TEMP, default=DEFAULT_HEAT_TEMP): vol.Coerce(
int
),
}
)
},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass, config):
"""Set up Nature Remo component."""
_LOGGER.debug("Setting up Nature Remo component.")
access_token = config[DOMAIN][CONF_ACCESS_TOKEN]
session = async_get_clientsession(hass)
api = NatureRemoAPI(access_token, session)
coordinator = hass.data[DOMAIN] = DataUpdateCoordinator(
hass,
_LOGGER,
name="Nature Remo update",
update_method=api.get,
update_interval=DEFAULT_UPDATE_INTERVAL,
)
await coordinator.async_refresh()
hass.data[DOMAIN] = {
"api": api,
"coordinator": coordinator,
"config": config[DOMAIN],
}
await discovery.async_load_platform(hass, "sensor", DOMAIN, {}, config)
await discovery.async_load_platform(hass, "climate", DOMAIN, {}, config)
await discovery.async_load_platform(hass, "light", DOMAIN, {}, config)
await discovery.async_load_platform(hass, "switch", DOMAIN, {}, config)
return True
class NatureRemoAPI:
"""Nature Remo API client"""
def __init__(self, access_token, session):
"""Init API client"""
self._access_token = access_token
self._session = session
async def get(self):
"""Get appliance and device list"""
_LOGGER.debug("Trying to fetch appliance and device list from API.")
headers = {"Authorization": f"Bearer {self._access_token}"}
response = await self._session.get(f"{_RESOURCE}/appliances", headers=headers)
appliances = {x["id"]: x for x in await response.json()}
response = await self._session.get(f"{_RESOURCE}/devices", headers=headers)
devices = {x["id"]: x for x in await response.json()}
return {"appliances": appliances, "devices": devices}
async def post(self, path, data):
"""Post any request"""
_LOGGER.debug("Trying to request post:%s, data:%s", path, data)
headers = {"Authorization": f"Bearer {self._access_token}"}
response = await self._session.post(
f"{_RESOURCE}{path}", data=data, headers=headers
)
return await response.json()
async def getany(self, path):
"""Get any request"""
_LOGGER.debug("Trying to request get:%s", path)
headers = {"Authorization": f"Bearer {self._access_token}"}
response = await self._session.get(f"{_RESOURCE}{path}", headers=headers)
signal_list = {x: x for x in await response.json()}
return signal_list
class NatureRemoBase(Entity):
"""Nature Remo entity base class."""
def __init__(self, coordinator, appliance):
self._coordinator = coordinator
self._name = f"Nature Remo {appliance['nickname']}"
self._appliance_id = appliance["id"]
self._device = appliance["device"]
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unique_id(self):
"""Return a unique ID."""
return self._appliance_id
@property
def should_poll(self):
"""Return the polling requirement of the entity."""
return False
@property
def device_info(self):
"""Return the device info for the sensor."""
# Since device registration requires Config Entries, this dosen't work for now
return {
"identifiers": {(DOMAIN, self._device["id"])},
"name": self._device["name"],
"manufacturer": "Nature Remo",
"model": self._device["serial_number"],
"sw_version": self._device["firmware_version"],
}
class NatureRemoDeviceBase(Entity):
"""Nature Remo Device entity base class."""
def __init__(self, coordinator, device):
self._coordinator = coordinator
self._name = f"Nature Remo {device['name']}"
self._device = device
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unique_id(self):
"""Return a unique ID."""
return self._device["id"]
@property
def should_poll(self):
"""Return the polling requirement of the entity."""
return True
@property
def device_info(self):
"""Return the device info for the sensor."""
# Since device registration requires Config Entries, this dosen't work for now
return {
"identifiers": {(DOMAIN, self._device["id"])},
"name": self._device["name"],
"manufacturer": "Nature Remo",
"model": self._device["serial_number"],
"sw_version": self._device["firmware_version"],
}
async def async_added_to_hass(self):
"""Subscribe to updates."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()