jason0x43 / hacs-hubitat

A Hubitat integration for Home Assistant
MIT License
189 stars 46 forks source link

Error when sensor reports string instead of numeric value #263

Closed monkmad closed 4 months ago

monkmad commented 4 months ago

Describe the bug

I have a CO/Smoke sensor which reports the status of "Carbon Monoxide" and "Smoke" as "clear" instead of 0 (zero). This results in an error since Home Assistant expects a numeric value. I'm not sure if this is related to how the HACS-hubitat integration first setup the sensor, or if it is a HomeAssistant feature (bug)

To Reproduce

  1. Pair an Ecolink Smoke/CO Audio Detector (Z-Wave) with Hubitat
  2. Wait for the device to be created and sync'd in Home Assistant via the HACS-hubitat integration
  3. Enable debug logs and observe
  4. See error in logs

Screenshots/Logs

2024-03-05 17:08:49.997 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform hubitat
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 669, in state
    numerical_value = float(value)  # type:ignore[arg-type]
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: 'clear'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 533, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 782, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1325, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 989, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1110, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1047, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 995, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 671, in state
    raise ValueError(
ValueError: Sensor sensor.ecolink_smoke_co_audio_detector_carbonmonoxide has device class 'carbon_monoxide', state class 'measurement' unit 'ppm' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'clear' (<class 'str'>)

Environment

jason0x43 commented 4 months ago

Could you grab the device capabilities for your device? I know what the issue is, but it'd nice to verify. The problem is that the integration currently treats both "carbon monoxide" and "carbon monoxide level" as measurements, but the non-level attribute should probably be a binary sensor.

monkmad commented 4 months ago

Sure; here you go. Looks like it is an ENUM with three values (clear, tested, detected)

[
    {
        "capabilities": [
            "Refresh",
            "Battery",
            {
                "attributes": [
                    {
                        "name": "battery",
                        "currentValue": "100",
                        "dataType": "NUMBER",
                        "values": null
                    }
                ]
            },
            "TamperAlert",
            {
                "attributes": [
                    {
                        "name": "tamper",
                        "currentValue": "clear",
                        "dataType": "ENUM",
                        "values": [
                            "clear",
                            "detected"
                        ]
                    }
                ]
            },
            "SmokeDetector",
            {
                "attributes": [
                    {
                        "name": "smoke",
                        "currentValue": "clear",
                        "dataType": "ENUM",
                        "values": [
                            "clear",
                            "tested",
                            "detected"
                        ]
                    }
                ]
            },
            "CarbonMonoxideDetector",
            {
                "attributes": [
                    {
                        "name": "carbonMonoxide",
                        "currentValue": "clear",
                        "dataType": "ENUM",
                        "values": [
                            "detected",
                            "tested",
                            "clear"
                        ]
                    }
                ]
            },
            "TemperatureMeasurement",
            {
                "attributes": [
                    {
                        "name": "temperature",
                        "currentValue": "72.50",
                        "dataType": "NUMBER",
                        "values": null
                    }
                ]
            },
            "Sensor"
        ]
    }
]
jason0x43 commented 4 months ago

This should be fixed in v0.9.25.

monkmad commented 4 months ago

Thanks again for the super fast turnaround! This seems to have eliminated the issue; I don't see the error in the logs anymore.

monkmad commented 4 months ago

Closing the issue as it is fixed in v0.9.25