|
1 | 1 | """The test for the label_state binary sensor platform.""" |
2 | 2 |
|
| 3 | +import pytest |
3 | 4 | from homeassistant.core import HomeAssistant |
4 | 5 | from homeassistant.helpers import entity_registry as er |
| 6 | +from homeassistant.helpers import label_registry as lr |
5 | 7 | from homeassistant.setup import async_setup_component |
| 8 | +from pytest_homeassistant_custom_component.common import MockConfigEntry |
6 | 9 |
|
| 10 | +from . import setup_integration |
| 11 | + |
| 12 | +VALUES_STATE1 = ["on", "off", "unavailable"] |
| 13 | +VALUES_STATE2 = ["on", "off", "unavailable"] |
7 | 14 | VALUES_NUMERIC = [17, 20, 15.2, 5, 3.8, 9.2, 6.7, 14, 6] |
8 | 15 | VALUES_ERROR = [17, "string", 15.3] |
9 | 16 | COUNT = len(VALUES_NUMERIC) |
|
12 | 19 | LAST_VALUE = VALUES_NUMERIC[-1] |
13 | 20 |
|
14 | 21 |
|
15 | | -# async def test_min_sensor( |
16 | | -# hass: HomeAssistant, entity_registry: er.EntityRegistry |
17 | | -# ) -> None: |
18 | | -# """Test the min sensor.""" |
19 | | -# sensor_entity_entry = entity_registry.async_get_or_create( |
20 | | -# "sensor", "test_1", "unique", suggested_object_id="test_1" |
21 | | -# ) |
22 | | -# assert sensor_entity_entry.entity_id == "sensor.test_1" |
23 | | - |
24 | | -# config = { |
25 | | -# "sensor": { |
26 | | -# "platform": "label_state", |
27 | | -# "name": "test_min", |
28 | | -# "type": "min", |
29 | | -# "entity_id": "sensor.test_1", |
30 | | -# "unique_id": "very_unique_id", |
31 | | -# } |
32 | | -# } |
33 | | - |
34 | | -# assert await async_setup_component(hass, "sensor", config) |
35 | | -# await hass.async_block_till_done() |
36 | | - |
37 | | -# for value in VALUES_NUMERIC: |
38 | | -# hass.states.async_set(sensor_entity_entry.entity_id, value) |
39 | | -# await hass.async_block_till_done() |
40 | | - |
41 | | -# state = hass.states.get("sensor.test_min") |
42 | | - |
43 | | -# assert str(float(MIN_VALUE)) == state.state |
44 | | - |
45 | | -# entity = entity_registry.async_get("sensor.test_min") |
46 | | -# assert entity.unique_id == "very_unique_id" |
47 | | - |
48 | | - |
49 | | -# async def test_max_sensor( |
50 | | -# hass: HomeAssistant, entity_registry: er.EntityRegistry |
51 | | -# ) -> None: |
52 | | -# """Test the max sensor.""" |
53 | | -# sensor_entity_entry = entity_registry.async_get_or_create( |
54 | | -# "sensor", "test_1", "unique", suggested_object_id="test_1" |
55 | | -# ) |
56 | | -# assert sensor_entity_entry.entity_id == "sensor.test_1" |
57 | | - |
58 | | -# config = { |
59 | | -# "sensor": { |
60 | | -# "platform": "label_state", |
61 | | -# "name": "test_max", |
62 | | -# "type": "max", |
63 | | -# "entity_id": "sensor.test_1", |
64 | | -# "unique_id": "very_unique_id", |
65 | | -# } |
66 | | -# } |
67 | | - |
68 | | -# assert await async_setup_component(hass, "sensor", config) |
69 | | -# await hass.async_block_till_done() |
70 | | - |
71 | | -# for value in VALUES_NUMERIC: |
72 | | -# hass.states.async_set(sensor_entity_entry.entity_id, value) |
73 | | -# await hass.async_block_till_done() |
74 | | - |
75 | | -# state = hass.states.get("sensor.test_max") |
76 | | - |
77 | | -# assert str(float(MAX_VALUE)) == state.state |
78 | | -# assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT |
79 | | - |
80 | | -# entity = entity_registry.async_get("sensor.test_max") |
81 | | -# assert entity.unique_id == "very_unique_id" |
82 | | - |
83 | | - |
84 | | -# async def test_value_error( |
85 | | -# hass: HomeAssistant, entity_registry: er.EntityRegistry |
86 | | -# ) -> None: |
87 | | -# """Test value error.""" |
88 | | -# sensor_entity_entry = entity_registry.async_get_or_create( |
89 | | -# "sensor", "test_1", "unique", suggested_object_id="test_1" |
90 | | -# ) |
91 | | -# assert sensor_entity_entry.entity_id == "sensor.test_1" |
92 | | - |
93 | | -# config = { |
94 | | -# "sensor": { |
95 | | -# "platform": "label_state", |
96 | | -# "name": "test_max", |
97 | | -# "type": "max", |
98 | | -# "entity_id": "sensor.test_1", |
99 | | -# "unique_id": "very_unique_id", |
100 | | -# } |
101 | | -# } |
102 | | - |
103 | | -# assert await async_setup_component(hass, "sensor", config) |
104 | | -# await hass.async_block_till_done() |
105 | | - |
106 | | -# for value in VALUES_ERROR: |
107 | | -# hass.states.async_set(sensor_entity_entry.entity_id, value) |
108 | | -# await hass.async_block_till_done() |
109 | | - |
110 | | -# state = hass.states.get("sensor.test_max") |
111 | | - |
112 | | -# assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT |
113 | | - |
114 | | -# entity = entity_registry.async_get("sensor.test_max") |
115 | | -# assert entity.unique_id == "very_unique_id" |
| 22 | +@pytest.mark.parametrize( |
| 23 | + ( |
| 24 | + "state_1", |
| 25 | + "state_2", |
| 26 | + "expected_state", |
| 27 | + ), |
| 28 | + [ |
| 29 | + ( |
| 30 | + "unavailable", |
| 31 | + "on", |
| 32 | + "on", |
| 33 | + ), |
| 34 | + ( |
| 35 | + "on", |
| 36 | + "on", |
| 37 | + "off", |
| 38 | + ), |
| 39 | + ], |
| 40 | +) |
| 41 | +async def test_state_sensor( |
| 42 | + hass: HomeAssistant, |
| 43 | + state_1: str, |
| 44 | + state_2: str, |
| 45 | + expected_state: str, |
| 46 | + entity_registry: er.EntityRegistry, |
| 47 | + label_registry: lr.LabelRegistry, |
| 48 | +) -> None: |
| 49 | + """Test the state sensor.""" |
| 50 | + |
| 51 | + test_label = label_registry.async_create( |
| 52 | + "test", |
| 53 | + ) |
| 54 | + |
| 55 | + sensor1_entity_entry = entity_registry.async_get_or_create( |
| 56 | + "sensor", "test_1", "unique", suggested_object_id="test_1" |
| 57 | + ) |
| 58 | + sensor1_entity_entry.labels.add(test_label.label_id) |
| 59 | + assert sensor1_entity_entry.entity_id == "sensor.test_1" |
| 60 | + |
| 61 | + sensor2_entity_entry = entity_registry.async_get_or_create( |
| 62 | + "sensor", "test_2", "unique", suggested_object_id="test_2" |
| 63 | + ) |
| 64 | + sensor2_entity_entry.labels.add(test_label.label_id) |
| 65 | + assert sensor2_entity_entry.entity_id == "sensor.test_2" |
| 66 | + |
| 67 | + config = MockConfigEntry( |
| 68 | + domain="label_state", |
| 69 | + data={}, |
| 70 | + options={ |
| 71 | + "name": "test_state", |
| 72 | + "label": test_label.label_id, |
| 73 | + "state_type": "state", |
| 74 | + "state_to": "unavailable", |
| 75 | + }, |
| 76 | + title="test_state", |
| 77 | + ) |
| 78 | + |
| 79 | + await setup_integration(hass, config) |
| 80 | + await hass.async_block_till_done() |
| 81 | + |
| 82 | + hass.states.async_set(sensor1_entity_entry.entity_id, state_1) |
| 83 | + await hass.async_block_till_done() |
| 84 | + |
| 85 | + hass.states.async_set(sensor2_entity_entry.entity_id, state_2) |
| 86 | + await hass.async_block_till_done() |
| 87 | + |
| 88 | + state = hass.states.get("binary_sensor.test_state") |
| 89 | + |
| 90 | + assert state is not None |
| 91 | + assert state.state == expected_state |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize( |
| 95 | + ( |
| 96 | + "state_1", |
| 97 | + "state_2", |
| 98 | + "state_lower_limit", |
| 99 | + "state_upper_limit", |
| 100 | + "expected_state", |
| 101 | + ), |
| 102 | + [ |
| 103 | + ("11", "12", "10", "20", "off"), |
| 104 | + ("1", "12", "10", "20", "on"), |
| 105 | + ], |
| 106 | +) |
| 107 | +async def test_numeric_state_sensor( |
| 108 | + hass: HomeAssistant, |
| 109 | + state_1: str, |
| 110 | + state_2: str, |
| 111 | + state_lower_limit: str, |
| 112 | + state_upper_limit: str, |
| 113 | + expected_state: str, |
| 114 | + entity_registry: er.EntityRegistry, |
| 115 | + label_registry: lr.LabelRegistry, |
| 116 | +) -> None: |
| 117 | + """Test the state sensor.""" |
| 118 | + |
| 119 | + test_label = label_registry.async_create( |
| 120 | + "test", |
| 121 | + ) |
| 122 | + |
| 123 | + sensor1_entity_entry = entity_registry.async_get_or_create( |
| 124 | + "sensor", "test_1", "unique", suggested_object_id="test_1" |
| 125 | + ) |
| 126 | + sensor1_entity_entry.labels.add(test_label.label_id) |
| 127 | + assert sensor1_entity_entry.entity_id == "sensor.test_1" |
| 128 | + assert test_label.label_id in sensor1_entity_entry.labels |
| 129 | + |
| 130 | + sensor2_entity_entry = entity_registry.async_get_or_create( |
| 131 | + "sensor", "test_2", "unique", suggested_object_id="test_2" |
| 132 | + ) |
| 133 | + sensor2_entity_entry.labels.add(test_label.label_id) |
| 134 | + assert sensor2_entity_entry.entity_id == "sensor.test_2" |
| 135 | + |
| 136 | + config = MockConfigEntry( |
| 137 | + domain="label_state", |
| 138 | + data={}, |
| 139 | + options={ |
| 140 | + "name": "test_numeric_state", |
| 141 | + "label": test_label.label_id, |
| 142 | + "state_type": "numeric_state", |
| 143 | + "state_lower_limit": state_lower_limit, |
| 144 | + "state_upper_limit": state_upper_limit, |
| 145 | + }, |
| 146 | + title="test_numeric_state", |
| 147 | + ) |
| 148 | + |
| 149 | + config.add_to_hass(hass) |
| 150 | + # await setup_integration(hass, config) |
| 151 | + assert await hass.config_entries.async_setup(config.entry_id) |
| 152 | + await hass.async_block_till_done() |
| 153 | + |
| 154 | + hass.states.async_set(sensor1_entity_entry.entity_id, state_1) |
| 155 | + await hass.async_block_till_done() |
| 156 | + |
| 157 | + hass.states.async_set(sensor2_entity_entry.entity_id, state_2) |
| 158 | + await hass.async_block_till_done() |
| 159 | + |
| 160 | + state = hass.states.get("binary_sensor.test_numeric_state") |
| 161 | + |
| 162 | + assert state is not None |
| 163 | + assert state.state == expected_state |
0 commit comments