Skip to content

Sensor init fails if unit of measurement is a string #326

@tr4nt0r

Description

@tr4nt0r

When providing a unit in the tuya V2 quirk builder that is a string (instead of an Enum) the sensor init fails.
I used the unit lx, which is defined in the const LIGHT_LUX

    .tuya_sensor(
        dp_id=101,
        attribute_name="measured_value",
        type=t.uint16_t,
        fallback_name="Illuminance",
        device_class=SensorDeviceClass.ILLUMINANCE,
        state_class=SensorStateClass.MEASUREMENT,
        unit=LIGHT_LUX,
    )

Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/zha/application/gateway.py", line 335, in create_platform_entities
    platform_entity = platform_entity_class.create_platform_entity(
        *args, **kw_args
    )
  File "/usr/local/lib/python3.13/site-packages/zha/application/platforms/sensor/__init__.py", line 186, in create_platform_entity
    return cls(unique_id, cluster_handlers, endpoint, device, **kwargs)
  File "/usr/local/lib/python3.13/site-packages/zha/application/platforms/sensor/__init__.py", line 198, in __init__
    super().__init__(unique_id, cluster_handlers, endpoint, device, **kwargs)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/zha/application/platforms/__init__.py", line 297, in __init__
    self._init_from_quirks_metadata(entity_metadata)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/zha/application/platforms/sensor/__init__.py", line 220, in _init_from_quirks_metadata
    self._attr_native_unit_of_measurement = validate_unit(
                                            ~~~~~~~~~~~~~^
        entity_metadata.unit
        ^^^^^^^^^^^^^^^^^^^^
    ).value
    ^
  File "/usr/local/lib/python3.13/site-packages/zha/units.py", line 181, in validate_unit
    return UNITS_OF_MEASURE[type(external_unit).__name__](external_unit.value)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'str'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions