RestoreSensor: handling native_value with hass.states.set

I’m creating an integration extending the Sensor Platform and using the RestoreSensor class.

In an Integration level Service I’m updating the value of the Sensor. I’m doing this using hass.states.set (or hass.states.async_set). This works and the sensor state changes when the service is run. However, this only changes the state and not the native_value. When restarting, I am running async_get_last_sensor_data to get the native_value but that does not contain the update value set by the service.

  1. Is there a way to set native_value from hass. or some other way without being within the sensor entity itself?

OR

  1. How can I reconcile the state change from hass.states.set with the native_value?

I could also run async_get_last_state and restore the state on restart as well, but that seems to defeat the purpose of native_value. I could also detect a state change in the sensor entity and also set it to the native_value but that also seems to defeat the purpose as well.

Restoring Sensor States
Integration Services
hass.states.set / hass.states.async_set

1 Like