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 HomeKit linked battery sensor crash #28974

Merged
merged 4 commits into from Nov 26, 2019

Conversation

@adrum
Copy link
Contributor

adrum commented Nov 23, 2019

Description:

This adds checks in to prevent HomeKit component from crashing when a linked battery sensor is missing or incorrectly configured with a nonexistent entity_id. It will also display a warning in the logs when a linked battery sensor is missing, and still allow HomeKit to start;.

Related issue (if applicable): fixes #28686

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

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

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.
@project-bot project-bot bot added this to Needs review in Dev Nov 23, 2019
@MartinHjelmare MartinHjelmare changed the title Fixes HomeKit linked battery sensor crash Fix HomeKit linked battery sensor crash Nov 23, 2019
@@ -105,8 +106,19 @@ def __init__(
battery_found = self.hass.states.get(self.entity_id).attributes.get(
ATTR_BATTERY_LEVEL
)
if self.linked_battery_sensor:
battery_found = self.hass.states.get(self.linked_battery_sensor).state

This comment has been minimized.

Copy link
@pvizeli

pvizeli Nov 23, 2019

Member

Can be simple fix if you check on this place if you get an None before you access to state. Your code work but look a bit complex for solve a simple problem like this

This comment has been minimized.

Copy link
@adrum

adrum Nov 23, 2019

Author Contributor

I've simplified it down to a check, but I also want to display a warning for this scenario. Also, I needed self.linked_battery_sensor = None in order to make sure no errors are thrown during run_handler.

This comment has been minimized.

Copy link
@balloob

balloob Nov 26, 2019

Member
state = self.hass.states.get(self.linked_battery_sensor)
if state is not None:
    battery_found = state.state
else:
    …
Dev automation moved this from Needs review to Review in progress Nov 23, 2019
@adrum

This comment has been minimized.

Copy link
Contributor Author

adrum commented Nov 23, 2019

@pvizeli I'm not entirely sure why the CheckFormat failed. Can you offer any insight or rerun this job if needed?

@springstan

This comment has been minimized.

Copy link
Member

springstan commented Nov 23, 2019

@adrum I think the code is not entirely formatted with black, you can try to fix that by running black --fast homeassistant in your development environment :)

Copy link
Member

balloob left a comment

ok to merge when comment addressed.

Dev automation moved this from Review in progress to Reviewer approved Nov 26, 2019
@balloob balloob merged commit c47ed74 into home-assistant:dev Nov 26, 2019
6 of 9 checks passed
6 of 9 checks passed
CI #20191126.31 failed
Details
CI (Tests PyTest Python36) Tests PyTest Python36 failed
Details
CI (Tests PyTest Python37) Tests PyTest Python37 failed
Details
CI (FullCheck Mypy) FullCheck Mypy succeeded
Details
CI (FullCheck Pylint) FullCheck Pylint succeeded
Details
CI (Overview CheckFormat) Overview CheckFormat succeeded
Details
CI (Overview Lint) Overview Lint succeeded
Details
CI (Overview Validate) Overview Validate succeeded
Details
cla-bot Everyone involved has signed the CLA
Dev automation moved this from Reviewer approved to Done Nov 26, 2019
@lock lock bot locked and limited conversation to collaborators Nov 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
5 participants
You can’t perform that action at this time.