Add sensor attributes restore to modem_callerid integration (#147753)

This commit is contained in:
HeroOfCanton16 2025-07-03 17:10:26 -04:00 committed by GitHub
parent 8ef6b62d9a
commit 11c75d7ef2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from phone_modem import PhoneModem
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import RestoreSensor
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, STATE_IDLE
from homeassistant.core import Event, HomeAssistant, callback
@ -40,7 +40,7 @@ async def async_setup_entry(
)
class ModemCalleridSensor(SensorEntity):
class ModemCalleridSensor(RestoreSensor):
"""Implementation of USB modem caller ID sensor."""
_attr_should_poll = False
@ -62,9 +62,21 @@ class ModemCalleridSensor(SensorEntity):
async def async_added_to_hass(self) -> None:
"""Call when the modem sensor is added to Home Assistant."""
self.api.registercallback(self._async_incoming_call)
await super().async_added_to_hass()
if (last_state := await self.async_get_last_state()) is not None:
self._attr_extra_state_attributes[CID.CID_NAME] = last_state.attributes.get(
CID.CID_NAME, ""
)
self._attr_extra_state_attributes[CID.CID_NUMBER] = (
last_state.attributes.get(CID.CID_NUMBER, "")
)
self._attr_extra_state_attributes[CID.CID_TIME] = last_state.attributes.get(
CID.CID_TIME, 0
)
self.api.registercallback(self._async_incoming_call)
@callback
def _async_incoming_call(self, new_state: str) -> None:
"""Handle new states."""