zha-device-handlers: _TZE204_ntcy3xu1 Smoke detector, battery missing
Bug description
Hi, I own a Tuya smoke detector. With the released quirk, only the smoke sensor is exposed, but the battery entity is missing.
for Model _TZE204_ntcy3xu1.
I have a custom quirk (see below), that offers the battery sensor, but the value readout is not correct. It jumps from 100% immediately to 5%
Steps to reproduce
Na
Expected behavior
Please Include the battery entity as well.
Screenshots/Video
Screenshots/Video
[Paste/upload your media here]
Device signature
Device signature
{
"node_descriptor": {
"logical_type": 2,
"complex_descriptor_available": 0,
"user_descriptor_available": 0,
"reserved": 0,
"aps_flags": 0,
"frequency_band": 8,
"mac_capability_flags": 128,
"manufacturer_code": 4417,
"maximum_buffer_size": 66,
"maximum_incoming_transfer_size": 66,
"server_mask": 10752,
"maximum_outgoing_transfer_size": 66,
"descriptor_capability_field": 0
},
"endpoints": {
"1": {
"profile_id": "0x0104",
"device_type": "0x0402",
"input_clusters": [
"0x0000",
"0x0001",
"0x0004",
"0x0005",
"0x000f",
"0x0500",
"0xef00"
],
"output_clusters": [
"0x000a",
"0x0019"
]
}
},
"manufacturer": "_TZE204_ntcy3xu1",
"model": "TS0601",
"class": "TS0601_TZE204_ntcy3xu1.TuyaSmokeDetector0601"
}
Diagnostic information
Diagnostic information
[Paste the diagnostic information here]
Logs
Logs
[Paste the logs here]
Additional information
Custom quirk
import logging
import zigpy.profiles.zha
from zigpy.quirks import CustomDevice
import zigpy.types as t
from zigpy.zcl.clusters.general import (
Basic,
Groups,
Ota,
Scenes,
Time,
BinaryInput,
BatterySize,
)
from zigpy.zcl.clusters.security import IasZone
from zhaquirks import Bus
from zhaquirks.const import (
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
ZONE_TYPE,
)
from zhaquirks.tuya import (
TuyaLocalCluster,
TuyaManufCluster,
TuyaManufClusterAttributes,
TuyaPowerConfigurationCluster,
)
_LOGGER = logging.getLogger(__name__)
TUYA_SMOKE_DETECTED_ATTR = 0x0401 # [0]/[1] [Detected]/[Clear]!
TUYA_SMOKE_TAMPERED_ATTR = 0x0104 # [0]/[1] [Clear]/[Tampered]!
TUYA_SMOKE_BATTERY_ATTR = 0x040e # [0]/[1]/[2] [Low]/[Med]]/[Full]!
class TuyaSmokeDetectorCluster(TuyaManufClusterAttributes):
"""Manufacturer Specific Cluster of the TS0601 smoke detector."""
attributes = {
TUYA_SMOKE_DETECTED_ATTR: ("smoke_detected", t.uint8_t, True),
TUYA_SMOKE_TAMPERED_ATTR: ("tampered_device", t.uint8_t, True),
TUYA_SMOKE_BATTERY_ATTR: ("battery_status", t.uint8_t, True),
}
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == TUYA_SMOKE_DETECTED_ATTR:
status = IasZone.ZoneStatus.Alarm_1 if value == 0 else 0
self.endpoint.device.ias_bus.listener_event("update_attribute", "zone_status", status)
elif attrid == TUYA_SMOKE_TAMPERED_ATTR:
self.endpoint.device.tamper_detection_bus.listener_event(
"update_attribute", "present_value", bool(value)
)
elif attrid == TUYA_SMOKE_BATTERY_ATTR:
batt = 5 if value == 0 else 40 if value == 1 else 100
self.endpoint.device.battery_bus.listener_event("battery_change", batt)
else:
_LOGGER.warning(
"[0x%04x:%s:0x%04x] unhandled attribute: 0x%04x",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
attrid,
)
class TuyaIasZone(TuyaLocalCluster, IasZone):
"""
IAS Zone: this generates the "Smoke" entity for HA.
Receives updates from TuyaSmokeDetectorCluster.
"""
_CONSTANT_ATTRIBUTES = {ZONE_TYPE: IasZone.ZoneType.Fire_Sensor}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.ias_bus.add_listener(self)
class TuyaPowerConfigCluster(TuyaPowerConfigurationCluster):
"""
Power Config Cluster: this generates the "Battery" entity for HA.
Receives updates from TuyaSmokeDetectorCluster.
"""
_CONSTANT_ATTRIBUTES = {
TuyaPowerConfigurationCluster.attributes_by_name["battery_size"].id: BatterySize.AAA,
TuyaPowerConfigurationCluster.attributes_by_name["battery_quantity"].id: 2,
TuyaPowerConfigurationCluster.attributes_by_name["battery_rated_voltage"].id: 15,
}
class TuyaTamperDetection(TuyaLocalCluster, BinaryInput):
"""
Tamper Detection Cluster: this generates the "Binary input" entity for HA, which is updated
with the Tampered state. Receives updates from TuyaSmokeDetectorCluster.
"""
_CONSTANT_ATTRIBUTES = {
BinaryInput.attributes_by_name["description"].id: "Tamper Detected",
BinaryInput.attributes_by_name["active_text"].id: "Tampered",
BinaryInput.attributes_by_name["inactive_text"].id: "Clear",
}
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.tamper_detection_bus.add_listener(self)
class TuyaSmokeDetector0601(CustomDevice):
"""TS0601 Smoke detector quirk."""
def __init__(self, *args, **kwargs):
"""Init."""
self.ias_bus = Bus()
self.battery_bus = Bus()
self.tamper_detection_bus = Bus()
super().__init__(*args, **kwargs)
signature = {
MODELS_INFO: [
("_TZE204_ntcy3xu1", "TS0601"),
],
ENDPOINTS: {
1: {
PROFILE_ID: zigpy.profiles.zha.PROFILE_ID,
DEVICE_TYPE: zigpy.profiles.zha.DeviceType.SMART_PLUG,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaManufCluster.cluster_id,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}
replacement = {
ENDPOINTS: {
1: {
PROFILE_ID: zigpy.profiles.zha.PROFILE_ID,
DEVICE_TYPE: zigpy.profiles.zha.DeviceType.IAS_ZONE,
INPUT_CLUSTERS: [
Basic.cluster_id,
Groups.cluster_id,
Scenes.cluster_id,
TuyaIasZone,
TuyaSmokeDetectorCluster,
TuyaPowerConfigCluster,
TuyaTamperDetection,
],
OUTPUT_CLUSTERS: [
Time.cluster_id,
Ota.cluster_id,
],
},
},
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 27
@prairiesnpr Sorry, I have another small question. Since I played with this custom quirk, Reloading the ZHA integration gives this error:
zigpy.exceptions.MultipleQuirksMatchException: Multiple matches found for device <Device model='TS0601' manuf='_TZE204_ntcy3xu1'When I fully restart HA, ZHA loads normal, but reloading still will fail. Is there some cache I can cleanup ?!Update: found the cause: https://github.com/zigpy/zigpy/issues/1508, This work around works:
Final custom quirk for TZE204_ntcy3xu1
Thanks again for all the help
Best Eric
That’s correct, see https://github.com/prairiesnpr/zha-device-handlers/blob/dee4296a65f49efee31231fd9807b5c3b67bb45b/zhaquirks/tuya/builder/__init__.py#L122
Since all of the smoke quirks used the same logic, I took the opportunity to add a helper method to TuyaQuirkBuilder and update all of the old style quirks in the same PR.
Perfect, PR is updated once it’s merged you should be good.
Changed the line and waiting for the next battery update… I start to understand the logging 😃 The raw battery value in the log is 0x02, so doubling seems right.
Update: with this ‘doubling modification’ it reports 100% for raw value 0x02. These relevant lines where in the log when pressing the test button:
Thanks for your help
Zigbee values are supposed to be doubled, I don’t recall where the conversation takes place right now though. You can try
converter=lambda x: {0: 10, 1: 80, 2: 200}[x],I’m at work right now, will look at your logs this evening.
Good news on the inversion, I’ll correct the PR tonight.
My bad, cp error, needed
.adds(TuyaIAS), added it above.Test it tomorrow 👌
I logged with my old V1 quirk. Device info:
Look at timestamp 15:42:19.464
Thanks for your help! ZHA debug Smoke.txt
Yes, enable debug logging in ZHA, then if you can cut it to just the timestamps when are testing we should see the traffic and be able to tell where I went wrong.