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 geniushub smart plug state at start-up #102110

Conversation

GeoffAtHome
Copy link
Contributor

@GeoffAtHome GeoffAtHome commented Oct 16, 2023

Breaking change

The initial state of the smart plug did not reflect the same state as in the Genius Hub application. Correcting this creates a breaking change in that the behaviour is now correct but is different than before.

If you have an automation that relies on this state you should check in case this change has broken your automation.

Proposed change

When creating a switch the initial state was incorrectly set.

The proposed change was to correct the is_on method.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • [x ] I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @manzanotti, mind taking a look at this pull request as it has been labeled with an integration (geniushub) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of geniushub can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign geniushub Removes the current integration label and assignees on the pull request, add the integration domain after the command.

@MartinHjelmare MartinHjelmare changed the title Smart plug did state wrong at start-up Fix geniushub smart plug state at start-up Oct 16, 2023
@manzanotti
Copy link
Contributor

@GeoffAtHome I'm a little concerned that the original comment explicitly rules out Timer as being on, any idea why that might be?

@zxdavb Can you recall why Timer/on wasn't considered a switch being in the On state?

@zxdavb
Copy link
Contributor

zxdavb commented Oct 21, 2023

@manzanotti I'm sorry - you might have to sign-post me to exact the piece of code...

But, if it is this doctext you're referring to:

The zone is considered 'on' if & only if it is override/on (e.g. timer/on is 'off').

IIRC, it is because that was the behaviour of the WebApp/v1 API at the time.

I wanted the library to have a consistent schema/namespace/attributes/methods/etc., regardless of whether it is using the v1 or v3 API. Obviously, v3 will be a superset of v1.

The two different APIs each offer their own advantages. The goal is that the user can choose either, or even seamlessly switch between.

If this is the case, then choosing to diverge the two behaviours would be a breaking change.

@GeoffAtHome
Copy link
Contributor Author

It is better to have it working correctly even if this is a breaking change.

I will look at the code when I have a chance and see if the behaviour can be modified based on the API version. I think this should be possible. But until I investigate I don't know.

@zxdavb thanks for explanation as I really wasn't sure why the code was written like it was.

@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Oct 22, 2023
@RogerSelwyn
Copy link
Contributor

It should be noted that this change is not to do with just startup status, it is changing how the switch behaves all the time (certainly for v3 API). Currently it only shows as 'on' if it is overridden on, and activating the switch will cause the plug to overridden (for the configured, in app, amount of time). The change will mean it will shows as 'on' when it is either overridden or in timer mode and is switched on.

For me, I'm fine with that, but it is changing behaviour and as noted is a breaking change.

PR #102112, which has already been merged, seems to be dependant on this PR to provide accurate plug status information using the switch entity, since 102112 removes the binary sensor that previously provided that function.

Conversation also present on this issue - #103195

@GeoffAtHome
Copy link
Contributor Author

The zone is considered 'on' if & only if it is override/on (e.g. timer/on is 'off').

I think I have got to the bottom of this. I believe, and may be wrong, that state of the switch (ON | OFF) was also trying to reflect the mode of the zone (Off | Timer | Boost).

If the mode of the zone is required this should be retrieved via a service call.

I do have a longer goal of making the switch more useful but at this time I am struggle how to bring it inline with HA. I am going to raise this discussion else where as it not strictly related to this PR.

I believe the code I submitted to be correct and should be merged.

@RogerSelwyn
Copy link
Contributor

RogerSelwyn commented Nov 7, 2023

For my part I’m happy it is merged, and urgently because the related PR having been merged leaves no reliable on/off status for the plug.

@GeoffAtHome
Copy link
Contributor Author

@manzanotti - please can you approve this PR.

@manzanotti
Copy link
Contributor

@GeoffAtHome I'll have a look (and test it) this evening, once I've got work and dinner out of the way.

@manzanotti
Copy link
Contributor

@GeoffAtHome So, I now have the smart plugs just appearing in the Binary Sensors. I'm going to have a look and see if I can work out why they aren't showing as switches, as I do agree with you that these should be classed as switches, not sensors.

@manzanotti
Copy link
Contributor

@GeoffAtHome So, bear with me, as this is the first time I've had a decent look at the HA integration (I'd only really worked on the genius client previously).

My smart plugs can only show up as a binary sensor because they aren't assigned to a zone. They are therefore only in the broker.client.device_objs list that the Binary Sensors are created from and not in the broker.client.zone_objs list that the switches are created from.

So, whilst this change does fix the issue @RogerSelwyn has, we do have another one to add to the list, namely switches that aren't in a zone. Then there's the additional issue of truly knowing if a switch zone is not.

But, those are separate issues, so I'm happy for this to be merged in.

@RogerSelwyn
Copy link
Contributor

You will need to create switches based on the list of devices, not on the zones if the plug is not in a zone. And then figure out how the switch will behave when toggled. Not sure you can do anything direct to a device, you can only override a zone, and if the plug has no zone, what does the switch do? If it does nothing, then is it a switch?

@GeoffAtHome
Copy link
Contributor Author

@manzanotti this is a good corner case that I did not expect. That said, it should be fairly trivial to fix. We can iterate over devices to find the smart plugs (switches) and do the same for zones. From the results remove duplicates so we only create one switch for each smart plugs.

Main difference is a smart plug that is not in a zone will appear as a switch with a device ID and not a zone name.

I give this a go later and report back.

@manzanotti
Copy link
Contributor

@GeoffAtHome I am wondering if at least some of this logic should go into the client, rather than here. Obviously, there shouldn't be anything in the client that is HA-specific.

But I do think that as part of the work I want to do to expose the V3 API via properties, and leave the JSON as the V1-compatible data, we could make the client do more of the work of indicating the true state of the system.

And if we can improve on the official Genius functionality, I'm happy to do that. For example, putting my smart plug in a zone means that when I turn the switch on in HA, the plug only stays on for an hour, which isn't how I'd expect a switch to work. I get that's how zones work, and that is also how some people would want their switch to work. But it'd be good if we can introduce more control over switches.

@GeoffAtHome
Copy link
Contributor Author

@manzanotti the switches have always been a bug bear of mine. I only got them as the suggestion was I needed three to cover the communications in the house.

I have never tried not putting them in a zone and don't know what to expect.

For me, I have schedule set up that keeps the switches on 24/7. Then I simple toggle between off and timer. I see little value in using the boost function. This way I can manage turning on and off the switch from HA.

…h,-does-not-reflect-correct-state-on-start-up
@GeoffAtHome
Copy link
Contributor Author

The code is ready to to merge. Sorry I didn't mark it as ready.

Copy link
Member

@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!

Do we agree that this is a breaking change and should be highlighted as such in the release notes with a paragraph explaining the change for the users? If so, please add that to the PR description. I'll add the label. If it's a breaking change we'll wait with releasing the PR until the next minor release in December.

@RogerSelwyn
Copy link
Contributor

@MartinHjelmare in my view this fixes something that is now broken. So whilst in itself it is a breaking change, a related PR was merged that broke the integration, so this one is needed to bring it back to a working but different state. See this issue for more details - #103195

@GeoffAtHome
Copy link
Contributor Author

@manzanotti this is a good corner case that I did not expect. That said, it should be fairly trivial to fix. We can iterate over devices to find the smart plugs (switches) and do the same for zones. From the results remove duplicates so we only create one switch for each smart plugs.

Main difference is a smart plug that is not in a zone will appear as a switch with a device ID and not a zone name.

I give this a go later and report back.

I failed to get any code for this as deleting a zone with a smart plug to try and simulate your setup give me too much grief. The smart plug got orphaned. Geniushub-client could not login into my hub.

Powercycling the hub and a bug report the GH fixed the issue. I don't want to try that again!

@MartinHjelmare
Copy link
Member

@MartinHjelmare in my view this fixes something that is now broken. So whilst in itself it is a breaking change, a related PR was merged that broke the integration, so this one is needed to bring it back to a working but different state. See this issue for more details - #103195

I understand that it fixes the integration for some users but it could also break assumptions for other users as I understand it from your posts. If that's true I suggest we label it as a breaking change and release in December. Otherwise we'd be making another unexpected breaking change. Two wrongs don't make a right, so to speak.

If all of you think we should release it as a fix anyway, I'll concede.

@RogerSelwyn
Copy link
Contributor

I’ll go with the PR creator and integration maintainer. In my view how the plugs work needs some serious thinking through all the different scenarios which I suspect will take some time.

@MartinHjelmare
Copy link
Member

@manzanotti and @GeoffAtHome, what do you think?

@GeoffAtHome
Copy link
Contributor Author

I too tired to really think so apologies now in case I am wrong.

Agree with two wrongs don't make a right but in this case if the two PRs are released together it does. But I can't remember all the details.

As I am currently running a private build selfishly this doesn't impact me.

Just driven 12 hours from Cardiff to the Scrabster and currently waiting for a ferry and getting my head down.

@MartinHjelmare
Copy link
Member

To be clear, #102112 was released in core-2023.11.0. So these PRs won't be released together.

@RogerSelwyn
Copy link
Contributor

RogerSelwyn commented Nov 10, 2023

And that is the problem. Personally I would prefer that 102112 is reverted. Then a new PR raised with all the required changes in it after thinking through all the impacts. From the issue I raised I can see it is not just smart plugs that are impacted but also electric switches. As we go into winter I suspect people can’t see if the heating is on or not (assuming the switches are being used to drive under floor heating and hot water tanks).

@MartinHjelmare
Copy link
Member

That's also a possibility. I suggest the three of you decide together what we should do. It's hard for me to judge the impact of the changes to these features. I can only make recommendations based on what you say.

@RogerSelwyn
Copy link
Contributor

Again for @manzanotti and @GeoffAtHome to decide. I’ve stated my preference and I’m in a working condition because I’ve substituted the old code base. But I’m trying to think of others who won’t know how to bring in a working code base.

@manzanotti
Copy link
Contributor

With this one running locally, I think I have a Genius integration looking like it did before the first PR went in.

Reading your initial comment about this pr correcting whether a switch is shown as being on correctly, I don't think this would be a breaking change, as it is fixing an issue where they aren't currently shown in the correct state.

I'm struggling to think of a scenario where someone would be relying on the switch status being shown incorrectly when in timer mode.

Do we have scenarios that we think we have broken, rather than fixed an issue?

@RogerSelwyn
Copy link
Contributor

Do we have scenarios that we think we have broken, rather than fixed an issue?

I don't know because I haven't tested all the scenarios. I've tested mine and it works for my scenario.

Do we have somewhere the last comms date for the plugs, like we do for all the other devices in the system. Something to think about since these will be the only devices not represented by the integration? I can think of people using that to see if their system is broken, but of course that would be presented in an error as well, even if you don't have the date/time info.

@manzanotti
Copy link
Contributor

So, can we either reach a consensus that this is fine to be released, or decide what needs to be updated to get it into that state? @RogerSelwyn @GeoffAtHome

@RogerSelwyn
Copy link
Contributor

@manzanotti , I’m leaving this to you guys since you are the code owner and @GeoffAtHome created the PR. For my part I am using a custom installation so I’ve got a working environment. I’ll work with whatever you two agree on.

@GeoffAtHome
Copy link
Contributor Author

GeoffAtHome commented Dec 2, 2023 via email

@MartinHjelmare
Copy link
Member

Should we add a breaking change paragraph or not?

@GeoffAtHome
Copy link
Contributor Author

As the behaviour has changed, then this is a breaking change from before. People should be aware that the overcome is different.

@MartinHjelmare
Copy link
Member

Ok. Please update the breaking change section in the PR description.

@MartinHjelmare
Copy link
Member

Ping me when that's done as PR description updates don't generate notifications.

@GeoffAtHome
Copy link
Contributor Author

Updated breaking change comment.

@manzanotti
Copy link
Contributor

Nice one, @GeoffAtHome , thanks for that.

Ready for you now, @MartinHjelmare

Copy link
Member

@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 MartinHjelmare added this to the 2023.12.0 milestone Dec 5, 2023
@MartinHjelmare MartinHjelmare merged commit c4fbc78 into home-assistant:dev Dec 5, 2023
18 checks passed
frenck pushed a commit that referenced this pull request Dec 5, 2023
* Smart plug did state wrong at start-up

* Update docstring to reflect code
@github-actions github-actions bot locked and limited conversation to collaborators Dec 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants