/
test_sensor.py
425 lines (347 loc) · 13.8 KB
/
test_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
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
import logging
import pytest
from homeassistant.components import light
from homeassistant.components.integration.sensor import ATTR_SOURCE_ID
from homeassistant.components.light import ColorMode
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.utility_meter.sensor import ATTR_PERIOD, DAILY, HOURLY
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_FRIENDLY_NAME,
ATTR_UNIT_OF_MEASUREMENT,
CONF_ENTITIES,
CONF_ENTITY_ID,
CONF_PLATFORM,
CONF_UNIQUE_ID,
ENERGY_KILO_WATT_HOUR,
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.area_registry import AreaRegistry
from homeassistant.helpers.device_registry import DeviceRegistry
from homeassistant.helpers.entity_registry import EntityRegistry
from homeassistant.setup import async_setup_component
import custom_components.test.light as test_light_platform
from custom_components.powercalc.const import (
ATTR_CALCULATION_MODE,
ATTR_ENTITIES,
ATTR_SOURCE_ENTITY,
CONF_AREA,
CONF_CREATE_GROUP,
CONF_CREATE_UTILITY_METERS,
CONF_ENABLE_AUTODISCOVERY,
CONF_ENERGY_SENSOR_FRIENDLY_NAMING,
CONF_ENERGY_SENSOR_NAMING,
CONF_FIXED,
CONF_GROUP,
CONF_INCLUDE,
CONF_MANUFACTURER,
CONF_MODE,
CONF_MODEL,
CONF_POWER,
CONF_POWER_SENSOR_FRIENDLY_NAMING,
CONF_POWER_SENSOR_NAMING,
CONF_UTILITY_METER_TYPES,
DOMAIN,
CalculationStrategy,
)
from custom_components.test.light import MockLight
from .common import (
create_discoverable_light,
create_input_boolean,
create_input_booleans,
create_mock_light_entity,
get_simple_fixed_config,
run_powercalc_setup_yaml_config,
)
async def test_fixed_power_sensor_from_yaml(hass: HomeAssistant):
await create_input_boolean(hass)
await run_powercalc_setup_yaml_config(
hass,
get_simple_fixed_config("input_boolean.test"),
)
state = hass.states.get("sensor.test_power")
assert state.state == "0.00"
hass.states.async_set("input_boolean.test", STATE_ON)
await hass.async_block_till_done()
power_state = hass.states.get("sensor.test_power")
assert power_state.state == "50.00"
assert (
power_state.attributes.get(ATTR_CALCULATION_MODE) == CalculationStrategy.FIXED
)
assert power_state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
energy_state = hass.states.get("sensor.test_energy")
assert energy_state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
assert (
energy_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
)
assert energy_state.attributes.get(ATTR_SOURCE_ID) == "sensor.test_power"
assert energy_state.attributes.get(ATTR_SOURCE_ENTITY) == "input_boolean.test"
async def test_utility_meter_is_created(hass: HomeAssistant):
"""Test that utility meters are succesfully created when `create_utility_meter: true`"""
await create_input_boolean(hass)
await run_powercalc_setup_yaml_config(
hass,
{
CONF_ENTITY_ID: "input_boolean.test",
CONF_MODE: CalculationStrategy.FIXED,
CONF_CREATE_UTILITY_METERS: True,
CONF_UTILITY_METER_TYPES: [DAILY, HOURLY],
CONF_FIXED: {CONF_POWER: 50},
},
)
daily_state = hass.states.get("sensor.test_energy_daily")
assert daily_state.attributes.get(ATTR_SOURCE_ID) == "sensor.test_energy"
assert daily_state.attributes.get(ATTR_PERIOD) == DAILY
hourly_state = hass.states.get("sensor.test_energy_hourly")
assert hourly_state
assert hourly_state.attributes.get(ATTR_SOURCE_ID) == "sensor.test_energy"
assert hourly_state.attributes.get(ATTR_PERIOD) == HOURLY
monthly_state = hass.states.get("sensor.test_energy_monthly")
assert not monthly_state
async def test_create_nested_group_sensor(hass: HomeAssistant):
await create_input_booleans(hass, ["test", "test1", "test2"])
await run_powercalc_setup_yaml_config(
hass,
{
CONF_CREATE_GROUP: "TestGroup1",
CONF_ENTITIES: [
get_simple_fixed_config("input_boolean.test", 50),
get_simple_fixed_config("input_boolean.test1", 50),
{
CONF_CREATE_GROUP: "TestGroup2",
CONF_ENTITIES: [
get_simple_fixed_config("input_boolean.test2", 50),
],
},
],
},
)
hass.states.async_set("input_boolean.test", STATE_ON)
hass.states.async_set("input_boolean.test1", STATE_ON)
hass.states.async_set("input_boolean.test2", STATE_ON)
await hass.async_block_till_done()
group1 = hass.states.get("sensor.testgroup1_power")
assert group1.attributes[ATTR_ENTITIES] == {
"sensor.test_power",
"sensor.test1_power",
"sensor.test2_power",
}
assert group1.state == "150.00"
group2 = hass.states.get("sensor.testgroup2_power")
assert group2.attributes[ATTR_ENTITIES] == {
"sensor.test2_power",
}
assert group2.state == "50.00"
hass.states.async_set("input_boolean.test2", STATE_OFF)
await hass.async_block_till_done()
group1 = hass.states.get("sensor.testgroup1_power")
assert group1.state == "100.00"
group2 = hass.states.get("sensor.testgroup2_power")
assert group2.state == "0.00"
async def test_light_lut_strategy(hass: HomeAssistant):
light_entity = test_light_platform.MockLight(
"test1",
STATE_ON,
unique_id="dsafbwq",
)
light_entity.supported_color_modes = {light.ColorMode.BRIGHTNESS}
light_entity.color_mode = light.ColorMode.BRIGHTNESS
light_entity.brightness = 125
light_entity.manufacturer = "signify"
light_entity.model = "LWB010"
(light_entity_id, __) = await create_mock_light_entity(hass, light_entity)
await run_powercalc_setup_yaml_config(
hass, {CONF_PLATFORM: DOMAIN, CONF_ENTITY_ID: light_entity_id}
)
state = hass.states.get("sensor.test1_power")
assert state
assert state.state == "2.67"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert state.attributes.get(ATTR_CALCULATION_MODE) == CalculationStrategy.LUT
assert state.attributes.get(ATTR_SOURCE_ENTITY) == light_entity_id
async def test_error_when_configuring_same_entity_twice(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
):
caplog.set_level(logging.ERROR)
await create_input_boolean(hass)
await run_powercalc_setup_yaml_config(
hass,
[
get_simple_fixed_config("input_boolean.test", 50),
get_simple_fixed_config("input_boolean.test", 100),
],
)
assert "This entity has already configured a power sensor" in caplog.text
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
async def test_alternate_naming_strategy(hass: HomeAssistant):
await create_input_boolean(hass)
await run_powercalc_setup_yaml_config(
hass,
[
get_simple_fixed_config("input_boolean.test", 50),
],
{
CONF_POWER_SENSOR_NAMING: "{} Power consumption",
CONF_POWER_SENSOR_FRIENDLY_NAMING: "{} Power friendly",
CONF_ENERGY_SENSOR_NAMING: "{} Energy kwh",
CONF_ENERGY_SENSOR_FRIENDLY_NAMING: "{} Energy friendly",
},
)
power_state = hass.states.get("sensor.test_power_consumption")
assert power_state
assert power_state.attributes.get(ATTR_FRIENDLY_NAME) == "test Power friendly"
energy_state = hass.states.get("sensor.test_energy_kwh")
assert energy_state
assert energy_state.attributes.get(ATTR_FRIENDLY_NAME) == "test Energy friendly"
async def test_can_create_same_entity_twice_with_unique_id(hass: HomeAssistant):
await create_input_boolean(hass)
await run_powercalc_setup_yaml_config(
hass,
[
{
CONF_ENTITY_ID: "input_boolean.test",
CONF_UNIQUE_ID: "111",
CONF_MODE: CalculationStrategy.FIXED,
CONF_FIXED: {CONF_POWER: 50},
},
{
CONF_ENTITY_ID: "input_boolean.test",
CONF_UNIQUE_ID: "222",
CONF_MODE: CalculationStrategy.FIXED,
CONF_FIXED: {CONF_POWER: 100},
},
],
)
assert hass.states.get("sensor.test_power")
assert hass.states.get("sensor.test_energy")
assert hass.states.get("sensor.test_power_2")
assert hass.states.get("sensor.test_energy_2")
async def test_unsupported_model_is_skipped_from_autodiscovery(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
):
light = test_light_platform.MockLight("test", STATE_ON)
light.manufacturer = "lidl"
light.model = "non_existing_model"
await create_mock_light_entity(hass, light)
# Run powercalc setup with autodiscovery
await run_powercalc_setup_yaml_config(hass, {}, {})
assert "Model not found in library, skipping auto configuration" in caplog.text
async def test_can_include_autodiscovered_entity_in_group(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
):
"""Test that models are automatically discovered and power sensors created"""
caplog.set_level(logging.ERROR)
await create_mock_light_entity(hass, create_discoverable_light("testa"))
hass.states.async_set(
"light.testa", STATE_ON, {"brightness": 125, "color_mode": ColorMode.BRIGHTNESS}
)
await hass.async_block_till_done()
await run_powercalc_setup_yaml_config(
hass,
[
{
CONF_CREATE_GROUP: "GroupA",
CONF_ENTITIES: [{CONF_ENTITY_ID: "light.testa"}],
},
],
{CONF_ENABLE_AUTODISCOVERY: True},
)
assert len(caplog.records) == 0
assert hass.states.get("sensor.testa_power")
group_state = hass.states.get("sensor.groupa_power")
assert group_state.attributes.get(ATTR_ENTITIES) == {"sensor.testa_power"}
async def test_include_area(
hass: HomeAssistant, entity_reg: EntityRegistry, area_reg: AreaRegistry
):
await create_mock_light_entity(hass, create_discoverable_light("bathroom_mirror"))
area = area_reg.async_get_or_create("Bathroom 1")
await hass.async_block_till_done()
entity_reg.async_update_entity("light.bathroom_mirror", area_id=area.id)
await hass.async_block_till_done()
await run_powercalc_setup_yaml_config(
hass,
{CONF_CREATE_GROUP: "Test include", CONF_INCLUDE: {CONF_AREA: "bathroom_1"}},
)
group_state = hass.states.get("sensor.test_include_power")
assert group_state
assert group_state.attributes.get(ATTR_ENTITIES) == {"sensor.bathroom_mirror_power"}
await run_powercalc_setup_yaml_config(
hass,
{
CONF_CREATE_GROUP: "Test include area by name",
CONF_INCLUDE: {CONF_AREA: "Bathroom 1"},
},
)
assert hass.states.get("sensor.test_include_area_by_name_power")
async def test_include_area_not_found(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
):
caplog.set_level(logging.ERROR)
await run_powercalc_setup_yaml_config(
hass,
{
CONF_CREATE_GROUP: "Test area not found",
CONF_INCLUDE: {CONF_AREA: "hallway"},
},
)
assert "No area with id or name" in caplog.text
async def test_include_light_group(hass: HomeAssistant):
discoverable_light = create_discoverable_light("bathroom_mirror")
non_discoverable_light = MockLight("bathroom_spots")
await create_mock_light_entity(hass, [discoverable_light, non_discoverable_light])
# Ugly hack, maybe I can figure out something better in the future.
# Light domain is already setup for platform test, remove the component so we can setup light group
if light.DOMAIN in hass.config.components:
hass.config.components.remove(light.DOMAIN)
await async_setup_component(
hass,
light.DOMAIN,
{
light.DOMAIN: {
"platform": "group",
"name": "Bathroom",
"entities": ["light.bathroom_mirror", "light.bathroom_spots"],
}
},
)
await hass.async_block_till_done()
await run_powercalc_setup_yaml_config(
hass,
{
CONF_CREATE_GROUP: "Test include lightgroup",
CONF_INCLUDE: {CONF_GROUP: "light.bathroom"},
},
)
await hass.async_start()
await hass.async_block_till_done()
group_state = hass.states.get("sensor.test_include_lightgroup_power")
assert group_state
assert group_state.attributes.get(ATTR_ENTITIES) == {"sensor.bathroom_mirror_power"}
async def test_user_can_rename_entity_id(hass: HomeAssistant, entity_reg: EntityRegistry):
"""
When the power/energy sensors exist already with an unique ID, don't change the entity ID
This allows the users to change the entity ID's from the GUI
"""
entity_reg.async_get_or_create("sensor", DOMAIN, "abcdef", suggested_object_id="my_renamed_power")
entity_reg.async_get_or_create("sensor", DOMAIN, "abcdef_energy", suggested_object_id="my_renamed_energy")
await hass.async_block_till_done()
await create_input_boolean(hass)
await run_powercalc_setup_yaml_config(
hass,
{
CONF_ENTITY_ID: "input_boolean.test",
CONF_MODE: CalculationStrategy.FIXED,
CONF_UNIQUE_ID: "abcdef",
CONF_FIXED: {CONF_POWER: 40},
}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.my_renamed_power")
assert not hass.states.get("sensor.test_power")
energy_state = hass.states.get("sensor.my_renamed_energy")
assert energy_state
assert energy_state.attributes.get("source") == "sensor.my_renamed_power"
assert not hass.states.get("sensor.test_energy")