Skip to content

Avoid divide by zero errors in tplink light integration#48235

Merged
frenck merged 1 commit intohome-assistant:devfrom
superm1:tplink-fallback
Mar 31, 2021
Merged

Avoid divide by zero errors in tplink light integration#48235
frenck merged 1 commit intohome-assistant:devfrom
superm1:tplink-fallback

Conversation

@superm1
Copy link
Copy Markdown
Contributor

@superm1 superm1 commented Mar 23, 2021

Proposed change

Some bulbs such as kl125 don't have a definition submitted into
pyHS100. Trying to interact with them will cause a divide by
zero error:

  File "/usr/src/homeassistant/homeassistant/components/tplink/light.py", line 248, in attempt_update
    self._light_features = self._get_light_features()
  File "/usr/src/homeassistant/homeassistant/components/tplink/light.py", line 289, in _get_light_features
    min_mireds = kelvin_to_mired(min_range)
  File "/usr/src/homeassistant/homeassistant/util/color.py", line 515, in color_temperature_kelvin_to_mired
    return math.floor(1000000 / kelvin_temperature)
ZeroDivisionError: division by zero

As the pyHS100 project has been abandoned in favor of python-kasa but there isn't yet
a component in home assistant for using python-kasa add some fallback code into the component.

If the light specifies it supports color temperature but the range isn't available choose a "safe"
range that is the minimum of what is available in bulbs today.

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

Example entry for configuration.yaml:

# Example configuration.yaml
tplink:
  discovery: false
  light:
    - host: !secret kasa_bulb_1

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

Copy link
Copy Markdown
Member

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

A couple of comments. I'm wondering if it would make sense to copy over the definitions from python-kasa to this file, and handle the detection here?

That being said, having the fallback values is still better than crashing when an unknown range is reported by pyhs100.

Comment thread homeassistant/components/tplink/light.py Outdated
Comment thread homeassistant/components/tplink/light.py Outdated
@superm1
Copy link
Copy Markdown
Contributor Author

superm1 commented Mar 23, 2021

A couple of comments. I'm wondering if it would make sense to copy over the definitions from python-kasa to this file, and handle the detection here?
That being said, having the fallback values is still better than crashing when an unknown range is reported by pyhs100.

I hadn't actually considered moving the definitions over. That's a great idea. I've brought them over and added the fallback check in case it's another unknown bulb.

Copy link
Copy Markdown
Member

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

There used to be this sort of mapping (iirc) which was moved to the backend lib which is the proper place for it. Considering the state of affairs, I think it makes sense rather to reintroduce this mapping inside homeassistant instead of doing a new pyhs100 maintenance release for new devices that were not available when pyhs100 got abandoned.

So 👍 after the changes I mentioned in the comments.

Comment thread homeassistant/components/tplink/light.py Outdated
Comment thread homeassistant/components/tplink/light.py Outdated
@superm1 superm1 force-pushed the tplink-fallback branch 2 times, most recently from a0f54f2 to 9276630 Compare March 24, 2021 04:21
@superm1
Copy link
Copy Markdown
Contributor Author

superm1 commented Mar 24, 2021

I'm not really sure what is wrong with the new test I added causing CI problems. They seem to work for me locally.. any ideas?

@superm1
Copy link
Copy Markdown
Contributor Author

superm1 commented Mar 24, 2021

Let me know if you need me to squash them or you will, but it looks like CI and codecov is working now.

Copy link
Copy Markdown
Member

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

LGTM, what do you think @TheGardenMonkey?

@rytilahti rytilahti changed the title Avoid divide by zero errors in pyHS100 Avoid divide by zero errors in tplink light integration Mar 24, 2021
Some bulbs such as kl125 don't have a definition submitted into
pyHS100.  Trying to interact with them will cause a divide by
zero error:
```
  File "/usr/src/homeassistant/homeassistant/components/tplink/light.py", line 248, in attempt_update
    self._light_features = self._get_light_features()
  File "/usr/src/homeassistant/homeassistant/components/tplink/light.py", line 289, in _get_light_features
    min_mireds = kelvin_to_mired(min_range)
  File "/usr/src/homeassistant/homeassistant/util/color.py", line 515, in color_temperature_kelvin_to_mired
    return math.floor(1000000 / kelvin_temperature)
ZeroDivisionError: division by zero
```

As the `pyHS100` project has been abandoned in favor of `python-kasa` but there isn't yet
a component in home assistant for using `python-kasa` move the bulb definitions into this file and
add some fallback code into the component.

If the light specifies it supports color temperature but the range isn't available choose a "safe"
range that is the minimum of what is available in bulbs today.
@superm1
Copy link
Copy Markdown
Contributor Author

superm1 commented Mar 25, 2021

Let me know if you need me to squash them or you will, but it looks like CI and codecov is working now.

FYI squashed it and force pushed, and also added a linked documentation update for this change.

@TheGardenMonkey
Copy link
Copy Markdown
Contributor

This makes sense to me as well.

Comment thread homeassistant/components/tplink/light.py
@superm1
Copy link
Copy Markdown
Contributor Author

superm1 commented Mar 28, 2021

Anything else needed to merge this?

@frenck
Copy link
Copy Markdown
Member

frenck commented Mar 31, 2021

Thanks, @superm1 👍

@frenck frenck merged commit 9fd6980 into home-assistant:dev Mar 31, 2021
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 1, 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.

tplink: Error on device update! (division by zero)

6 participants