diff --git a/homeassistant/components/modem_callerid/sensor.py b/homeassistant/components/modem_callerid/sensor.py index de8e4b2f73c..db901511d5f 100644 --- a/homeassistant/components/modem_callerid/sensor.py +++ b/homeassistant/components/modem_callerid/sensor.py @@ -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."""