Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if attributes are present in new_state before accessing them #71967

Merged
merged 4 commits into from May 25, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions homeassistant/components/integration/sensor.py
Expand Up @@ -191,11 +191,16 @@ def calc_integration(event):
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)
Expand All @@ -214,11 +219,9 @@ def calc_integration(event):
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

Expand Down