-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Hey there @tuya, @zlinoliver, @frenck, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
302fb1c
to
8269881
Compare
# 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Breaking change
n/a
Proposed change
unsupported operand type(s) for -: 'int' and 'str'
is fixed with this.Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: