-
Notifications
You must be signed in to change notification settings - Fork 8
/
sensor.py
350 lines (297 loc) · 10.2 KB
/
sensor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""MyJDownloader sensors."""
from __future__ import annotations
import datetime
from homeassistant.components.sensor import DOMAIN, SensorEntity, SensorStateClass
from homeassistant.const import DATA_RATE_MEGABYTES_PER_SECOND
from homeassistant.core import callback
from homeassistant.helpers import entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from . import MyJDownloaderHub
from .const import (
ATTR_LINKS,
ATTR_PACKAGES,
DATA_MYJDOWNLOADER_CLIENT,
DOMAIN as MYJDOWNLOADER_DOMAIN,
SCAN_INTERVAL_SECONDS,
SERVICE_RESTART_AND_UPDATE,
SERVICE_RUN_UPDATE_CHECK,
SERVICE_START_DOWNLOADS,
SERVICE_STOP_DOWNLOADS,
)
from .entities import MyJDownloaderDeviceEntity, MyJDownloaderEntity
SCAN_INTERVAL = datetime.timedelta(seconds=SCAN_INTERVAL_SECONDS)
async def async_setup_entry(hass, entry, async_add_entities, discovery_info=None):
"""Set up the sensor using config entry."""
hub = hass.data[MYJDOWNLOADER_DOMAIN][entry.entry_id][DATA_MYJDOWNLOADER_CLIENT]
# This device-less sensor periodically fetches the list of currently online devices
async_add_entities([MyJDownloaderJDownloadersOnlineSensor(hub)], True)
@callback
def async_add_sensor(devices=hub.devices):
entities = []
for device_id in devices.keys():
if DOMAIN not in hub.devices_platforms[device_id]:
hub.devices_platforms[device_id].add(DOMAIN)
entities += [
MyJDownloaderDownloadSpeedSensor(hub, device_id),
MyJDownloaderPackagesSensor(hub, device_id),
MyJDownloaderLinksSensor(hub, device_id),
MyJDownloaderStatusSensor(hub, device_id),
]
if entities:
async_add_entities(entities, True)
entry.async_on_unload(
async_dispatcher_connect(
hass, f"{MYJDOWNLOADER_DOMAIN}_new_devices", async_add_sensor
)
)
async_add_sensor(hub.devices)
# device services
platform = entity_platform.current_platform.get()
assert platform is not None
platform.async_register_entity_service(
SERVICE_RESTART_AND_UPDATE,
{},
"restart_and_update",
)
platform.async_register_entity_service(
SERVICE_RUN_UPDATE_CHECK,
{},
"run_update_check",
)
platform.async_register_entity_service(
SERVICE_START_DOWNLOADS,
{},
"start_downloads",
)
platform.async_register_entity_service(
SERVICE_STOP_DOWNLOADS,
{},
"stop_downloads",
)
class MyJDownloaderDeviceSensor(MyJDownloaderDeviceEntity, SensorEntity):
"""Defines a MyJDownloader device sensor."""
def __init__(
self,
hub: MyJDownloaderHub,
device_id: str,
name_template: str,
icon: str | None,
measurement: str,
unit_of_measurement: str | None,
state_class: str | None,
entity_category: EntityCategory | None = None,
enabled_default: bool = True,
) -> None:
"""Initialize MyJDownloader sensor."""
self._state: str | None = None
self._unit_of_measurement = unit_of_measurement
self._state_class = state_class
self.measurement = measurement
super().__init__(
hub, device_id, name_template, icon, entity_category, enabled_default
)
@property
def unique_id(self) -> str:
"""Return the unique ID for this sensor."""
return "_".join(
[
MYJDOWNLOADER_DOMAIN,
self._name,
DOMAIN,
self.measurement,
]
)
@property
def native_value(self) -> str | None:
"""Return the native value of the sensor."""
return self._state
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit this entity's native value is expressed in."""
return self._unit_of_measurement
@property
def state_class(self) -> str | None:
"""State class of sensor."""
return self._state_class
class MyJDownloaderSensor(MyJDownloaderEntity):
"""Defines a MyJDownloader sensor entity."""
def __init__(
self,
hub: MyJDownloaderHub,
name: str,
icon: str,
measurement: str,
unit_of_measurement: str | None,
state_class: str | None,
entity_category: EntityCategory | None = None,
enabled_default: bool = True,
) -> None:
"""Initialize MyJDownloader sensor."""
self._state: str | None = None
self._unit_of_measurement = unit_of_measurement
self._state_class = state_class
self.measurement = measurement
super().__init__(hub, name, icon, entity_category, enabled_default)
@property
def unique_id(self) -> str:
"""Return the unique ID for this sensor."""
return "_".join(
[
MYJDOWNLOADER_DOMAIN,
self._name,
DOMAIN,
self.measurement,
]
)
@property
def state(self) -> str | None:
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit this state is expressed in."""
return self._unit_of_measurement
@property
def state_class(self) -> str | None:
"""State class of sensor."""
return self._state_class
class MyJDownloaderJDownloadersOnlineSensor(MyJDownloaderSensor):
"""Defines a MyJDownloader JDownloaders Online sensor."""
def __init__(
self,
hub: MyJDownloaderHub,
) -> None:
"""Initialize MyJDownloader sensor."""
super().__init__(
hub, "JDownloaders Online", "mdi:download-multiple", "number", None, None
)
self.devices: list = []
async def _myjdownloader_update(self) -> None:
"""Update MyJDownloader entity."""
await self.hub.async_update_devices()
self.devices = self.hub.devices
self._state = str(len(self.devices))
@property
def extra_state_attributes(self):
"""Return the state attributes."""
return {"jdownloaders": sorted(device.name for device in self.devices.values())}
class MyJDownloaderDownloadSpeedSensor(MyJDownloaderDeviceSensor):
"""Defines a MyJDownloader download speed sensor."""
def __init__(
self,
hub: MyJDownloaderHub,
device_id: str,
) -> None:
"""Initialize MyJDownloader sensor."""
super().__init__(
hub,
device_id,
"JDownloader $device_name Download Speed",
"mdi:download",
"download_speed",
DATA_RATE_MEGABYTES_PER_SECOND,
SensorStateClass.MEASUREMENT,
)
async def _myjdownloader_update(self) -> None:
"""Update MyJDownloader entity."""
device = self.hub.get_device(self._device_id)
self._state = round(
await self.hub.async_query(device.downloadcontroller.get_speed_in_bytes)
/ 1_000_000,
2,
)
class MyJDownloaderPackagesSensor(MyJDownloaderDeviceSensor):
"""Defines a MyJDownloader packages sensor."""
def __init__(
self,
hub: MyJDownloaderHub,
device_id: str,
) -> None:
"""Initialize MyJDownloader sensor."""
self._packages_list: list = []
super().__init__(
hub,
device_id,
"JDownloader $device_name Packages",
"mdi:download",
"packages",
None,
None,
None,
False,
)
async def _myjdownloader_update(self) -> None:
"""Update MyJDownloader entity."""
device = self.hub.get_device(self._device_id)
self._packages_list = await self.hub.async_query(
device.downloads.query_packages
)
self._state = str(len(self._packages_list))
@property
def extra_state_attributes(self):
"""Return the state attributes."""
return {ATTR_PACKAGES: self._packages_list}
class MyJDownloaderLinksSensor(MyJDownloaderDeviceSensor):
"""Defines a MyJDownloader links sensor."""
def __init__(
self,
hub: MyJDownloaderHub,
device_id: str,
) -> None:
"""Initialize MyJDownloader sensor."""
self._links_list: list = []
super().__init__(
hub,
device_id,
"JDownloader $device_name Links",
"mdi:download",
"links",
None,
None,
None,
False,
)
async def _myjdownloader_update(self) -> None:
"""Update MyJDownloader entity."""
device = self.hub.get_device(self._device_id)
self._links_list = await self.hub.async_query(device.downloads.query_links)
self._state = str(len(self._links_list))
@property
def extra_state_attributes(self):
"""Return the state attributes."""
return {ATTR_LINKS: self._links_list}
class MyJDownloaderStatusSensor(MyJDownloaderDeviceSensor):
"""Defines a MyJDownloader status sensor."""
STATE_ICONS = {
"idle": "mdi:stop",
"running": "mdi:play",
"pause": "mdi:pause",
"stopped": "mdi:stop",
}
def __init__(
self,
hub: MyJDownloaderHub,
device_id: str,
) -> None:
"""Initialize MyJDownloader sensor."""
super().__init__(
hub,
device_id,
"JDownloader $device_name Status",
"mdi:play-pause",
"status",
None,
None,
)
@property
def icon(self) -> str | None:
"""Return the mdi icon of the entity."""
if self._state:
return MyJDownloaderStatusSensor.STATE_ICONS.get(self._state, self._icon)
return self._icon
async def _myjdownloader_update(self) -> None:
"""Update MyJDownloader entity."""
device = self.hub.get_device(self._device_id)
status = await self.hub.async_query(device.downloadcontroller.get_current_state)
self._state = status.lower().replace("_state", "") # stopped_state -> stopped