The presence simulation switch did not seem to reliably update persistent state, which meant that manually operation of the switch did not seem to persist across HA restarts. This diff does a few things:
Make switch.internal_turn_[on, off] call self.async_write_ha_state() to actively inform HA of state changes.
Collect together calls to set the attributes of the switch before turning on, so the attributes are persisted as well with the call to self.async_write_ha_state()
Simplify switch to use the is_on, state and name functionality inherited from ToggleEntity rather than duplicating it.
Set inherited switch attribute _attr_available initially to False until restore_state runs and determines the the prior running state of the simulation. This mostly affects the lovelace UI representation of the switch.
When restoring switch state, tolerate missing entity_id attribute in the database. In this case it logs an error and abandons any previous simulation that may have been running.
The presence simulation switch did not seem to reliably update persistent state, which meant that manually operation of the switch did not seem to persist across HA restarts. This diff does a few things:
switch.internal_turn_[on, off]
callself.async_write_ha_state()
to actively inform HA of state changes.self.async_write_ha_state()
is_on
,state
andname
functionality inherited from ToggleEntity rather than duplicating it._attr_available
initially to False untilrestore_state
runs and determines the the prior running state of the simulation. This mostly affects the lovelace UI representation of the switch.entity_id
attribute in the database. In this case it logs an error and abandons any previous simulation that may have been running.running