Skip to content

Commit 7e8a807

Browse files
committed
fix: Fix power source uknown for various SONOFF devices Koenkk/zigbee2mqtt#24025
1 parent 511a622 commit 7e8a807

File tree

2 files changed

+31
-52
lines changed

2 files changed

+31
-52
lines changed

src/devices/sonoff.ts

+20-51
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
humidity,
1919
iasZoneAlarm,
2020
numeric,
21+
occupancy,
2122
onOff,
2223
ota,
2324
temperature,
@@ -27,7 +28,7 @@ import * as tuya from '../lib/tuya';
2728
import {DefinitionWithExtend, Fz, KeyValue, KeyValueAny, ModernExtend, Tz} from '../lib/types';
2829
import * as utils from '../lib/utils';
2930

30-
const {ewelinkAction} = ewelinkModernExtend;
31+
const {ewelinkAction, ewelinkBattery} = ewelinkModernExtend;
3132

3233
const NS = 'zhc:sonoff';
3334
const manufacturerOptions = {
@@ -639,45 +640,35 @@ const definitions: DefinitionWithExtend[] = [
639640
vendor: 'SONOFF',
640641
whiteLabel: [{vendor: 'eWeLink', model: 'RHK06'}, tuya.whitelabel('Tuya', 'WL-19DWZ', 'Contact sensor', ['_TZ3000_n2egfsli'])],
641642
description: 'Contact sensor',
642-
exposes: [e.contact(), e.battery_low(), e.battery(), e.battery_voltage()],
643-
fromZigbee: [fz.ias_contact_alarm_1, fz.battery],
644-
toZigbee: [],
645-
configure: async (device, coordinatorEndpoint) => {
646-
const endpoint = device.getEndpoint(1);
647-
await reporting.bind(endpoint, coordinatorEndpoint, ['genPowerCfg']);
648-
await reporting.batteryVoltage(endpoint, {min: 3600, max: 7200});
649-
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
650-
},
643+
extend: [ewelinkBattery(), iasZoneAlarm({zoneType: 'contact', zoneAttributes: ['alarm_1', 'battery_low']})],
651644
},
652645
{
653646
zigbeeModel: ['WB01', 'WB-01'],
654647
model: 'SNZB-01',
655648
vendor: 'SONOFF',
656649
whiteLabel: [{vendor: 'eWeLink', model: 'RHK07'}],
657650
description: 'Wireless button',
658-
exposes: [e.battery(), e.action(['single', 'double', 'long']), e.battery_voltage()],
659-
fromZigbee: [fz.ewelink_action, fz.battery],
651+
extend: [ewelinkBattery()],
652+
exposes: [e.action(['single', 'double', 'long'])],
653+
fromZigbee: [fz.ewelink_action],
660654
toZigbee: [],
661655
configure: async (device, coordinatorEndpoint) => {
662656
const endpoint = device.getEndpoint(1);
663-
await reporting.bind(endpoint, coordinatorEndpoint, ['genOnOff', 'genPowerCfg']);
664-
await reporting.batteryVoltage(endpoint, {min: 3600, max: 7200});
665-
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
657+
await reporting.bind(endpoint, coordinatorEndpoint, ['genOnOff']);
666658
},
667659
},
668660
{
669661
zigbeeModel: ['KF01', 'KF-01'],
670662
model: 'SNZB-01-KF',
671663
vendor: 'SONOFF',
672664
description: 'Wireless button',
673-
exposes: [e.battery(), e.action(['off', 'single']), e.battery_voltage()],
674-
fromZigbee: [fz.command_status_change_notification_action, fz.battery],
665+
extend: [ewelinkBattery()],
666+
exposes: [e.action(['off', 'single'])],
667+
fromZigbee: [fz.command_status_change_notification_action],
675668
toZigbee: [],
676669
configure: async (device, coordinatorEndpoint) => {
677670
const endpoint = device.getEndpoint(1);
678-
await reporting.bind(endpoint, coordinatorEndpoint, ['ssIasZone', 'genPowerCfg']);
679-
await reporting.batteryVoltage(endpoint, {min: 3600, max: 7200});
680-
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
671+
await reporting.bind(endpoint, coordinatorEndpoint, ['ssIasZone']);
681672
},
682673
},
683674
{
@@ -712,6 +703,8 @@ const definitions: DefinitionWithExtend[] = [
712703
fromZigbee: [fz.SNZB02_temperature, fz.humidity, fz.battery],
713704
toZigbee: [],
714705
configure: async (device, coordinatorEndpoint) => {
706+
device.powerSource = 'Battery';
707+
device.save();
715708
try {
716709
const endpoint = device.getEndpoint(1);
717710
const bindClusters = ['msTemperatureMeasurement', 'msRelativeHumidity', 'genPowerCfg'];
@@ -731,7 +724,7 @@ const definitions: DefinitionWithExtend[] = [
731724
model: 'SNZB-02D',
732725
vendor: 'SONOFF',
733726
description: 'Temperature and humidity sensor with screen',
734-
extend: [battery({percentage: true}), temperature(), humidity(), bindCluster({cluster: 'genPollCtrl', clusterType: 'input'})],
727+
extend: [battery(), temperature(), humidity(), bindCluster({cluster: 'genPollCtrl', clusterType: 'input'})],
735728
},
736729
{
737730
fingerprint: [
@@ -754,18 +747,7 @@ const definitions: DefinitionWithExtend[] = [
754747
vendor: 'SONOFF',
755748
whiteLabel: [{vendor: 'eWeLink', model: 'RHK09'}],
756749
description: 'Motion sensor',
757-
fromZigbee: [fz.ias_occupancy_alarm_1, fz.battery],
758-
toZigbee: [],
759-
configure: async (device, coordinatorEndpoint) => {
760-
const endpoint = device.getEndpoint(1);
761-
const bindClusters = ['genPowerCfg'];
762-
await reporting.bind(endpoint, coordinatorEndpoint, bindClusters);
763-
// 3600/7200 prevents disconnect
764-
// https://github.com/Koenkk/zigbee2mqtt/issues/13600#issuecomment-1283827935
765-
await reporting.batteryVoltage(endpoint, {min: 3600, max: 7200});
766-
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
767-
},
768-
exposes: [e.occupancy(), e.battery_low(), e.battery(), e.battery_voltage()],
750+
extend: [ewelinkBattery(), iasZoneAlarm({zoneType: 'occupancy', zoneAttributes: ['alarm_1', 'battery_low']})],
769751
},
770752
{
771753
zigbeeModel: ['S26R2ZB'],
@@ -839,9 +821,8 @@ const definitions: DefinitionWithExtend[] = [
839821
model: 'SNZB-04P',
840822
vendor: 'SONOFF',
841823
description: 'Contact sensor',
842-
exposes: [e.contact(), e.battery_low(), e.battery(), e.battery_voltage()],
843-
fromZigbee: [fz.ias_contact_alarm_1, fz.battery],
844824
extend: [
825+
iasZoneAlarm({zoneType: 'contact', zoneAttributes: ['alarm_1', 'battery_low']}),
845826
binary({
846827
name: 'tamper',
847828
cluster: 0xfc11,
@@ -853,22 +834,16 @@ const definitions: DefinitionWithExtend[] = [
853834
access: 'STATE_GET',
854835
}),
855836
ota(),
837+
ewelinkBattery(),
856838
],
857-
configure: async (device, coordinatorEndpoint) => {
858-
const endpoint = device.getEndpoint(1);
859-
await reporting.bind(endpoint, coordinatorEndpoint, ['genPowerCfg']);
860-
await reporting.batteryVoltage(endpoint, {min: 3600, max: 7200});
861-
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
862-
},
863839
},
864840
{
865841
zigbeeModel: ['SNZB-03P'],
866842
model: 'SNZB-03P',
867843
vendor: 'SONOFF',
868844
description: 'Zigbee PIR sensor',
869-
fromZigbee: [fz.occupancy, fz.battery],
870-
exposes: [e.occupancy(), e.battery()],
871845
extend: [
846+
occupancy(),
872847
numeric({
873848
name: 'motion_timeout',
874849
cluster: 0x0406,
@@ -887,13 +862,8 @@ const definitions: DefinitionWithExtend[] = [
887862
access: 'STATE',
888863
}),
889864
ota(),
865+
ewelinkBattery(),
890866
],
891-
configure: async (device, coordinatorEndpoint) => {
892-
const endpoint = device.getEndpoint(1);
893-
await reporting.bind(endpoint, coordinatorEndpoint, ['genPowerCfg']);
894-
await reporting.batteryVoltage(endpoint, {min: 3600, max: 7200});
895-
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
896-
},
897867
},
898868
{
899869
zigbeeModel: ['SNZB-05P'],
@@ -907,9 +877,8 @@ const definitions: DefinitionWithExtend[] = [
907877
model: 'SNZB-06P',
908878
vendor: 'SONOFF',
909879
description: 'Zigbee occupancy sensor',
910-
fromZigbee: [fz.occupancy],
911-
exposes: [e.occupancy()],
912880
extend: [
881+
occupancy(),
913882
numeric({
914883
name: 'occupancy_timeout',
915884
cluster: 0x0406,

src/lib/ewelink.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {presets} from './exposes';
2-
import {setupConfigureForBinding} from './modernExtend';
2+
import {battery, setupConfigureForBinding} from './modernExtend';
33
import {Configure, Expose, Fz, KeyValueAny, ModernExtend} from './types';
44

55
export const ewelinkModernExtend = {
@@ -21,6 +21,16 @@ export const ewelinkModernExtend = {
2121

2222
return {exposes, fromZigbee, configure, isModernExtend: true};
2323
},
24+
ewelinkBattery: (): ModernExtend => {
25+
// 3600/7200 prevents disconnect
26+
// https://github.com/Koenkk/zigbee2mqtt/issues/13600#issuecomment-1283827935
27+
return battery({
28+
voltage: true,
29+
voltageReporting: true,
30+
percentageReportingConfig: {min: 3600, max: 7200, change: 10},
31+
voltageReportingConfig: {min: 3600, max: 7200, change: 10},
32+
});
33+
},
2434
};
2535

2636
export {ewelinkModernExtend as modernExtend};

0 commit comments

Comments
 (0)