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 Hue motion sensor state if sensor is disabled #63000

Merged
merged 3 commits into from
Dec 29, 2021
Merged

Fix Hue motion sensor state if sensor is disabled #63000

merged 3 commits into from
Dec 29, 2021

Conversation

marcelveldt
Copy link
Member

@marcelveldt marcelveldt commented Dec 29, 2021

Proposed change

If the Hue motion sensor is turned off, its value is None instead of a boolean.
This change explicitly checks for a boolean True for the motion sensor state.

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

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
  • 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.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

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

"""Return true if the binary sensor is on."""
return self.resource.motion.motion
return self.resource.motion.motion is True
Copy link
Member

@frenck frenck Dec 29, 2021

Choose a reason for hiding this comment

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

So this is a bit of a weird one to review right now.

In the current stable release (2021.12), it should be a boolean that is returned.
However, on the current dev (2022.2), binary sensors actually can return a boolean or None value. In case it is unknown, it should be None.

So I think this is a correct fix for the current stable version, but incorrect behavior for the current dev branch.

Considering that, I would say the existing code is causing an unexpected behavior that is actually expected behavior in the next Home Assistant. My advice would be: Don't fix it; or else it would be a "bug" next month anyways.

Copy link
Member Author

Choose a reason for hiding this comment

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

haha! In that case it was already working correctly.
In fact it is unknown because the sensor is disabled in Hue. Even Hue reports it as unknown due to the None value.

Copy link
Member

Choose a reason for hiding this comment

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

In fact it is unknown because the sensor is disabled in Hue. Even Hue reports it as unknown due to the None value.

Hmm... so shouldn't that be marked as unavailable on the Home Assistant end in that case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good suggestion but unavailable is used for connection state...

Copy link
Member

Choose a reason for hiding this comment

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

So unavailable on entity/device level, when connecting to hubs, is generally a combined state.

If the Hub is unavailable/cannot be connected to, of course, the device and its entities won't be available either. However, if the device is not available on the hub, the device and entities can be marked unavailable as well, while other devices of the hub remain available.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not exactly as the other attributes and entities of the device still work. Unavailable is when the device itself is not reachable. At least that's how we implemented it in Hue ;-)

I've adjusted the code to specific check that enabled state, might be enough for now ?

Copy link
Member

@frenck frenck Dec 29, 2021

Choose a reason for hiding this comment

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

I don't agree with that as per above. Sorry.

I think it is really clear the device is disabled in Hue, thus not available for use in Home Assistant.

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case I think we should leave it as-is. Attaching the available state to the enabled property also doesn't feel right as users might go looking for connectivity issues, not remembering they disabled the sensor

Copy link
Contributor

Choose a reason for hiding this comment

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

And as of Home Assistant 2022.2, Unknown when we don't know.

except that we do know, because their associated switch is turned-off, so it must be Clear (can never be 'Detected')

as said, that feels regression.... A frontend with all unknowns, even though a sensors parent switch is in a known state. And toggling it will fix it.

This has been working fine for years, and now an academic truth (unknown because no positive other state can be determined), while in practice every end user will know if their switch is turned on or off.

Copy link
Member

Choose a reason for hiding this comment

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

I really don't agree with that perspective actually. There is no difference here compared to e.g., unplugging a bulb. Communication with the hub is possible, true, but communication with the end device has been cut off by the hub because it has been disabled there. The device is therefore not available.

Comment on lines 87 to 88
if not self.resource.enabled:
return False
Copy link
Member

Choose a reason for hiding this comment

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

This change induces the same issue. We don't know if it is false or not. For example, with a motion sensor, there could be motion? We don't know... as the device is disabled.

Copy link
Contributor

@Mariusthvdb Mariusthvdb Dec 29, 2021

Choose a reason for hiding this comment

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

On the contrary: the device is disabled, so there can never be any detected motion ;-) That is a 100% certainty, not an 'unknown'.

It must be 'off', because, well, it is switched off. And a binary that is off translates to 'Clear' for a device_class: motion.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is very specific for Hue where you can temporary disable the motion sensor (from controlling your lights) and its state is always False in that case. But agreed, strictly taken there can be motion while the sensor is disabled.

I'm tempted to just close this PR as its not a very big deal and Unknown is not that bad ;-)

Copy link
Member

@frenck frenck Dec 29, 2021

Choose a reason for hiding this comment

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

On the contrary: the device is disabled, so there can never be any detected motion ;-) That is a 100% certainty, not an 'unknown'.

That is the wrong viewing angle. The point is, the device looks available from HA and reports 'no motion'. That isn't correct in any way.

It must be 'off', because, well, it is switched off.

I think you are confused what binary sensor mean. Off in case of motion sensors mean: No motion. This has been documented.

Copy link
Member

Choose a reason for hiding this comment

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

I'm tempted to just close this PR as its not a very big deal and Unknown is not that bad ;-)

I'm tempted to actually want to get this fixed, as it is broken in the current state and not correct IMHO. Closing this PR won't fix that either.

Copy link
Member Author

@marcelveldt marcelveldt Dec 29, 2021

Choose a reason for hiding this comment

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

I agree with @frenck here that the current working is not correct and should be fixed anyway.
I see 2 possibilities here:

  1. Force the return of None if the sensor is disabled (displayed as Unknown in the frontend)
  2. Mark the entity as unavailable if its disabled.

I have no preference, both are fine to me.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. 1: would only be valid in current dev, 2: is preferred I think, but you raised a concern about third-party devices. So that would make 1 the middle ground.

Copy link
Member Author

Choose a reason for hiding this comment

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

Adjusted the logic to force return None (unknown) when the sensor is set disabled.

@frenck frenck removed this from the 2021.12.7 milestone Dec 29, 2021
Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @marcelveldt 👍

@frenck frenck merged commit f28c66c into home-assistant:dev Dec 29, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Dec 30, 2021
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.

Hue turned off motion sensors show 'unknown' upon restart
5 participants