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:
parent
5dfeb1e02a
commit
c1ddde3764
2 changed files with 19 additions and 6 deletions
|
@ -191,11 +191,16 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
|
|||
old_state = event.data.get("old_state")
|
||||
new_state = event.data.get("new_state")
|
||||
|
||||
if new_state is None or new_state.state in (
|
||||
STATE_UNKNOWN,
|
||||
STATE_UNAVAILABLE,
|
||||
):
|
||||
return
|
||||
|
||||
# We may want to update our state before an early return,
|
||||
# based on the source sensor's unit_of_measurement
|
||||
# or device_class.
|
||||
update_state = False
|
||||
|
||||
unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
if unit is not None:
|
||||
new_unit_of_measurement = self._unit_template.format(unit)
|
||||
|
@ -214,11 +219,9 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
|
|||
if update_state:
|
||||
self.async_write_ha_state()
|
||||
|
||||
if (
|
||||
old_state is None
|
||||
or new_state is None
|
||||
or old_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE)
|
||||
or new_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE)
|
||||
if old_state is None or old_state.state in (
|
||||
STATE_UNKNOWN,
|
||||
STATE_UNAVAILABLE,
|
||||
):
|
||||
return
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue