Improve utility meter restore state tests (#114356)
parent
21bff95bd7
commit
42580a1113
|
@ -610,7 +610,7 @@ async def test_device_class(
|
||||||
"utility_meter": {
|
"utility_meter": {
|
||||||
"energy_bill": {
|
"energy_bill": {
|
||||||
"source": "sensor.energy",
|
"source": "sensor.energy",
|
||||||
"tariffs": ["onpeak", "midpeak", "offpeak", "superpeak"],
|
"tariffs": ["tariff1", "tariff2", "tariff3", "tariff4"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -626,7 +626,7 @@ async def test_device_class(
|
||||||
"offset": 0,
|
"offset": 0,
|
||||||
"periodically_resetting": True,
|
"periodically_resetting": True,
|
||||||
"source": "sensor.energy",
|
"source": "sensor.energy",
|
||||||
"tariffs": ["onpeak", "midpeak", "offpeak", "superpeak"],
|
"tariffs": ["tariff1", "tariff2", "tariff3", "tariff4"],
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -638,82 +638,89 @@ async def test_restore_state(
|
||||||
# Home assistant is not runnit yet
|
# Home assistant is not runnit yet
|
||||||
hass.set_state(CoreState.not_running)
|
hass.set_state(CoreState.not_running)
|
||||||
|
|
||||||
last_reset = "2020-12-21T00:00:00.013073+00:00"
|
last_reset_1 = "2020-12-21T00:00:00.013073+00:00"
|
||||||
|
last_reset_2 = "2020-12-22T00:00:00.013073+00:00"
|
||||||
|
|
||||||
mock_restore_cache_with_extra_data(
|
mock_restore_cache_with_extra_data(
|
||||||
hass,
|
hass,
|
||||||
[
|
[
|
||||||
|
# sensor.energy_bill_tariff1 is restored as expected
|
||||||
(
|
(
|
||||||
State(
|
State(
|
||||||
"sensor.energy_bill_onpeak",
|
"sensor.energy_bill_tariff1",
|
||||||
"3",
|
"1.1",
|
||||||
attributes={
|
attributes={
|
||||||
ATTR_STATUS: PAUSED,
|
ATTR_STATUS: PAUSED,
|
||||||
ATTR_LAST_RESET: last_reset,
|
ATTR_LAST_RESET: last_reset_1,
|
||||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.MEGA_WATT_HOUR,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"native_value": {
|
"native_value": {
|
||||||
"__type": "<class 'decimal.Decimal'>",
|
"__type": "<class 'decimal.Decimal'>",
|
||||||
"decimal_str": "3",
|
"decimal_str": "1.2",
|
||||||
},
|
},
|
||||||
"native_unit_of_measurement": "kWh",
|
"native_unit_of_measurement": "kWh",
|
||||||
"last_reset": last_reset,
|
"last_reset": last_reset_2,
|
||||||
"last_period": "7",
|
"last_period": "1.3",
|
||||||
"last_valid_state": "None",
|
"last_valid_state": None,
|
||||||
"status": "paused",
|
"status": "paused",
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
# sensor.energy_bill_tariff2 has missing keys and falls back to
|
||||||
|
# saved state
|
||||||
(
|
(
|
||||||
State(
|
State(
|
||||||
"sensor.energy_bill_midpeak",
|
"sensor.energy_bill_tariff2",
|
||||||
"5",
|
"2.1",
|
||||||
attributes={
|
attributes={
|
||||||
ATTR_STATUS: PAUSED,
|
ATTR_STATUS: PAUSED,
|
||||||
ATTR_LAST_RESET: last_reset,
|
ATTR_LAST_RESET: last_reset_1,
|
||||||
ATTR_LAST_VALID_STATE: None,
|
ATTR_LAST_VALID_STATE: None,
|
||||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.MEGA_WATT_HOUR,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"native_value": {
|
"native_value": {
|
||||||
"__type": "<class 'decimal.Decimal'>",
|
"__type": "<class 'decimal.Decimal'>",
|
||||||
"decimal_str": "3",
|
"decimal_str": "2.2",
|
||||||
},
|
},
|
||||||
"native_unit_of_measurement": "kWh",
|
"native_unit_of_measurement": "kWh",
|
||||||
"last_valid_state": "None",
|
"last_valid_state": "None",
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
# sensor.energy_bill_tariff3 has invalid data and falls back to
|
||||||
|
# saved state
|
||||||
(
|
(
|
||||||
State(
|
State(
|
||||||
"sensor.energy_bill_offpeak",
|
"sensor.energy_bill_tariff3",
|
||||||
"6",
|
"3.1",
|
||||||
attributes={
|
attributes={
|
||||||
ATTR_STATUS: COLLECTING,
|
ATTR_STATUS: COLLECTING,
|
||||||
ATTR_LAST_RESET: last_reset,
|
ATTR_LAST_RESET: last_reset_1,
|
||||||
ATTR_LAST_VALID_STATE: None,
|
ATTR_LAST_VALID_STATE: None,
|
||||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.MEGA_WATT_HOUR,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"native_value": {
|
"native_value": {
|
||||||
"__type": "<class 'decimal.Decimal'>",
|
"__type": "<class 'decimal.Decimal'>",
|
||||||
"decimal_str": "3f",
|
"decimal_str": "3f", # Invalid
|
||||||
},
|
},
|
||||||
"native_unit_of_measurement": "kWh",
|
"native_unit_of_measurement": "kWh",
|
||||||
"last_valid_state": "None",
|
"last_valid_state": "None",
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
# No extra saved data, fall back to saved state
|
||||||
(
|
(
|
||||||
State(
|
State(
|
||||||
"sensor.energy_bill_superpeak",
|
"sensor.energy_bill_tariff4",
|
||||||
"error",
|
"error",
|
||||||
attributes={
|
attributes={
|
||||||
ATTR_STATUS: COLLECTING,
|
ATTR_STATUS: COLLECTING,
|
||||||
ATTR_LAST_RESET: last_reset,
|
ATTR_LAST_RESET: last_reset_1,
|
||||||
ATTR_LAST_VALID_STATE: None,
|
ATTR_LAST_VALID_STATE: None,
|
||||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.MEGA_WATT_HOUR,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
{},
|
{},
|
||||||
|
@ -736,25 +743,28 @@ async def test_restore_state(
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# restore from cache
|
# restore from cache
|
||||||
state = hass.states.get("sensor.energy_bill_onpeak")
|
state = hass.states.get("sensor.energy_bill_tariff1")
|
||||||
assert state.state == "3"
|
assert state.state == "1.2"
|
||||||
assert state.attributes.get("status") == PAUSED
|
assert state.attributes.get("status") == PAUSED
|
||||||
assert state.attributes.get("last_reset") == last_reset
|
assert state.attributes.get("last_reset") == last_reset_2
|
||||||
assert state.attributes.get("last_valid_state") == "None"
|
assert state.attributes.get("last_valid_state") == "None"
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
|
||||||
|
|
||||||
state = hass.states.get("sensor.energy_bill_midpeak")
|
state = hass.states.get("sensor.energy_bill_tariff2")
|
||||||
assert state.state == "5"
|
assert state.state == "2.1"
|
||||||
|
assert state.attributes.get("status") == PAUSED
|
||||||
|
assert state.attributes.get("last_reset") == last_reset_1
|
||||||
assert state.attributes.get("last_valid_state") == "None"
|
assert state.attributes.get("last_valid_state") == "None"
|
||||||
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.MEGA_WATT_HOUR
|
||||||
|
|
||||||
state = hass.states.get("sensor.energy_bill_offpeak")
|
state = hass.states.get("sensor.energy_bill_tariff3")
|
||||||
assert state.state == "6"
|
assert state.state == "3.1"
|
||||||
assert state.attributes.get("status") == COLLECTING
|
assert state.attributes.get("status") == COLLECTING
|
||||||
assert state.attributes.get("last_reset") == last_reset
|
assert state.attributes.get("last_reset") == last_reset_1
|
||||||
assert state.attributes.get("last_valid_state") == "None"
|
assert state.attributes.get("last_valid_state") == "None"
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.MEGA_WATT_HOUR
|
||||||
|
|
||||||
state = hass.states.get("sensor.energy_bill_superpeak")
|
state = hass.states.get("sensor.energy_bill_tariff4")
|
||||||
assert state.state == STATE_UNKNOWN
|
assert state.state == STATE_UNKNOWN
|
||||||
|
|
||||||
# utility_meter is loaded, now set sensors according to utility_meter:
|
# utility_meter is loaded, now set sensors according to utility_meter:
|
||||||
|
@ -764,13 +774,18 @@ async def test_restore_state(
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("select.energy_bill")
|
state = hass.states.get("select.energy_bill")
|
||||||
assert state.state == "onpeak"
|
assert state.state == "tariff1"
|
||||||
|
|
||||||
state = hass.states.get("sensor.energy_bill_onpeak")
|
state = hass.states.get("sensor.energy_bill_tariff1")
|
||||||
assert state.attributes.get("status") == COLLECTING
|
assert state.attributes.get("status") == COLLECTING
|
||||||
|
|
||||||
state = hass.states.get("sensor.energy_bill_offpeak")
|
for entity_id in (
|
||||||
assert state.attributes.get("status") == PAUSED
|
"sensor.energy_bill_tariff2",
|
||||||
|
"sensor.energy_bill_tariff3",
|
||||||
|
"sensor.energy_bill_tariff4",
|
||||||
|
):
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.attributes.get("status") == PAUSED
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
Loading…
Reference in New Issue