Skip to content

ZHA patch for more precision display of data for all Zigbee devices. #6

@pvvx

Description

@pvvx

ZHA patch for more precision display of data for all Zigbee devices.

New version Home Assistant
Core: 2024.10.2
Frontend: 20241002.3

Accuracy of values ​​according to the Zigbee standard:

Find the file zha/application/platforms/sensor/__init__.py (base path ~/lib64/python3.xx/site-packages/zha/application/platforms/sensor) and fix it _decimals: int = 1 to _decimals: int = 2:

class Sensor(PlatformEntity):
    """Base ZHA sensor."""

    PLATFORM = Platform.SENSOR
    _attribute_name: int | str | None = None
    _decimals: int = 2
    _divisor: int = 1

Battery percent - step 0.5%

~/lib64/python3.xx/site-packages/zha/application/platforms/sensor/init.py

    @staticmethod
    def formatter(value: int) -> int | None:  # pylint: disable=arguments-differ
        """Return the state of the entity."""
        # per zcl specs battery percent is reported at 200% ¯\_(ツ)_/¯
        if not isinstance(value, numbers.Number) or value in (-1, 255):
            return None
        value = round(value / 2, 1)
        return value

REPORT_CONFIG

Find the file zha/zigbee/cluster_handlers/measurement.py (base path ~/lib64/python3.xx/site-packages/zha/zigbee/cluster_handlers/measurement.py) and fix it REPORT_CONFIG step value:

class RelativeHumidityClusterHandler(ClusterHandler):
    """Relative Humidity measurement cluster handler."""

    REPORT_CONFIG = (
        AttrReportConfig(
            attr=RelativeHumidity.AttributeDefs.measured_value.name,
            config=(REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, 50),
        ),
    )

...

class TemperatureMeasurementClusterHandler(ClusterHandler):
    """Temperature measurement cluster handler."""

    REPORT_CONFIG = (
        AttrReportConfig(
            attr=TemperatureMeasurement.AttributeDefs.measured_value.name,
            config=(REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, 10),
        ),
    )

REPORT_CONFIG_MAX_INT, REPORT_CONFIG_MIN_INT

~/lib64/python3.xx/site-packages/zha/zigbee/cluster_handlers/const.py

REPORT_CONFIG_ATTR_PER_REQ: Final[int] = 3
REPORT_CONFIG_MAX_INT: Final[int] = 180
REPORT_CONFIG_MAX_INT_BATTERY_SAVE: Final[int] = 10800
REPORT_CONFIG_MIN_INT: Final[int] = 30
REPORT_CONFIG_MIN_INT_ASAP: Final[int] = 1
REPORT_CONFIG_MIN_INT_IMMEDIATE: Final[int] = 0
REPORT_CONFIG_MIN_INT_OP: Final[int] = 5
REPORT_CONFIG_MIN_INT_BATTERY_SAVE: Final[int] = 3600
REPORT_CONFIG_RPT_CHANGE: Final[int] = 1

Old version ZHA / Home Assistant:

Find the file components/zha/sensor.py (base path ~/.lib64/python3.xx/site-packages/homeassistant) and fix it:

class Sensor(ZhaEntity, SensorEntity):
    """Base ZHA sensor."""

    _attribute_name: int | str | None = None
    _decimals: int = 2
    _divisor: int = 1
    _multiplier: int | float = 1

        """Return the state of the entity."""
        # per zcl specs battery percent is reported at 200% ¯\_(ツ)_/¯
        if not isinstance(value, numbers.Number) or value == -1:
            return None
        value = round(value / 2, 1)
        return value

site-packages/homeassistant/components/zha/core/cluster_handlers/general.py :


class DeviceTemperature(ClusterHandler):
    """Device Temperature cluster handler."""

    REPORT_CONFIG = (
        {
            "attr": "current_temperature",
            "config": (REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, 10),
        },
    )

site-packages/homeassistant/components/zha/core/cluster_handlers/measurement.py :


class RelativeHumidity(ClusterHandler):
    """Relative Humidity measurement cluster handler."""

    REPORT_CONFIG = (
        AttrReportConfig(
            attr="measured_value",
            config=(REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, 50),
        ),
    )

...

class TemperatureMeasurement(ClusterHandler):
    """Temperature measurement cluster handler."""

    REPORT_CONFIG = (
        AttrReportConfig(
            attr="measured_value",
            config=(REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, 10),
        ),
    )

site-packages/homeassistant/components/zha/core/const.py :

REPORT_CONFIG_ATTR_PER_REQ = 3
REPORT_CONFIG_MAX_INT = 180
REPORT_CONFIG_MAX_INT_BATTERY_SAVE = 10800
REPORT_CONFIG_MIN_INT = 30
REPORT_CONFIG_MIN_INT_ASAP = 1

image

image

image

image


https://community.home-assistant.io/t/provide-higher-precision-temperature-value-unrounded-values/502245

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions