mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
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:
parent
492ef81069
commit
a4f7b7d784
@ -160,14 +160,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
|
||||
@ -204,8 +207,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.
|
||||
@ -246,8 +248,10 @@ class NotionEntity(CoordinatorEntity):
|
||||
@callback
|
||||
def _handle_coordinator_update(self):
|
||||
"""Respond to a DataUpdateCoordinator update."""
|
||||
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):
|
||||
|
@ -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"
|
||||
|
@ -76,7 +76,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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user