Make AugustOperatorSensor a RestoreSensor (#98526)

pull/101560/head
Erik Montnemery 2023-10-06 21:25:04 +02:00 committed by GitHub
parent 8877cafe0c
commit 207adaf9cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 5 deletions

View File

@ -12,6 +12,7 @@ from yalexs.keypad import KeypadDetail
from yalexs.lock import Lock, LockDetail from yalexs.lock import Lock, LockDetail
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
RestoreSensor,
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
@ -27,7 +28,6 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from . import AugustData from . import AugustData
from .const import ( from .const import (
@ -174,8 +174,7 @@ async def _async_migrate_old_unique_ids(hass, devices):
registry.async_update_entity(old_entity_id, new_unique_id=device.unique_id) registry.async_update_entity(old_entity_id, new_unique_id=device.unique_id)
# pylint: disable-next=hass-invalid-inheritance # needs fixing class AugustOperatorSensor(AugustEntityMixin, RestoreSensor):
class AugustOperatorSensor(AugustEntityMixin, RestoreEntity, SensorEntity):
"""Representation of an August lock operation sensor.""" """Representation of an August lock operation sensor."""
_attr_translation_key = "operator" _attr_translation_key = "operator"
@ -247,10 +246,15 @@ class AugustOperatorSensor(AugustEntityMixin, RestoreEntity, SensorEntity):
await super().async_added_to_hass() await super().async_added_to_hass()
last_state = await self.async_get_last_state() last_state = await self.async_get_last_state()
if not last_state or last_state.state == STATE_UNAVAILABLE: last_sensor_state = await self.async_get_last_sensor_data()
if (
not last_state
or not last_sensor_state
or last_state.state == STATE_UNAVAILABLE
):
return return
self._attr_native_value = last_state.state self._attr_native_value = last_sensor_state.native_value
if ATTR_ENTITY_PICTURE in last_state.attributes: if ATTR_ENTITY_PICTURE in last_state.attributes:
self._attr_entity_picture = last_state.attributes[ATTR_ENTITY_PICTURE] self._attr_entity_picture = last_state.attributes[ATTR_ENTITY_PICTURE]
if ATTR_OPERATION_REMOTE in last_state.attributes: if ATTR_OPERATION_REMOTE in last_state.attributes: