|
1 | 1 | """Test setting up sensors.""" |
2 | 2 | from datetime import timedelta |
3 | 3 |
|
4 | | -from homeassistant.components.iotawatt.const import ATTR_LAST_UPDATE |
5 | 4 | from homeassistant.components.sensor import ( |
6 | 5 | ATTR_STATE_CLASS, |
7 | 6 | SensorDeviceClass, |
|
14 | 13 | UnitOfEnergy, |
15 | 14 | UnitOfPower, |
16 | 15 | ) |
17 | | -from homeassistant.core import State |
18 | 16 | from homeassistant.setup import async_setup_component |
19 | 17 | from homeassistant.util.dt import utcnow |
20 | 18 |
|
21 | | -from . import ( |
22 | | - INPUT_ACCUMULATED_SENSOR, |
23 | | - INPUT_SENSOR, |
24 | | - OUTPUT_ACCUMULATED_SENSOR, |
25 | | - OUTPUT_SENSOR, |
26 | | -) |
| 19 | +from . import INPUT_SENSOR, OUTPUT_SENSOR |
27 | 20 |
|
28 | | -from tests.common import async_fire_time_changed, mock_restore_cache |
| 21 | +from tests.common import async_fire_time_changed |
29 | 22 |
|
30 | 23 |
|
31 | 24 | async def test_sensor_type_input(hass, mock_iotawatt): |
@@ -83,161 +76,3 @@ async def test_sensor_type_output(hass, mock_iotawatt): |
83 | 76 | await hass.async_block_till_done() |
84 | 77 |
|
85 | 78 | assert hass.states.get("sensor.my_watthour_sensor") is None |
86 | | - |
87 | | - |
88 | | -async def test_sensor_type_accumulated_output(hass, mock_iotawatt): |
89 | | - """Tests the sensor type of Accumulated Output and that it's properly restored from saved state.""" |
90 | | - mock_iotawatt.getSensors.return_value["sensors"][ |
91 | | - "my_watthour_accumulated_output_sensor_key" |
92 | | - ] = OUTPUT_ACCUMULATED_SENSOR |
93 | | - |
94 | | - DUMMY_DATE = "2021年09月01日T14:00:00+10:00" |
95 | | - |
96 | | - mock_restore_cache( |
97 | | - hass, |
98 | | - ( |
99 | | - State( |
100 | | - "sensor.my_watthour_accumulated_output_sensor_wh_accumulated", |
101 | | - "100.0", |
102 | | - { |
103 | | - "device_class": SensorDeviceClass.ENERGY, |
104 | | - "unit_of_measurement": UnitOfEnergy.WATT_HOUR, |
105 | | - "last_update": DUMMY_DATE, |
106 | | - }, |
107 | | - ), |
108 | | - ), |
109 | | - ) |
110 | | - |
111 | | - assert await async_setup_component(hass, "iotawatt", {}) |
112 | | - await hass.async_block_till_done() |
113 | | - |
114 | | - assert len(hass.states.async_entity_ids()) == 1 |
115 | | - |
116 | | - state = hass.states.get( |
117 | | - "sensor.my_watthour_accumulated_output_sensor_wh_accumulated" |
118 | | - ) |
119 | | - assert state is not None |
120 | | - |
121 | | - assert state.state == "300.0" # 100 + 200 |
122 | | - assert ( |
123 | | - state.attributes[ATTR_FRIENDLY_NAME] |
124 | | - == "My WattHour Accumulated Output Sensor.wh Accumulated" |
125 | | - ) |
126 | | - assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL |
127 | | - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR |
128 | | - assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY |
129 | | - assert state.attributes["type"] == "Output" |
130 | | - assert state.attributes[ATTR_LAST_UPDATE] is not None |
131 | | - assert state.attributes[ATTR_LAST_UPDATE] != DUMMY_DATE |
132 | | - |
133 | | - |
134 | | -async def test_sensor_type_accumulated_output_error_restore(hass, mock_iotawatt): |
135 | | - """Tests the sensor type of Accumulated Output and that it's properly restored from saved state.""" |
136 | | - mock_iotawatt.getSensors.return_value["sensors"][ |
137 | | - "my_watthour_accumulated_output_sensor_key" |
138 | | - ] = OUTPUT_ACCUMULATED_SENSOR |
139 | | - |
140 | | - DUMMY_DATE = "2021年09月01日T14:00:00+10:00" |
141 | | - |
142 | | - mock_restore_cache( |
143 | | - hass, |
144 | | - ( |
145 | | - State( |
146 | | - "sensor.my_watthour_accumulated_output_sensor_wh_accumulated", |
147 | | - "unknown", |
148 | | - ), |
149 | | - ), |
150 | | - ) |
151 | | - |
152 | | - assert await async_setup_component(hass, "iotawatt", {}) |
153 | | - await hass.async_block_till_done() |
154 | | - |
155 | | - assert len(hass.states.async_entity_ids()) == 1 |
156 | | - |
157 | | - state = hass.states.get( |
158 | | - "sensor.my_watthour_accumulated_output_sensor_wh_accumulated" |
159 | | - ) |
160 | | - assert state is not None |
161 | | - |
162 | | - assert state.state == "200.0" # Returns the new read as restore failed. |
163 | | - assert ( |
164 | | - state.attributes[ATTR_FRIENDLY_NAME] |
165 | | - == "My WattHour Accumulated Output Sensor.wh Accumulated" |
166 | | - ) |
167 | | - assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL |
168 | | - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR |
169 | | - assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY |
170 | | - assert state.attributes["type"] == "Output" |
171 | | - assert state.attributes[ATTR_LAST_UPDATE] is not None |
172 | | - assert state.attributes[ATTR_LAST_UPDATE] != DUMMY_DATE |
173 | | - |
174 | | - |
175 | | -async def test_sensor_type_multiple_accumulated_output(hass, mock_iotawatt): |
176 | | - """Tests the sensor type of Accumulated Output and that it's properly restored from saved state.""" |
177 | | - mock_iotawatt.getSensors.return_value["sensors"][ |
178 | | - "my_watthour_accumulated_output_sensor_key" |
179 | | - ] = OUTPUT_ACCUMULATED_SENSOR |
180 | | - mock_iotawatt.getSensors.return_value["sensors"][ |
181 | | - "my_watthour_accumulated_input_sensor_key" |
182 | | - ] = INPUT_ACCUMULATED_SENSOR |
183 | | - |
184 | | - DUMMY_DATE = "2021年09月01日T14:00:00+10:00" |
185 | | - |
186 | | - mock_restore_cache( |
187 | | - hass, |
188 | | - ( |
189 | | - State( |
190 | | - "sensor.my_watthour_accumulated_output_sensor_wh_accumulated", |
191 | | - "100.0", |
192 | | - { |
193 | | - "device_class": SensorDeviceClass.ENERGY, |
194 | | - "unit_of_measurement": UnitOfEnergy.WATT_HOUR, |
195 | | - "last_update": DUMMY_DATE, |
196 | | - }, |
197 | | - ), |
198 | | - State( |
199 | | - "sensor.my_watthour_accumulated_input_sensor_wh_accumulated", |
200 | | - "50.0", |
201 | | - { |
202 | | - "device_class": SensorDeviceClass.ENERGY, |
203 | | - "unit_of_measurement": UnitOfEnergy.WATT_HOUR, |
204 | | - "last_update": DUMMY_DATE, |
205 | | - }, |
206 | | - ), |
207 | | - ), |
208 | | - ) |
209 | | - |
210 | | - assert await async_setup_component(hass, "iotawatt", {}) |
211 | | - await hass.async_block_till_done() |
212 | | - |
213 | | - assert len(hass.states.async_entity_ids()) == 2 |
214 | | - |
215 | | - state = hass.states.get( |
216 | | - "sensor.my_watthour_accumulated_output_sensor_wh_accumulated" |
217 | | - ) |
218 | | - assert state is not None |
219 | | - |
220 | | - assert state.state == "300.0" # 100 + 200 |
221 | | - assert ( |
222 | | - state.attributes[ATTR_FRIENDLY_NAME] |
223 | | - == "My WattHour Accumulated Output Sensor.wh Accumulated" |
224 | | - ) |
225 | | - assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL |
226 | | - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR |
227 | | - assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY |
228 | | - assert state.attributes["type"] == "Output" |
229 | | - assert state.attributes[ATTR_LAST_UPDATE] is not None |
230 | | - assert state.attributes[ATTR_LAST_UPDATE] != DUMMY_DATE |
231 | | - |
232 | | - state = hass.states.get( |
233 | | - "sensor.my_watthour_accumulated_input_sensor_wh_accumulated" |
234 | | - ) |
235 | | - assert state is not None |
236 | | - |
237 | | - assert state.state == "550.0" # 50 + 500 |
238 | | - assert ( |
239 | | - state.attributes[ATTR_FRIENDLY_NAME] |
240 | | - == "My WattHour Accumulated Input Sensor.wh Accumulated" |
241 | | - ) |
242 | | - assert state.attributes[ATTR_LAST_UPDATE] is not None |
243 | | - assert state.attributes[ATTR_LAST_UPDATE] != DUMMY_DATE |
0 commit comments