Skip to content

Commit

Permalink
Add custom firmware z03mmc quirk and test
Browse files Browse the repository at this point in the history
  • Loading branch information
gigamonster256 committed Aug 28, 2024
1 parent 5c4e08d commit d864e35
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/test_xiaomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,3 +1757,30 @@ def test_aqara_acn014_signature_match(assert_signature_matches_quirk):
assert_signature_matches_quirk(
zhaquirks.xiaomi.aqara.light_acn.LumiLightAcn014, signature
)


def test_custom_z03mmc_signature_match(assert_signature_matches_quirk):
"""Test signature."""
signature = {
"node_descriptor": "NodeDescriptor(logical_type=<LogicalType.EndDevice: 2>, complex_descriptor_available=0, user_descriptor_available=0, reserved=0, aps_flags=0, frequency_band=<FrequencyBand.Freq2400MHz: 8>, mac_capability_flags=<MACCapabilityFlags.AllocateAddress: 128>, manufacturer_code=56085, maximum_buffer_size=74, maximum_incoming_transfer_size=404, server_mask=10752, maximum_outgoing_transfer_size=404, descriptor_capability_field=<DescriptorCapability.NONE: 0>, *allocate_address=True, *is_alternate_pan_coordinator=False, *is_coordinator=False, *is_end_device=True, *is_full_function_device=False, *is_mains_powered=False, *is_receiver_on_when_idle=False, *is_router=False, *is_security_capable=False)",
"endpoints": {
"1": {
"profile_id": 0x0104,
"device_type": "0x0302",
"in_clusters": [
"0x0000",
"0x0001",
"0x0003",
"0x0204",
"0x0402",
"0x0405",
],
"out_clusters": ["0x0019"],
}
},
"manufacturer": "Xiaomi",
"model": "LYWSD03MMC",
"class": "zigpy.device.Device",
}

assert_signature_matches_quirk(zhaquirks.xiaomi.custom.z03mmc.LYWSD03MMC, signature)
1 change: 1 addition & 0 deletions zhaquirks/xiaomi/custom/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Module for Xiaomi custom firmware quirks implementations."""
153 changes: 153 additions & 0 deletions zhaquirks/xiaomi/custom/z03mmc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
"""Xiaomi LYWSD03MMC Bluetooth temperature and humidity sensor."""

from zigpy.profiles import zha
from zigpy.types import Bool, int16s
from zigpy.zcl.clusters.general import Basic, Identify, Ota, PowerConfiguration
from zigpy.zcl.clusters.hvac import UserInterface
from zigpy.zcl.clusters.measurement import RelativeHumidity, TemperatureMeasurement
from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef

from zhaquirks import CustomCluster
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.xiaomi import XiaomiCustomDevice


class CalibratableTemperatureMeasurementCluster(CustomCluster, TemperatureMeasurement):
"""Temperature Measurement Cluster with calibration attribute."""

class AttributeDefs(TemperatureMeasurement.AttributeDefs):
"""Attribute Definitions."""

# A value in 0.01ºC offset to fix up incorrect values from sensor
temperature_calibration = ZCLAttributeDef(
id=0x0010,
type=int16s,
access="rw",
is_manufacturer_specific=True,
)


class CalibratableRelativeHumidityCluster(CustomCluster, RelativeHumidity):
"""Relative Humidity Cluster with calibration attribute."""

class AttributeDefs(RelativeHumidity.AttributeDefs):
"""Attribute Definitions."""

# A value in 0.01%RH offset to fix up incorrect values from sensor
humidity_calibration = ZCLAttributeDef(
id=0x0010,
type=int16s,
access="rw",
is_manufacturer_specific=True,
)


class SmileyUserInterfaceCluster(CustomCluster, UserInterface):
"""Custom User Interface Cluster with smiley control."""

class AttributeDefs(BaseAttributeDefs):
"""Attribute Definitions."""

# of the 3 ZCL spec attributes, only the first one (TemperatureDisplayMode) is implemented
# KeypadLockout is implemented but completely unused in the device firmware
# ScheduleProgrammingVisibility is not implemented at all
# https://github.com/devbis/z03mmc/blob/1.1.0/src/sensorEpCfg.c#L256
temperature_display_mode = UserInterface.AttributeDefs.temperature_display_mode

# 0 - smiley is off, 1 - smiley is on (according to comfort values)
smiley = ZCLAttributeDef(
id=0x0010,
type=Bool,
access="rw",
is_manufacturer_specific=True,
)

# display. 0 - display is off, 1 - display is on
display = ZCLAttributeDef(
id=0x0011,
type=Bool,
access="rw",
is_manufacturer_specific=True,
)

# comfort temperature min: A value in 0.01ºC to set minimum comfort temperature for happy face
comfort_temperature_min = ZCLAttributeDef(
id=0x0102,
type=int16s,
access="rw",
is_manufacturer_specific=True,
)

# comfort temperature max: A value in 0.01ºC to set maximum comfort temperature for happy face
comfort_temperature_max = ZCLAttributeDef(
id=0x0103,
type=int16s,
access="rw",
is_manufacturer_specific=True,
)

# comfort humidity min: A value in 0.01%RH to set minimum comfort humidity for happy face
comfort_humidity_min = ZCLAttributeDef(
id=0x0104,
type=int16s,
access="rw",
is_manufacturer_specific=True,
)

# comfort humidity max: A value in 0.01%RH to set maximum comfort humidity for happy face
comfort_humidity_max = ZCLAttributeDef(
id=0x0105,
type=int16s,
access="rw",
is_manufacturer_specific=True,
)


# https://github.com/devbis/z03mmc
# defined by 1.1.0 firmware (0x11003001)
# see README.md in the repo for more info
class LYWSD03MMC(XiaomiCustomDevice):
"""LYWSD03MMC sensor."""

signature = {
MODELS_INFO: [("Xiaomi", "LYWSD03MMC")],
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.TEMPERATURE_SENSOR,
INPUT_CLUSTERS: [
Basic.cluster_id,
Identify.cluster_id,
PowerConfiguration.cluster_id,
RelativeHumidity.cluster_id,
TemperatureMeasurement.cluster_id,
UserInterface.cluster_id,
],
OUTPUT_CLUSTERS: [Ota.cluster_id],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zha.PROFILE_ID,
DEVICE_TYPE: zha.DeviceType.TEMPERATURE_SENSOR,
INPUT_CLUSTERS: [
Basic.cluster_id,
Identify.cluster_id,
PowerConfiguration.cluster_id,
CalibratableTemperatureMeasurementCluster,
CalibratableRelativeHumidityCluster,
SmileyUserInterfaceCluster,
],
OUTPUT_CLUSTERS: [Ota.cluster_id],
},
},
}

0 comments on commit d864e35

Please sign in to comment.