mirror of
https://github.com/home-assistant/core.git
synced 2025-11-05 08:59:57 +00:00
Check if attributes are present in new_state before accessing them (#71967)
* Check if attributes are present in new_state before accessing them. * Early return if new state is None|Unknown|Unavailable * Removed whitespace at line endings. +black run * Update test for coverage
This commit is contained in:
@@ -9,6 +9,7 @@ from homeassistant.const import (
|
||||
ENERGY_WATT_HOUR,
|
||||
POWER_KILO_WATT,
|
||||
POWER_WATT,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
TIME_SECONDS,
|
||||
)
|
||||
@@ -350,6 +351,15 @@ async def test_units(hass):
|
||||
# they became valid
|
||||
assert state.attributes.get("unit_of_measurement") == ENERGY_WATT_HOUR
|
||||
|
||||
# When source state goes to None / Unknown, expect an early exit without
|
||||
# changes to the state or unit_of_measurement
|
||||
hass.states.async_set(entity_id, STATE_UNAVAILABLE, None)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
new_state = hass.states.get("sensor.integration")
|
||||
assert state == new_state
|
||||
assert state.attributes.get("unit_of_measurement") == ENERGY_WATT_HOUR
|
||||
|
||||
|
||||
async def test_device_class(hass):
|
||||
"""Test integration sensor units using a power source."""
|
||||
|
||||
Reference in New Issue
Block a user