Skip to content
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 node internal state handling #363

Merged

Conversation

gorbunovav
Copy link
Contributor

@gorbunovav gorbunovav commented May 31, 2021

Closes #362.

  • Local node state is updated via message from node only (to minimize inconsistency between the real and assumed states).
  • Set messages to smart sleep nodes will be repeated until the response from the node will be received.
  • Refactoring for readability purposes
  • sensor's set_child_value method was removed and replaced and split into update_child_value and set_child_desired_state methods (the first one updates the internal state immediately and the second one sets the target state for smart sleep nodes)

gorbunovav added 20 commits May 30, 2021 22:03
…ew value is set to None upon confirmation from node receive (theolind#362)
…des - it is handled by the `update_child_value` method internally (theolind#362)
… in case node responds with something different than the desired state value (theolind#362)
…for both regular and smart sleep sensors processing (theolind#362)
… the default ack value for the `set` message type (motivation - give a way to match regular and smart sleep behaviour) (theolind#362)
@gorbunovav
Copy link
Contributor Author

@MartinHjelmare take a look, please

@MartinHjelmare
Copy link
Collaborator

I'll have a look later in the week. Thanks!

tests/test_mysensors.py Show resolved Hide resolved
tests/test_mysensors.py Outdated Show resolved Hide resolved
tests/test_mysensors.py Outdated Show resolved Hide resolved
mysensors/sensor.py Outdated Show resolved Hide resolved
mysensors/sensor.py Outdated Show resolved Hide resolved
mysensors/sensor.py Outdated Show resolved Hide resolved
mysensors/__init__.py Outdated Show resolved Hide resolved
@@ -39,6 +38,7 @@ def __init__(self, event_callback=None, protocol_version="1.4"):
self.protocol_version = protocol_version
self.sensors = {}
self.tasks = None
self.use_ack_when_set_values = kwargs.get("use_ack_when_set_values", False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this option belongs in set_child_values per message, and not a new gateway attribute. We already supported it before per message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare this is a workaround for smart sleep nodes.
The gateway's set_child_values method still allows to configure the ACK parameter, but for smart sleep nodes it doesn't work, because we convert set message into the new state value (which has the ChildSensor type and doesn't allow to store message parameters).

E.g. right now Home Assistant integration always requests echo messages during set_child_values calls, but for smart sleep nodes it does nothing.

Other approach would be to use a specialized type to store the new state, which would allow to save message parameters in addition to the value.
Or maybe allow to configure sensor to always use ACK for some types of messages.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider storing all or latest message instead of the state and replaying the message(s).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare yes, I had the same idea, but it requires more changes. Plus storing the desired state instead of just a message is helpful when you need to reply to a state request from the node (handle_req function).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare I think I've addressed all your comments except this one. We can revert this change, if you want, and leave that fix for the next PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's remove it for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gorbunovav and others added 6 commits June 12, 2021 17:15
Co-authored-by: Martin Hjelmare <[email protected]>
Co-authored-by: Martin Hjelmare <[email protected]>
…eter for the default ack value for the `set` message type (motivation - give a way to match regular and smart sleep behaviour) (theolind#362)"

This reverts commit d8fe478.
@MartinHjelmare MartinHjelmare changed the title Fix node internal state handling (#362) Fix node internal state handling Jul 7, 2021
@MartinHjelmare
Copy link
Collaborator

Please update the PR description after the latest commit.

@gorbunovav
Copy link
Contributor Author

@MartinHjelmare oh, right. Done.

Copy link
Collaborator

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MartinHjelmare
Copy link
Collaborator

I think we should mention in the PR description that the Sensor method set_child_value has been removed and how it has been replaced since this is a breaking change.

@gorbunovav
Copy link
Contributor Author

@MartinHjelmare updated. Should not be the breaking change since that method was not part of the public API, as far as I understand it. All state changes should be routed through the gateway class (gateway.set_child_value) 🤔

@MartinHjelmare
Copy link
Collaborator

Technically all non private methods are part of the API.

@gorbunovav
Copy link
Contributor Author

@MartinHjelmare is current description ok?

@MartinHjelmare
Copy link
Collaborator

Thanks!

@MartinHjelmare MartinHjelmare merged commit f13b0dc into theolind:master Jul 11, 2021
Copy link
Collaborator

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking over the code again and found a potential bug.

child = self.new_state[child_id]
value = child.values.get(value_type) if child else None

if value:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check for None explicitly here since the value may be an empty string, which is valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare I've created a new PR - #406

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting node state on message send causes issues in case of message drop
2 participants