Entity state on homeassistant restart

I’m trying to figure out the correct way to restore state and device attributes when restarting home assistant on a new component I am working on. The component has a scan interval of once an hour as it’s a value that doesn’t change often. If I wait an hour home assistant will populate the state and device attributes correctly.

However on restart it sets the state and device attributes to the default values (None and {} respectively) rather than restoring the previous state. This means that on every restart I have to wait an hour for the entity to have it’s state and device attributes populated. This is because the update method of the entity hasn’t been called yet so it’s using the default values. The following values in my database represent before restart and after:

*************************** 27. row ***************************
state_id: 1255389
domain: sensor
entity_id: sensor.crossfit_classes
state: 0
attributes: {"expiration_date": "2019-05-04T00:00:00", "expired": false, "purchased": 20, "remaining": 1, "unit_of_measurement": "classes remaining", "friendly_name": "Crossfit Classes", "icon": "mdi:dumbbell"}
event_id: 1722098
last_changed: 2019-04-13 18:15:08
last_updated: 2019-04-13 18:15:08
created: 2019-04-13 18:15:08
context_id: 4dc786a3f3da425eb852409632683667
context_user_id: NULL
*************************** 28. row ***************************
state_id: 1256870
domain: sensor
entity_id: sensor.crossfit_classes
state: unknown
attributes: {"unit_of_measurement": "classes remaining", "friendly_name": "Crossfit Classes", "icon": "mdi:dumbbell"}
event_id: 1724560
last_changed: 2019-04-13 18:42:06
last_updated: 2019-04-13 18:42:06
created: 2019-04-13 18:42:17
context_id: 1d8e3afc119e42b6a2b407a0d1ca67c7
context_user_id: NULL

How can I make homeassistant use the previous value instead of it updating to an unknown state on restart and then waiting for an hour for the component’s update method to run and re-populate the state and device attributes?

Here’s the basic sensor entity code:

class CrossfitSensor(Entity):
    """Representation of a sensor."""

    def __init__(self, hass, username, password):
        self._hass = hass
        self.username = username
        self.password = password
        self.attrs = {}
        self._state = None

    def name(self):
        """Return the name of the sensor."""
        return 'Crossfit Classes'

    def unit_of_measurement(self):
        """Return the unit of measurement of this entity, if any."""
        return 'classes remaining'

    def state(self):
        """Return the state of the sensor."""
        if self._state is None:
            if self.attrs:
                if self.attrs['expired']:
                    self._state = 0
                    self._state = self.attrs['remaining']
        return self._state

    def icon(self):
        """Icon to use in the frontend."""
        return 'mdi:dumbbell'

    def device_state_attributes(self):
        return self.attrs

    def update(self):
        """Get the latest data and updates the state."""
        # calls api to get data and sets state/attrs.

I’m not a strong enough coder to attempt getting the syntax right, but I think using a template_sensor – specifically the is_state function – is the direction to be looking.