forked from dext0r/yandex_smart_home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_prop_float.py
494 lines (410 loc) · 17.9 KB
/
test_prop_float.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
from __future__ import annotations
from homeassistant.components import (
air_quality,
binary_sensor,
climate,
cover,
fan,
humidifier,
light,
sensor,
switch,
water_heater,
)
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT,
ATTR_VOLTAGE,
PERCENTAGE,
STATE_ON,
STATE_UNKNOWN,
)
from homeassistant.core import State
import pytest
from pytest_homeassistant_custom_component.common import MockConfigEntry
from custom_components.yandex_smart_home import const
from custom_components.yandex_smart_home.error import SmartHomeError
from custom_components.yandex_smart_home.prop_event import PROPERTY_EVENT
from custom_components.yandex_smart_home.prop_float import PRESSURE_UNITS_TO_YANDEX_UNITS, PROPERTY_FLOAT, FloatProperty
from . import BASIC_CONFIG, MockConfig
from .test_prop import assert_no_properties, get_exact_one_property
class MockFloatProperty(FloatProperty):
instance = 'humidity'
def supported(self) -> bool:
return True
def get_value(self):
pass
async def test_property_float(hass):
prop = MockFloatProperty(hass, BASIC_CONFIG, State('binary_sensor.test', STATE_ON))
assert prop.convert_value(1, 'foo') == 1
@pytest.mark.parametrize('domain,device_class,attribute,supported', [
(sensor.DOMAIN, SensorDeviceClass.HUMIDITY, None, True),
(air_quality.DOMAIN, None, climate.ATTR_HUMIDITY, True),
(air_quality.DOMAIN, None, None, False),
(climate.DOMAIN, None, climate.ATTR_CURRENT_HUMIDITY, True),
(climate.DOMAIN, None, None, False),
(fan.DOMAIN, None, climate.ATTR_CURRENT_HUMIDITY, True),
(fan.DOMAIN, None, None, False),
(humidifier.DOMAIN, None, climate.ATTR_CURRENT_HUMIDITY, True),
(humidifier.DOMAIN, None, None, False),
])
async def test_property_float_humidity(hass, domain, device_class, attribute, supported):
attributes = {}
value = STATE_ON
if attribute is None:
value = 69
else:
attributes[attribute] = 69
if device_class:
attributes[ATTR_DEVICE_CLASS] = device_class
state = State(f'{domain}.test', value, attributes)
if supported:
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_HUMIDITY)
else:
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_HUMIDITY)
return
assert prop.retrievable
assert prop.parameters() == {'instance': 'humidity', 'unit': 'unit.percent'}
assert prop.get_value() == 69
if attribute is None:
value = -5
else:
attributes[attribute] = -5
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() == 0
if attribute is None:
value = STATE_UNKNOWN
else:
attributes[attribute] = None
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() is None
@pytest.mark.parametrize('domain,device_class,attribute,supported', [
(sensor.DOMAIN, SensorDeviceClass.TEMPERATURE, None, True),
(air_quality.DOMAIN, None, climate.ATTR_TEMPERATURE, True),
(air_quality.DOMAIN, None, None, False),
(climate.DOMAIN, None, climate.ATTR_CURRENT_TEMPERATURE, True),
(climate.DOMAIN, None, None, False),
(fan.DOMAIN, None, climate.ATTR_CURRENT_TEMPERATURE, True),
(fan.DOMAIN, None, None, False),
(humidifier.DOMAIN, None, climate.ATTR_CURRENT_TEMPERATURE, True),
(humidifier.DOMAIN, None, None, False),
(water_heater.DOMAIN, None, climate.ATTR_CURRENT_TEMPERATURE, True),
(water_heater.DOMAIN, None, None, False),
])
async def test_property_float_temperature(hass, domain, device_class, attribute, supported):
attributes = {}
value = STATE_ON
if attribute is None:
value = 34
else:
attributes[attribute] = 34
if device_class:
attributes[ATTR_DEVICE_CLASS] = device_class
state = State(f'{domain}.test', value, attributes)
if supported:
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_TEMPERATURE)
else:
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_TEMPERATURE)
return
assert prop.retrievable
assert prop.parameters() == {'instance': 'temperature', 'unit': 'unit.temperature.celsius'}
assert prop.get_value() == 34
if attribute is None:
value = -50
else:
attributes[attribute] = -50
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() == -50
if attribute is None:
value = STATE_UNKNOWN
else:
attributes[attribute] = None
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() is None
@pytest.mark.parametrize('yandex_pressure_unit,v', [
(const.PRESSURE_UNIT_PASCAL, 98658.28),
(const.PRESSURE_UNIT_MMHG, 740),
(const.PRESSURE_UNIT_ATM, 0.97),
(const.PRESSURE_UNIT_BAR, 0.99),
])
def test_property_float_pressure(hass, yandex_pressure_unit, v):
entry = MockConfigEntry(options={
const.CONF_PRESSURE_UNIT: yandex_pressure_unit
})
config = MockConfig(entry=entry)
state = State('sensor.test', '740', {
ATTR_DEVICE_CLASS: SensorDeviceClass.PRESSURE
})
assert_no_properties(hass, config, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_PRESSURE)
state = State('sensor.test', '740', {
ATTR_DEVICE_CLASS: SensorDeviceClass.PRESSURE,
ATTR_UNIT_OF_MEASUREMENT: const.PRESSURE_UNIT_MMHG
})
prop = get_exact_one_property(hass, config, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_PRESSURE)
prop.state = State('sensor.test', '740', {
ATTR_DEVICE_CLASS: SensorDeviceClass.PRESSURE
})
with pytest.raises(SmartHomeError):
prop.get_value()
state = State('sensor.test', '740', {
ATTR_DEVICE_CLASS: SensorDeviceClass.PRESSURE,
ATTR_UNIT_OF_MEASUREMENT: const.PRESSURE_UNIT_MMHG
})
prop = get_exact_one_property(hass, config, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_PRESSURE)
assert prop.retrievable
assert prop.parameters() == {
'instance': 'pressure',
'unit': PRESSURE_UNITS_TO_YANDEX_UNITS[yandex_pressure_unit]
}
assert prop.get_value() == v
prop.state = State('sensor.test', '-5', {
ATTR_DEVICE_CLASS: SensorDeviceClass.PRESSURE,
ATTR_UNIT_OF_MEASUREMENT: const.PRESSURE_UNIT_MMHG
})
assert prop.get_value() == 0
@pytest.mark.parametrize('domain,device_class,attribute,supported', [
(sensor.DOMAIN, SensorDeviceClass.ILLUMINANCE, None, True),
(sensor.DOMAIN, None, None, False),
(sensor.DOMAIN, None, 'illuminance', True),
(light.DOMAIN, None, 'illuminance', True),
(light.DOMAIN, None, None, False),
(fan.DOMAIN, None, 'illuminance', True),
(fan.DOMAIN, None, None, False),
])
async def test_property_float_illuminance(hass, domain, device_class, attribute, supported):
attributes = {}
value = STATE_ON
if attribute is None:
value = 48
else:
attributes[attribute] = 48
if device_class:
attributes[ATTR_DEVICE_CLASS] = device_class
state = State(f'{domain}.test', value, attributes)
if supported:
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_ILLUMINATION)
else:
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_ILLUMINATION)
return
assert prop.retrievable
assert prop.parameters() == {'instance': 'illumination', 'unit': 'unit.illumination.lux'}
assert prop.get_value() == 48
if attribute is None:
value = -5
else:
attributes[attribute] = -5
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() == 0
if attribute is None:
value = STATE_UNKNOWN
else:
attributes[attribute] = None
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() is None
@pytest.mark.parametrize('domain,attribute,supported', [
(fan.DOMAIN, 'water_level', True),
(humidifier.DOMAIN, 'water_level', True),
])
async def test_property_float_water_level(hass, domain, attribute, supported):
state = State(f'{domain}.test', STATE_ON, {attribute: '90'})
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_EVENT, const.FLOAT_INSTANCE_WATER_LEVEL)
if supported:
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_WATER_LEVEL)
else:
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_WATER_LEVEL)
return
assert prop.retrievable
assert prop.parameters() == {'instance': 'water_level', 'unit': 'unit.percent'}
assert prop.get_value() == 90
for v in [-5, 200]:
prop.state = State(f'{domain}.test', STATE_ON, {attribute: v})
assert prop.get_value() == 0
prop.state = State(f'{domain}.test', STATE_ON, {attribute: None})
assert prop.get_value() is None
@pytest.mark.parametrize('domain,device_class,attribute,supported', [
(sensor.DOMAIN, SensorDeviceClass.CO2, None, True),
(sensor.DOMAIN, None, None, False),
(air_quality.DOMAIN, None, air_quality.ATTR_CO2, True),
(air_quality.DOMAIN, None, None, False),
(fan.DOMAIN, None, air_quality.ATTR_CO2, True),
(fan.DOMAIN, None, None, False),
])
async def test_property_float_co2(hass, domain, device_class, attribute, supported):
attributes = {}
value = STATE_ON
if attribute is None:
value = 643
else:
attributes[attribute] = 643
if device_class:
attributes[ATTR_DEVICE_CLASS] = device_class
state = State(f'{domain}.test', value, attributes)
if supported:
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_CO2_LEVEL)
else:
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_CO2_LEVEL)
return
assert prop.retrievable
assert prop.parameters() == {'instance': 'co2_level', 'unit': 'unit.ppm'}
assert prop.get_value() == 643
if attribute is None:
value = -5
else:
attributes[attribute] = -5
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() == 0
if attribute is None:
value = STATE_UNKNOWN
else:
attributes[attribute] = None
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() is None
@pytest.mark.parametrize('attribute,instance', [
(air_quality.ATTR_PM_0_1, const.FLOAT_INSTANCE_PM1_DENSITY),
(air_quality.ATTR_PM_2_5, const.FLOAT_INSTANCE_PM2_5_DENSITY),
(air_quality.ATTR_PM_10, const.FLOAT_INSTANCE_PM10_DENSITY),
])
async def test_property_float_pm(hass, attribute, instance):
state = State('air_quality.test', STATE_ON, {attribute: 300})
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, instance)
assert prop.retrievable
assert prop.parameters() == {'instance': instance, 'unit': 'unit.density.mcg_m3'}
assert prop.get_value() == 300
prop.state = State('air_quality.test', STATE_ON, {attribute: -5})
assert prop.get_value() == 0
prop.state = State('air_quality.test', STATE_ON, {attribute: None})
assert prop.get_value() is None
@pytest.mark.parametrize('unit,v', [
('ppb', 134.89),
('ppm', 134888.81),
('µg/m³', 30),
('mg/m³', 30000),
('μg/ft³', 1059.44),
('unsupported', 30),
])
async def test_property_float_tvoc(hass, unit, v):
state = State('air_quality.test', STATE_ON,)
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_TVOC)
state = State('air_quality.test', STATE_ON, {
'total_volatile_organic_compounds': 30,
ATTR_UNIT_OF_MEASUREMENT: unit
})
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_TVOC)
assert prop.retrievable
assert prop.parameters() == {'instance': const.FLOAT_INSTANCE_TVOC, 'unit': 'unit.density.mcg_m3'}
assert prop.get_value() == v
prop.state = State('air_quality.test', STATE_ON, {'total_volatile_organic_compounds': -5})
assert prop.get_value() == 0
prop.state = State('air_quality.test', STATE_ON, {'total_volatile_organic_compounds': None})
assert prop.get_value() is None
@pytest.mark.parametrize('unit,v', [
('A', 1245),
('mA', 1.245),
])
async def test_property_float_amperage_value(hass, unit, v):
state = State('switch.test', STATE_ON, {
'current': 1245,
ATTR_UNIT_OF_MEASUREMENT: unit
})
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_AMPERAGE)
assert prop.retrievable
assert prop.parameters() == {'instance': const.FLOAT_INSTANCE_AMPERAGE, 'unit': 'unit.ampere'}
assert prop.get_value() == v
@pytest.mark.parametrize('domain,device_class,attribute,instance,unit,supported', [
(binary_sensor.DOMAIN, SensorDeviceClass.VOLTAGE, None, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', False),
(sensor.DOMAIN, SensorDeviceClass.VOLTAGE, None, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', True),
(sensor.DOMAIN, None, None, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', False),
(switch.DOMAIN, None, ATTR_VOLTAGE, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', True),
(switch.DOMAIN, None, None, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', False),
(light.DOMAIN, None, ATTR_VOLTAGE, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', True),
(light.DOMAIN, None, None, const.FLOAT_INSTANCE_VOLTAGE, 'unit.volt', False),
(binary_sensor.DOMAIN, SensorDeviceClass.CURRENT, None, const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', False),
(sensor.DOMAIN, SensorDeviceClass.CURRENT, None, const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', True),
(sensor.DOMAIN, None, None, const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', False),
(switch.DOMAIN, None, 'current', const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', True),
(switch.DOMAIN, None, None, const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', False),
(light.DOMAIN, None, 'current', const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', True),
(light.DOMAIN, None, None, const.FLOAT_INSTANCE_AMPERAGE, 'unit.ampere', False),
(sensor.DOMAIN, SensorDeviceClass.POWER, None, const.FLOAT_INSTANCE_POWER, 'unit.watt', True),
(sensor.DOMAIN, None, None, const.FLOAT_INSTANCE_POWER, 'unit.watt', False),
(switch.DOMAIN, None, 'power', const.FLOAT_INSTANCE_POWER, 'unit.watt', True),
(switch.DOMAIN, None, 'load_power', const.FLOAT_INSTANCE_POWER, 'unit.watt', True),
(switch.DOMAIN, None, None, const.FLOAT_INSTANCE_POWER, 'unit.watt', False),
])
async def test_property_float_simple(hass, domain, device_class, attribute, instance: str, unit, supported):
attributes = {}
value = STATE_ON
if attribute is None:
value = 220
else:
attributes[attribute] = 220
if device_class:
attributes[ATTR_DEVICE_CLASS] = device_class
state = State(f'{domain}.test', value, attributes)
if supported:
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, instance)
else:
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, instance)
return
assert prop.retrievable
assert prop.parameters() == {'instance': instance, 'unit': unit}
assert prop.get_value() == 220
if attribute is None:
value = -5
else:
attributes[attribute] = -5
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() == 0
if attribute is None:
value = STATE_UNKNOWN
else:
attributes[attribute] = None
prop.state = State(f'{domain}.test', value, attributes)
assert prop.get_value() is None
@pytest.mark.parametrize('domain', [switch.DOMAIN, sensor.DOMAIN, light.DOMAIN, cover.DOMAIN])
async def test_property_float_battery_class(hass, domain):
state = State(f'{domain}.test', '50', {
ATTR_DEVICE_CLASS: SensorDeviceClass.BATTERY
})
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_EVENT, const.FLOAT_INSTANCE_BATTERY_LEVEL)
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_BATTERY_LEVEL)
state = State(f'{domain}.test', '50', {
ATTR_DEVICE_CLASS: SensorDeviceClass.BATTERY,
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE
})
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_BATTERY_LEVEL)
assert prop.retrievable
assert prop.parameters() == {'instance': 'battery_level', 'unit': 'unit.percent'}
assert prop.get_value() == 50
prop.state = State(f'{domain}.test', STATE_UNKNOWN, {
ATTR_DEVICE_CLASS: SensorDeviceClass.BATTERY,
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE
})
assert prop.get_value() is None
for s in ['low', 'charging', -5, 200]:
prop.state = State(f'{domain}.test', s, {
ATTR_DEVICE_CLASS: SensorDeviceClass.BATTERY,
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE
})
assert prop.get_value() == 0
@pytest.mark.parametrize('domain', [switch.DOMAIN, sensor.DOMAIN, light.DOMAIN, cover.DOMAIN])
async def test_property_float_battery_attr(hass, domain):
state = State(f'{domain}.test', STATE_ON, {
ATTR_BATTERY_LEVEL: 50
})
assert_no_properties(hass, BASIC_CONFIG, state, PROPERTY_EVENT, const.FLOAT_INSTANCE_BATTERY_LEVEL)
prop = get_exact_one_property(hass, BASIC_CONFIG, state, PROPERTY_FLOAT, const.FLOAT_INSTANCE_BATTERY_LEVEL)
assert prop.retrievable
assert prop.parameters() == {'instance': 'battery_level', 'unit': 'unit.percent'}
assert prop.get_value() == 50
prop.state = State(f'{domain}.test', STATE_ON, {
ATTR_BATTERY_LEVEL: None
})
assert prop.get_value() is None
for s in ['low', 'charging', -5, 200]:
prop.state = State(f'{domain}.test', STATE_ON, {
ATTR_BATTERY_LEVEL: s
})
assert prop.get_value() == 0