-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX: Unnecessary flash writes by ModbusSwitch component #3648
Conversation
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration ( |
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration ( |
Hey there @martgras, mind taking a look at this pull request as it has been labeled with an integration ( |
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration ( |
80c19b5
to
d594a5d
Compare
5f2dcbc
to
6070c71
Compare
5f2dcbc
to
8c8c189
Compare
@jesserockz Can we finish this review? I had recently to rebase due to a number of conflicts, I would like to avoid too much divergence, this fix is important to avoid flash wear. Thanks! |
e19bbd0
to
f1d59ca
Compare
clang tidy clang tidy clang tidy 2 remove unused import
Here is the PR for the related documentation: esphome/esphome-docs#2381 |
My relay card operation is not very consistent so I'd rather rely on ESPHome restore functionality to always forcefully restore the modbus switches to what they were previously. For example when there's a power cycle, the Modbus device doesn't restore the relays states by itself, it would be very handy to restore them from ESPHome. Do I assume correctly that with |
Answering myself, no. Tested this PR with:
And config:
The expectation was that the board should have saved the switch state to the flash (didn't see an entry in the log about that, either) and restore it after reboot. |
4cefdbb
to
603bc53
Compare
603bc53
to
831e0db
Compare
Hello @nagyrobi, Indeed this PR does not do what you expect. Note that it was not the original purpose of this PR, which was to fix some flash writes that were not applicable to the modbus component and that were causing unnecessary flash wear. However, this PR does lay the groundwork to quickly build the feature you need. Hopefully I can do that once this is merged. In the meantime, I have updated the documentation: https://deploy-preview-2381--esphome.netlify.app/components/switch/modbus_controller.html based on your feedback, to avoid confusion. Regards |
I would really appreciate if it would be possible to merge all in one, in case it's not too complicated. Since I have a real use case, I can contribute with real world tests in exchange for your work. |
This PR is already too big, and those requirements out of its scope. Let's push it to be merged as soon as possible, so we can add that functionality. In the meantime, a boot script should fix your problem: https://esphome.io/components/esphome.html#on-boot |
Thanks @jesserockz @nagyrobi can you test #4122 and see if it solves your issue? |
@jpeletier could you have a look at this? esphome/issues#3878 😇 |
What does this implement/fix?
Modbus switches are unnecessarily updating their status to flash, since the actual state resides in the modbus slave device.
This save state behavior resides in the base
Switch
class inpublish_state()
, thereforeModbusSwitch
could not disable saving to flash. In fact,ModbusSwitch
had to hack its way out of this, being forced to do a dummy read ofinitial_state
even though it is not necessary forModbusSwitch
.When using a large number of Modbus switches, these end up unnecessarily updating their status to flash, causing flash wear, or even a boot loop when saving preferences.
This PR addresses the problem the following way:
GpioSwitch
andOutputSwitch
, moving it toSwitch
ModbusSwitch
restore mode default toALWAYS_OFF
: starts asOFF
until the first modbus refresh, which is the way it is working now.GpioSwitch
andOutputSwitch
to rely onSwitch
for restore mode configuration.The change is backward compatible. current
Switch
-based components are not affected.However, any
Switch
-based component can optionally leverage restore mode. For example,ModbusSwitch
could be easily rewritten to actually persist changes to flash, so that the state is restored and held until the first modbus refresh takes place. Other components, such asTemplateSwitch
could deprecaterestore_state
and such, in favor of the built-inSwitch
restore functionality.Types of changes
Related issue or feature (if applicable): fixes
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#2381
Test Environment
Example entry for
config.yaml
:# Example config.yaml
Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: