From 92cd1373de6739a38a5bae8782160b555b5e5b85 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sun, 22 Nov 2020 20:15:38 -0700 Subject: [PATCH] Fix bug related to possibly missing task ID in Notion API data (#43330) * Fix bug related to possibly missing task ID in Notion API data * Calculate unique ID once * Code review * Simplify * Code review --- homeassistant/components/notion/__init__.py | 16 ++++++++++------ homeassistant/components/notion/binary_sensor.py | 4 ++-- homeassistant/components/notion/sensor.py | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/notion/__init__.py b/homeassistant/components/notion/__init__.py index 296eb34934b..007f18525fe 100644 --- a/homeassistant/components/notion/__init__.py +++ b/homeassistant/components/notion/__init__.py @@ -163,14 +163,17 @@ class NotionEntity(CoordinatorEntity): self._sensor_id = sensor_id self._state = None self._system_id = system_id - self._task_id = task_id + self._unique_id = ( + f'{sensor_id}_{self.coordinator.data["tasks"][task_id]["task_type"]}' + ) + self.task_id = task_id @property def available(self) -> bool: """Return True if entity is available.""" return ( self.coordinator.last_update_success - and self._task_id in self.coordinator.data["tasks"] + and self.task_id in self.coordinator.data["tasks"] ) @property @@ -207,8 +210,7 @@ class NotionEntity(CoordinatorEntity): @property def unique_id(self) -> str: """Return a unique, unchanging string that represents this entity.""" - task = self.coordinator.data["tasks"][self._task_id] - return f'{self._sensor_id}_{task["task_type"]}' + return self._unique_id async def _async_update_bridge_id(self) -> None: """Update the entity's bridge ID if it has changed. @@ -249,8 +251,10 @@ class NotionEntity(CoordinatorEntity): @callback def _handle_coordinator_update(self): """Respond to a DataUpdateCoordinator update.""" - self.hass.async_create_task(self._async_update_bridge_id()) - self._async_update_from_latest_data() + if self.task_id in self.coordinator.data["tasks"]: + self.hass.async_create_task(self._async_update_bridge_id()) + self._async_update_from_latest_data() + self.async_write_ha_state() async def async_added_to_hass(self): diff --git a/homeassistant/components/notion/binary_sensor.py b/homeassistant/components/notion/binary_sensor.py index b8fd96fabc5..a198903b99a 100644 --- a/homeassistant/components/notion/binary_sensor.py +++ b/homeassistant/components/notion/binary_sensor.py @@ -77,7 +77,7 @@ class NotionBinarySensor(NotionEntity, BinarySensorEntity): @callback def _async_update_from_latest_data(self) -> None: """Fetch new state data for the sensor.""" - task = self.coordinator.data["tasks"][self._task_id] + task = self.coordinator.data["tasks"][self.task_id] if "value" in task["status"]: self._state = task["status"]["value"] @@ -87,7 +87,7 @@ class NotionBinarySensor(NotionEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return whether the sensor is on or off.""" - task = self.coordinator.data["tasks"][self._task_id] + task = self.coordinator.data["tasks"][self.task_id] if task["task_type"] == SENSOR_BATTERY: return self._state == "critical" diff --git a/homeassistant/components/notion/sensor.py b/homeassistant/components/notion/sensor.py index 091dcd324dc..8f7337d200f 100644 --- a/homeassistant/components/notion/sensor.py +++ b/homeassistant/components/notion/sensor.py @@ -79,7 +79,7 @@ class NotionSensor(NotionEntity): @callback def _async_update_from_latest_data(self) -> None: """Fetch new state data for the sensor.""" - task = self.coordinator.data["tasks"][self._task_id] + task = self.coordinator.data["tasks"][self.task_id] if task["task_type"] == SENSOR_TEMPERATURE: self._state = round(float(task["status"]["value"]), 1)