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
This commit is contained in:
Aaron Bach 2020-11-22 20:15:38 -07:00 committed by GitHub
parent 492ef81069
commit a4f7b7d784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View File

@ -160,14 +160,17 @@ class NotionEntity(CoordinatorEntity):
self._sensor_id = sensor_id self._sensor_id = sensor_id
self._state = None self._state = None
self._system_id = system_id 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 @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
return ( return (
self.coordinator.last_update_success self.coordinator.last_update_success
and self._task_id in self.coordinator.data["tasks"] and self.task_id in self.coordinator.data["tasks"]
) )
@property @property
@ -204,8 +207,7 @@ class NotionEntity(CoordinatorEntity):
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique, unchanging string that represents this entity.""" """Return a unique, unchanging string that represents this entity."""
task = self.coordinator.data["tasks"][self._task_id] return self._unique_id
return f'{self._sensor_id}_{task["task_type"]}'
async def _async_update_bridge_id(self) -> None: async def _async_update_bridge_id(self) -> None:
"""Update the entity's bridge ID if it has changed. """Update the entity's bridge ID if it has changed.
@ -246,8 +248,10 @@ class NotionEntity(CoordinatorEntity):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Respond to a DataUpdateCoordinator update.""" """Respond to a DataUpdateCoordinator update."""
self.hass.async_create_task(self._async_update_bridge_id()) if self.task_id in self.coordinator.data["tasks"]:
self._async_update_from_latest_data() self.hass.async_create_task(self._async_update_bridge_id())
self._async_update_from_latest_data()
self.async_write_ha_state() self.async_write_ha_state()
async def async_added_to_hass(self): async def async_added_to_hass(self):

View File

@ -77,7 +77,7 @@ class NotionBinarySensor(NotionEntity, BinarySensorEntity):
@callback @callback
def _async_update_from_latest_data(self) -> None: def _async_update_from_latest_data(self) -> None:
"""Fetch new state data for the sensor.""" """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"]: if "value" in task["status"]:
self._state = task["status"]["value"] self._state = task["status"]["value"]
@ -87,7 +87,7 @@ class NotionBinarySensor(NotionEntity, BinarySensorEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return whether the sensor is on or off.""" """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: if task["task_type"] == SENSOR_BATTERY:
return self._state == "critical" return self._state == "critical"

View File

@ -76,7 +76,7 @@ class NotionSensor(NotionEntity):
@callback @callback
def _async_update_from_latest_data(self) -> None: def _async_update_from_latest_data(self) -> None:
"""Fetch new state data for the sensor.""" """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: if task["task_type"] == SENSOR_TEMPERATURE:
self._state = round(float(task["status"]["value"]), 1) self._state = round(float(task["status"]["value"]), 1)