While implementing a new device I noticed a few things that could be improved in the Base class.
The callbacks were being called after every update, regardless of whether the devices state was actually updated. If a device has multiple outputs that are updated it would run the callbacks for each update, even if just one of those updated the state.
Declaring the _callbacks = set() at the class level was causing the callbacks to be shared between all instances of the class. Which would then add callbacks to devices that were not intended to have them.
This PR adds new classes _refresh_state_from_output and _refresh_state_from_input to handle the state updates and only call the callbacks if the state was actually updated.
I've moved the update_device logic to the Base class as that logic is generally the same between devices, but can be overwritten.
Adds an additional test to test the scenario where the websocket would send a device update that doesn't affect the state of the device.
While implementing a new device I noticed a few things that could be improved in the
Base
class._callbacks = set()
at the class level was causing the callbacks to be shared between all instances of the class. Which would then add callbacks to devices that were not intended to have them.This PR adds new classes
_refresh_state_from_output
and_refresh_state_from_input
to handle the state updates and only call the callbacks if the state was actually updated. I've moved theupdate_device
logic to theBase
class as that logic is generally the same between devices, but can be overwritten. Adds an additional test to test the scenario where the websocket would send a device update that doesn't affect the state of the device.