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

Make sure ValueNotification includes the latest event data #156

Merged

Conversation

raman325
Copy link
Contributor

@raman325 raman325 commented Mar 5, 2021

In #154 the order of operations for creating the ValueNotification was wrong and led to the value that came in the notification being returned as the value that we was previously in the state (see home-assistant/core#47432). This fixes the order of operations.

I could use some help writing some good test cases for this one, I don't think we have enough given all the issues that have occurred as we try to fix this

@raman325
Copy link
Contributor Author

raman325 commented Mar 5, 2021

@kpine can you take a look at this and see if it makes sense since you spotted this immediately?

@raman325 raman325 changed the title Switch order of ValueNotification creation and update so that the notification includes the latest value Switch order of ValueNotification and Value update so that the notification includes the latest value Mar 5, 2021
Copy link
Contributor

@kpine kpine left a comment

Choose a reason for hiding this comment

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

This is working fine for me. I took a slightly different approach, same result.

        # append metadata if value metadata is available
        value = self.values.get(_get_value_id_from_dict(self, event.data["args"]))
        if value:
            value.update(event.data["args"])
            value_notification = ValueNotification(self, value.data)
        else:
            value_notification = ValueNotification(self, event.data["args"])
        event.data["value_notification"] = value_notification

@kpine
Copy link
Contributor

kpine commented Mar 5, 2021

I was wondering, why is value modified at all? Isn't it needed just to get the metadata? These notifications are supposed to be events or stateless values, not value updates.

@MartinHjelmare
Copy link
Contributor

MartinHjelmare commented Mar 5, 2021

To test this I'd create a Value instance on the node and then create an event coming in with new data, that should trigger a ValueNotification event. The ValueNotification data should then be created with the Value instance data as base but be updated with the new data from the event. So we need to make sure that we can assert that difference when creating the initial Value and event data

@MartinHjelmare
Copy link
Contributor

I was wondering, why is value modified at all? Isn't it needed just to get the metadata? These notifications are supposed to be events or stateless values, not value updates.

Yes, it doesn't sound like the Value state should be updated when reading the docs about ValueNotification event:
https://zwave-js.github.io/node-zwave-js/#/api/node?id=quotvalue-notificationquot

@jcam
Copy link

jcam commented Mar 5, 2021

Is there any pre-existing method to do this?
value_notification.update(value.metadata)
I imagine value.metadata doesn't exist yet...

@jcam
Copy link

jcam commented Mar 5, 2021

ah, perhaps it does...
so maybe this value_notification.metadata.update(value.metadata)

@MartinHjelmare
Copy link
Contributor

value_notification.metadata.update(value.metadata.data)

@jcam
Copy link

jcam commented Mar 5, 2021

Just tested on my prod setup, this works, and resolves home-assistant/core#47432

value_notification.metadata.update(value.metadata.data)

@raman325
Copy link
Contributor Author

raman325 commented Mar 5, 2021

To test this I'd create a Value instance on the node and then create an event coming in with new data, that should trigger a ValueNotification event. The ValueNotification data should then be created with the Value instance data as base but be updated with the new data from the event. So we need to make sure that we can assert that difference when creating the initial Value and event data

So it looks like my tests are missing a value in the args part of the value notification event, and I don't test the value explicitly. If I were to add it with a different value from the Values value, and then check it, would that be sufficient?

@raman325
Copy link
Contributor Author

raman325 commented Mar 5, 2021

To test this I'd create a Value instance on the node and then create an event coming in with new data, that should trigger a ValueNotification event. The ValueNotification data should then be created with the Value instance data as base but be updated with the new data from the event. So we need to make sure that we can assert that difference when creating the initial Value and event data

So it looks like my tests are missing a value in the args part of the value notification event, and I don't test the value explicitly. If I were to add it with a different value from the Values value, and then check it, would that be sufficient?

actually, the Value in my test data doesn't even have a value, so if I add a value to the notification, we can guarantee it's coming from the notification event if the ValueNotification has a value

@raman325
Copy link
Contributor Author

raman325 commented Mar 5, 2021

value_notification.metadata.update(value.metadata.data)

The problem with this approach is that ValueNotification.endpoint returns None with no other changes (this is why the tests in my latest commit fail). There are two ways to solve this:

  1. Update the entire value_notification.data. This may have unintended side effects but not sure.
  2. Update the endpoint property to return 0 by default. I think this is safe because every value has to be on an endpoint, doesn't it?

@jcam
Copy link

jcam commented Mar 5, 2021

I think the endpoint is not included from the ValueNotification, so the endpoint shouldn't be included in the HA event?

@raman325
Copy link
Contributor Author

raman325 commented Mar 5, 2021

I think the endpoint is not included from the ValueNotification, so the endpoint shouldn't be included in the HA event?

The notification is for a value that does have an endpoint. There's a third option, see my upcoming commit

@kpine
Copy link
Contributor

kpine commented Mar 5, 2021

Looks good!

FYI, my scene activation and basic set events do include endpoint from the server.

@MartinHjelmare MartinHjelmare changed the title Switch order of ValueNotification and Value update so that the notification includes the latest value Make sure ValueNotification includes the latest event data Mar 5, 2021
@MartinHjelmare MartinHjelmare merged commit 96adbe4 into home-assistant-libs:master Mar 5, 2021
@raman325 raman325 deleted the value_notification branch March 5, 2021 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants