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

Add typecasting for brightness and temperature for tuya lights #113603

Closed
wants to merge 1 commit into from

Conversation

gautamkrishnar
Copy link

Breaking change

n/a

Proposed change

  • Added typecasting for brightness and temperature for tuya lights to prevent errors while starting. Errors like unsupported operand type(s) for -: 'int' and 'str' is fixed with this.

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

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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format 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[bot]

This comment was marked as resolved.

@home-assistant

This comment was marked as resolved.

@home-assistant home-assistant bot marked this pull request as draft March 16, 2024 16:34
@home-assistant
Copy link

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

Code owner commands

Code owners of tuya 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 tuya Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@gautamkrishnar gautamkrishnar marked this pull request as ready for review March 16, 2024 16:36
@MartinHjelmare MartinHjelmare changed the title Added typecasting for brightness and temperature for tuya lights Add typecasting for brightness and temperature for tuya lights Mar 17, 2024
Comment on lines +692 to +695
# try to convert temperature to float if it's a string or any other type
if not isinstance(temperature, float):
with contextlib.suppress(ValueError):
temperature = float(temperature)
Copy link
Member

Choose a reason for hiding this comment

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

Both isinstance and contextlib.suppress are pretty expensive.

I'm not fully following the type conversion happening. This should be a float value according to specifications. Why isn't it in this case?

Copy link
Author

@gautamkrishnar gautamkrishnar Mar 18, 2024

Choose a reason for hiding this comment

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

@frenck you can check the issue #111839 I am getting this with my tuya lights:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 576, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 865, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1328, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 992, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1113, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1052, in __async_calculate_state
    attr.update(self.state_attributes or {})
                ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 1202, in state_attributes
    data[ATTR_BRIGHTNESS] = self.brightness
                            ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tuya/light.py", line 648, in brightness
    brightness = self._brightness.remap_value_to(brightness)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tuya/base.py", line 63, in remap_value_to
    return remap_value(value, self.min, self.max, to_min, to_max, reverse)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tuya/util.py", line 16, in remap_value
    return ((value - from_min) / (from_max - from_min)) * (to_max - to_min) + to_min
             ~~~~~~^~~~~~~~~~
TypeError: unsupported operand type(s) for -: 'str' and 'int'
2024-03-18 23:37:25.678 ERROR (MainThread) [homeassistant.components.light] Error adding entity light.bathroom_light for domain light with platform tuya
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 576, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 865, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1328, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 992, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1113, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1052, in __async_calculate_state
    attr.update(self.state_attributes or {})
                ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 1202, in state_attributes
    data[ATTR_BRIGHTNESS] = self.brightness
                            ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tuya/light.py", line 648, in brightness
    brightness = self._brightness.remap_value_to(brightness)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tuya/base.py", line 63, in remap_value_to
    return remap_value(value, self.min, self.max, to_min, to_max, reverse)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/tuya/util.py", line 16, in remap_value
    return ((value - from_min) / (from_max - from_min)) * (to_max - to_min) + to_min
             ~~~~~~^~~~~~~~~~
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Please let me know if you need any additional info

Copy link
Member

@frenck frenck Mar 18, 2024

Choose a reason for hiding this comment

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

Right, but that is based on an error. Why does that light communicate non-standard via their cloud? Have you verified with Tuya this is expected for this device? What was their response? Sounds like an upstream bug...

Copy link
Author

Choose a reason for hiding this comment

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

@frenck I haven't communicated with them. Haven't got time to dig deep into this. I can find some bugs reported in the actual repo: tuya/tuya-device-sharing-sdk#11

I am not a Tuya API expert so the stuff I can suggest to the team is limited.

Copy link
Member

Choose a reason for hiding this comment

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

In that case, I'm going to friendly decline this PR.

As without communication, this could be an upstream issue that can be easily fixed. I don't think we should riddle our code with exceptions to handle possible upstream issues unless we can settle on whether this isn't fixable otherwise.

../Frenck

@home-assistant home-assistant bot marked this pull request as draft March 18, 2024 15:54
@frenck frenck closed this Mar 18, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 19, 2024
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.

tuya: unsupported operand type(s) for -: 'int' and 'str'
2 participants