Add cleanup to Notion (#32887)
* Add cleanup to Notion * Base update method
This commit is contained in:
parent
0c49c8578b
commit
1da35e2939
3 changed files with 17 additions and 3 deletions
|
@ -306,13 +306,22 @@ class NotionEntity(Entity):
|
||||||
def update():
|
def update():
|
||||||
"""Update the entity."""
|
"""Update the entity."""
|
||||||
self.hass.async_create_task(self._update_bridge_id())
|
self.hass.async_create_task(self._update_bridge_id())
|
||||||
self.async_schedule_update_ha_state(True)
|
self.update_from_latest_data()
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
|
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
|
||||||
self.hass, TOPIC_DATA_UPDATE, update
|
self.hass, TOPIC_DATA_UPDATE, update
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.update_from_latest_data()
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
async def async_will_remove_from_hass(self):
|
||||||
"""Disconnect dispatcher listener when removed."""
|
"""Disconnect dispatcher listener when removed."""
|
||||||
if self._async_unsub_dispatcher_connect:
|
if self._async_unsub_dispatcher_connect:
|
||||||
self._async_unsub_dispatcher_connect()
|
self._async_unsub_dispatcher_connect()
|
||||||
|
self._async_unsub_dispatcher_connect = None
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def update_from_latest_data(self):
|
||||||
|
"""Update the entity from the latest data."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||||
|
from homeassistant.core import callback
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
BINARY_SENSOR_TYPES,
|
BINARY_SENSOR_TYPES,
|
||||||
|
@ -75,7 +76,8 @@ class NotionBinarySensor(NotionEntity, BinarySensorDevice):
|
||||||
if task["task_type"] == SENSOR_SMOKE_CO:
|
if task["task_type"] == SENSOR_SMOKE_CO:
|
||||||
return self._state != "no_alarm"
|
return self._state != "no_alarm"
|
||||||
|
|
||||||
async def async_update(self):
|
@callback
|
||||||
|
def update_from_latest_data(self):
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
task = self._notion.tasks[self._task_id]
|
task = self._notion.tasks[self._task_id]
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
"""Support for Notion sensors."""
|
"""Support for Notion sensors."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.core import callback
|
||||||
|
|
||||||
from . import SENSOR_TEMPERATURE, SENSOR_TYPES, NotionEntity
|
from . import SENSOR_TEMPERATURE, SENSOR_TYPES, NotionEntity
|
||||||
from .const import DATA_CLIENT, DOMAIN
|
from .const import DATA_CLIENT, DOMAIN
|
||||||
|
|
||||||
|
@ -58,7 +60,8 @@ class NotionSensor(NotionEntity):
|
||||||
"""Return the unit of measurement."""
|
"""Return the unit of measurement."""
|
||||||
return self._unit
|
return self._unit
|
||||||
|
|
||||||
async def async_update(self):
|
@callback
|
||||||
|
def update_from_latest_data(self):
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
task = self._notion.tasks[self._task_id]
|
task = self._notion.tasks[self._task_id]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue