Skip to content

validate_unit doesn't allow valid values #327

@TheJulianJES

Description

@TheJulianJES

First, validate_unit typing is Enum, although it's typed as str everywhere else, so in quirks v2 metadata, and so on.
In ZHA and HA, _attr_native_unit_of_measurement is also typed as str | None.

It should be changed to accept and return a str IMO.

The mapping is also not ideal, as we have units replicated in both zigpy and ZHA now. We should change this in the future, e.g. by just having it in zigpy (and maybe renaming the quirks/v2 where it is currently to something else). Or a separate lib?

We also need to fix not accepting the units strings that are defined, like PERCENTAGE. We need to allow those.
Actually, I don't think we really need the validation for a string anyways. Quirks v2 entities are still reviewed and obviously need to use the constants defined here before they can be merged.
HA might also already validate the units used?

Link to method (and units file):

zha/zha/units.py

Lines 179 to 181 in c013c5b

def validate_unit(external_unit: Enum) -> Enum:
"""Validate and return a unit of measure."""
return UNITS_OF_MEASURE[type(external_unit).__name__](external_unit.value)

cc @prairiesnpr

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions