Main purpose of RestoreEntity

I saw that some entities in the HA components are derived from RestoreEntity, like:

class ModbusCoilSwitch(ToggleEntity, RestoreEntity):
    """Representation of a Modbus coil switch."""
...

Can anyone explain me the main purpose of the RestoreEntity usage?

Regards,
Daniel.

It stores the state of the entity so the integration can recreate the entity after restart

Ok, but why doesn’t use some entities this feature, e.g. KNXLight(Light)?

For the upper example of ModbusCoilSwitch … it can already get the state from the modbus registers.
So why does it need to store it?

I have no knowledge about that specific integration

The RestoreEntity class is used by Home Assistant to restore an entities state when you restart home assistant. It saves periodically and when you shutdown home assistant, all the states of your entities in a file under config/.storage/core.restore_state. When you startup home assistant, integrations that extend RestoreEntity can call a method async_get_last_state(self) to retrieve the previous state and use it to setup the component.

As for why some use or don’t use RestoreState, some use this so they have an initial state in home assistant until they receive a new one. Others that work in, or have the option for an optimistic mode will require this as they have no way to retrieve a new state.

Ultimately it depends how the integration chooses to implement this feature, have a look at how they use the async_get_last_state() method.

2 Likes