home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.67k stars 30.43k forks source link

Tuya - remap_value - TypeError: unsupported operand type(s) for -: 'int' and 'str' #64998

Closed fOmey closed 2 years ago

fOmey commented 2 years ago

The problem

Additional tuya fan support was recently added #64947.

In my original issue report #64762 I mentioned some fatal errors appearing when toggling colour temp / light.

Fatal errors are now appearing when toggling the fan.. prior to pulling the recent changes I was able to toggle the fan, not the light. Now I'm unable to toggle the light and the fan.

In both cases they seem to be related to the utility function remap_value() in the tuya component.

  File "/config/custom_components/tuya/fan.py", line 251, in percentage
    return int(self._speed.remap_value_to(value, 0, 100))
  File "/config/custom_components/tuya/base.py", line 62, in remap_value_to
    return remap_value(value, self.min, self.max, to_min, to_max, reverse)
  File "/config/custom_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 -: 'int' and 'str'

My original issue was closed with a half cocked resolution, this issue has now became worse after pulling the latest changes.

What version of Home Assistant Core has the issue?

core-2021.12.10

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Tuya

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tuya/

Example YAML snippet

No response

Anything in the logs that might be useful for us?

[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] done.
[services.d] starting services
[services.d] done.
     
2022-01-27 06:55:23 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
     
2022-01-27 06:55:23 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration tuya which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-01-27 06:55:28 WARNING (MainThread) [slixmpp.stringprep] Using slower stringprep, consider compiling the faster cython/libidn one.
2022-01-27 06:55:30 WARNING (MainThread) [slixmpp.basexmpp] Legacy XMPP 0.9 protocol detected.
2022-01-27 06:55:31 ERROR (MainThread) [homeassistant.components.fan] Error adding entities for domain fan with platform tuya
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 613, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 748, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 540, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 630, in state_attributes
    data[ATTR_SPEED] = self.speed
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 471, in speed
    if (percentage := self.percentage) is None:
  File "/config/custom_components/tuya/fan.py", line 251, in percentage
    return int(self._speed.remap_value_to(value, 0, 100))
  File "/config/custom_components/tuya/base.py", line 62, in remap_value_to
    return remap_value(value, self.min, self.max, to_min, to_max, reverse)
  File "/config/custom_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 -: 'int' and 'str'
2022-01-27 06:55:31 ERROR (MainThread) [homeassistant.components.fan] Error while setting up tuya platform for fan
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 257, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 613, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 748, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 540, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 630, in state_attributes
    data[ATTR_SPEED] = self.speed
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 471, in speed
    if (percentage := self.percentage) is None:
  File "/config/custom_components/tuya/fan.py", line 251, in percentage
    return int(self._speed.remap_value_to(value, 0, 100))
  File "/config/custom_components/tuya/base.py", line 62, in remap_value_to
    return remap_value(value, self.min, self.max, to_min, to_max, reverse)
  File "/config/custom_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 -: 'int' and 'str'
2022-01-27 06:55:36 ERROR (SyncWorker_4) [homeassistant] Error doing job: Task was destroyed but it is pending!
2022-01-27 06:55:45 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.load_1m (None) does not match the unit of already compiled statistics ( ). Generation of long term statistics will be suppressed unless the unit changes back to  
2022-01-27 06:55:45 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.load_5m (None) does not match the unit of already compiled statistics ( ). Generation of long term statistics will be suppressed unless the unit changes back to  
2022-01-27 06:55:45 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.load_15m (None) does not match the unit of already compiled statistics ( ). Generation of long term statistics will be suppressed unless the unit changes back to

Additional information

No response

probot-home-assistant[bot] commented 2 years ago

tuya documentation tuya source (message by IssueLinks)

probot-home-assistant[bot] commented 2 years ago

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

fOmey commented 2 years ago

As a sanitity check I pulled the latest dev branch core-2022.2.0.dev20220126, issue persists.

frenck commented 2 years ago

If you can and want to, you could try the beta which just started.

It has the ability to download diagnostics on a device page, which can be helpful in this case

fOmey commented 2 years ago

If you can and want to, you could try the beta which just started.

It has the ability to download diagnostics on a device page, which can be helpful in this case

I've just pulled and installed 2022.2.0b0, happy to assist anyway I can.

I noticed when reverting back the latest stable, when toggling the fan on, the speed on the home assistant interface remained on 0 even tho the fan begun spinning.

I'm assuming this is why the crash is not occuring while using the stable, its not retrieving the speed? Only setting it?

I noticed similar behaviour when I added basic support for the light, I was able to toggle the light on/off.. however the crash occured only when retrieving the status of the light.

2022.2.0b0:

[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
Found 2 non-daemonic threads.
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] done.
[services.d] starting services
[services.d] done.
     
2022-01-27 07:58:19 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-01-27 07:58:24 WARNING (MainThread) [slixmpp.stringprep] Using slower stringprep, consider compiling the faster cython/libidn one.
2022-01-27 07:58:27 WARNING (MainThread) [slixmpp.basexmpp] Legacy XMPP 0.9 protocol detected.
2022-01-27 07:58:28 ERROR (MainThread) [homeassistant.components.fan] Error adding entities for domain fan with platform tuya
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 614, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 773, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 530, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 565, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 475, in state_attributes
    data[ATTR_SPEED] = self.speed
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 389, in speed
    if (percentage := self.percentage) is None:
  File "/usr/src/homeassistant/homeassistant/components/tuya/fan.py", line 251, in percentage
    return int(self._speed.remap_value_to(value, 0, 100))
  File "/usr/src/homeassistant/homeassistant/components/tuya/base.py", line 62, 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 -: 'int' and 'str'
2022-01-27 07:58:28 ERROR (MainThread) [homeassistant] Error doing job: Task was destroyed but it is pending!
2022-01-27 07:58:28 ERROR (MainThread) [homeassistant.components.fan] Error while setting up tuya platform for fan
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 257, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 614, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 773, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 530, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 565, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 475, in state_attributes
    data[ATTR_SPEED] = self.speed
  File "/usr/src/homeassistant/homeassistant/components/fan/__init__.py", line 389, in speed
    if (percentage := self.percentage) is None:
  File "/usr/src/homeassistant/homeassistant/components/tuya/fan.py", line 251, in percentage
    return int(self._speed.remap_value_to(value, 0, 100))
  File "/usr/src/homeassistant/homeassistant/components/tuya/base.py", line 62, 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 -: 'int' and 'str'
fOmey commented 2 years ago

@frenck

Fan diagnostics:

{
  "home_assistant": {
    "installation_type": "Home Assistant Container",
    "version": "2022.2.0b0",
    "dev": false,
    "hassio": false,
    "virtualenv": false,
    "python_version": "3.9.7",
    "docker": true,
    "arch": "aarch64",
    "timezone": "Australia/Sydney",
    "os_name": "Linux",
    "os_version": "4.9.277-meson64",
    "run_as_root": true
  },
  "custom_components": {
    "hacs": {
      "version": "1.19.3",
      "requirements": [
        "aiogithubapi>=21.11.0"
      ]
    }
  },
  "integration_manifest": {
    "domain": "tuya",
    "name": "Tuya",
    "documentation": "https://www.home-assistant.io/integrations/tuya",
    "requirements": [
      "tuya-iot-py-sdk==0.6.6"
    ],
    "dependencies": [
      "ffmpeg"
    ],
    "codeowners": [
      "@Tuya",
      "@zlinoliver",
      "@METISU",
      "@frenck"
    ],
    "config_flow": true,
    "iot_class": "cloud_push",
    "dhcp": [
      {
        "macaddress": "105A17*"
      },
      {
        "macaddress": "10D561*"
      },
      {
        "macaddress": "1869D8*"
      },
      {
        "macaddress": "381F8D*"
      },
      {
        "macaddress": "508A06*"
      },
      {
        "macaddress": "68572D*"
      },
      {
        "macaddress": "708976*"
      },
      {
        "macaddress": "7CF666*"
      },
      {
        "macaddress": "84E342*"
      },
      {
        "macaddress": "D4A651*"
      },
      {
        "macaddress": "D81F12*"
      }
    ],
    "is_built_in": true
  },
  "data": {
    "endpoint": "https://openapi.tuyaeu.com",
    "auth_type": 0,
    "country_code": "44",
    "app_type": "tuyaSmart",
    "mqtt_connected": true,
    "disabled_by": null,
    "disabled_polling": false,
    "name": "Fan",
    "model": "MZ-DCFAN",
    "category": "fs",
    "product_id": "agyqzsfpafsvkrl9",
    "product_name": "DC\u540a\u6247",
    "online": true,
    "sub": false,
    "time_zone": "+11:00",
    "active_time": "2022-01-23T20:31:46+00:00",
    "create_time": "2022-01-23T10:47:03+00:00",
    "update_time": "2022-01-24T09:52:52+00:00",
    "function": {
      "switch": {
        "type": "Boolean",
        "value": {}
      },
      "fan_speed_percent": {
        "type": "Integer",
        "value": {
          "min": "1",
          "max": "6",
          "scale": "0",
          "step": "1"
        }
      },
      "light": {
        "type": "Boolean",
        "value": {}
      },
      "bright_value": {
        "type": "Integer",
        "value": {
          "min": "10",
          "max": "1000",
          "scale": "0",
          "step": "1"
        }
      },
      "temp_value": {
        "type": "Integer",
        "value": {
          "min": "0",
          "max": "1000",
          "scale": "0",
          "step": "1"
        }
      }
    },
    "status_range": {
      "switch": {
        "type": "Boolean",
        "value": {}
      },
      "fan_speed_percent": {
        "type": "Integer",
        "value": {
          "min": "1",
          "max": "6",
          "scale": "0",
          "step": "1"
        }
      },
      "light": {
        "type": "Boolean",
        "value": {}
      },
      "bright_value": {
        "type": "Integer",
        "value": {
          "min": "10",
          "max": "1000",
          "scale": "0",
          "step": "1"
        }
      },
      "temp_value": {
        "type": "Integer",
        "value": {
          "min": "0",
          "max": "1000",
          "scale": "0",
          "step": "1"
        }
      }
    },
    "status": {
      "switch": false,
      "fan_speed_percent": 6,
      "light": false,
      "bright_value": 1000,
      "temp_value": 550
    },
    "home_assistant": {
      "name": "Fan",
      "name_by_user": null,
      "disabled": false,
      "disabled_by": null,
      "entities": [
        {
          "disabled": false,
          "disabled_by": null,
          "entity_category": null,
          "device_class": null,
          "original_device_class": null,
          "icon": null,
          "original_icon": null,
          "unit_of_measurement": null,
          "state": {
            "entity_id": "fan.fan",
            "state": "unavailable",
            "attributes": {
              "restored": true,
              "speed_list": [
                "off",
                "low",
                "medium",
                "high"
              ],
              "preset_modes": [],
              "friendly_name": "Fan",
              "supported_features": 1
            },
            "last_changed": "2022-01-26T20:58:44.052097+00:00",
            "last_updated": "2022-01-26T20:58:44.052097+00:00"
          }
        },
        {
          "disabled": false,
          "disabled_by": null,
          "entity_category": null,
          "device_class": null,
          "original_device_class": null,
          "icon": null,
          "original_icon": null,
          "unit_of_measurement": null,
          "state": {
            "entity_id": "light.fan",
            "state": "off",
            "attributes": {
              "min_mireds": 153,
              "max_mireds": 500,
              "supported_color_modes": [
                "brightness",
                "color_temp",
                "onoff"
              ],
              "friendly_name": "Fan",
              "supported_features": 0
            },
            "last_changed": "2022-01-26T20:58:28.465042+00:00",
            "last_updated": "2022-01-26T20:58:28.465042+00:00"
          }
        }
      ]
    }
  }
}
frenck commented 2 years ago

@fOmey That is really helpful, thanks!

It actually reveals the issue, which probably explains quite a lot more reported ones.

fOmey commented 2 years ago

@fOmey That is really helpful, thanks!

It actually reveals the issue, which probably explains quite a lot more reported ones.

Great!

It's not something as silly as a casting issue? Treating strings as int's?

frenck commented 2 years ago

It's not something as silly as a casting issue? Treating strings as int's?

Well, something like that yeah... Strings are received from the API instead of numeric values. Which is odd and not according to specifications; but something we can work with.

frenck commented 2 years ago

@fOmey Could you maybe try and test against the second beta? (2022.2.0b1)

fOmey commented 2 years ago

@fOmey Could you maybe try and test against the second beta? (2022.2.0b1)

@frenck

Your changes have resolved this issue, no more crashing.. logs looking nice and clean! 👍

I've noticed the fan on/off status doesnt seem to be accurately detected by home assistant, the fan speed doesnt seem to be adjustable either.. that is a different issue tho.

Light function status also doesnt seem to be detected by home assistant, however brightness & colour temp controls do seem to be working.