Skip to content

Commit

Permalink
fix: Convert Sonoff SNZB-02D & SNZB-02P to modernExtend (#7572)
Browse files Browse the repository at this point in the history
* Convert SNZB-02D & SNZB-02P to `modernExtend`

* Fix imports & adjust reporting

* Fix import in sonoff.test.ts

* Add suggestion

* Update sonoff.ts

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
  • Loading branch information
burmistrzak and Koenkk authored May 30, 2024
1 parent fff4ffb commit 825a7fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
41 changes: 13 additions & 28 deletions src/devices/sonoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as reporting from '../lib/reporting';
import {
binary, enumLookup, forcePowerSource, numeric, onOff,
customTimeResponse, battery, ota, deviceAddCustomCluster,
temperature, humidity, bindCluster,
} from '../lib/modernExtend';
import {Definition, Fz, KeyValue, KeyValueAny, ModernExtend, Tz} from '../lib/types';
import * as utils from '../lib/utils';
Expand Down Expand Up @@ -622,19 +623,13 @@ const definitions: Definition[] = [
model: 'SNZB-02D',
vendor: 'SONOFF',
description: 'Temperature and humidity sensor with screen',
exposes: [e.battery(), e.temperature(), e.humidity()],
fromZigbee: [fz.temperature, fz.humidity, fz.battery],
toZigbee: [],
configure: async (device, coordinatorEndpoint) => {
const endpoint = device.getEndpoint(1);
const bindClusters = ['msTemperatureMeasurement', 'msRelativeHumidity', 'genPowerCfg'];
await reporting.bind(endpoint, coordinatorEndpoint, bindClusters);
await reporting.temperature(endpoint, {min: 30, max: constants.repInterval.MINUTES_5, change: 20});
await reporting.humidity(endpoint, {min: 30, max: constants.repInterval.MINUTES_5, change: 100});
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
device.powerSource = 'Battery';
device.save();
},
extend: [
forcePowerSource({powerSource: 'Battery'}),
battery({percentage: true}),
temperature(),
humidity(),
bindCluster({cluster: 'genPollCtrl', clusterType: 'input'}),
],
},
{
fingerprint: [
Expand Down Expand Up @@ -737,22 +732,12 @@ const definitions: Definition[] = [
model: 'SNZB-02P',
vendor: 'SONOFF',
description: 'Temperature and humidity sensor',
exposes: [e.battery(), e.temperature(), e.humidity(), e.battery_low(), e.battery_voltage()],
fromZigbee: [fz.temperature, fz.humidity, fz.battery],
configure: async (device, coordinatorEndpoint) => {
try {
const endpoint = device.getEndpoint(1);
const bindClusters = ['msTemperatureMeasurement', 'msRelativeHumidity', 'genPowerCfg'];
await reporting.bind(endpoint, coordinatorEndpoint, bindClusters);
await reporting.temperature(endpoint, {min: 30, max: constants.repInterval.MINUTES_5, change: 20});
await reporting.humidity(endpoint, {min: 30, max: constants.repInterval.MINUTES_5, change: 100});
await reporting.batteryPercentageRemaining(endpoint, {min: 3600, max: 7200});
} catch (e) {/* Not required for all: https://github.com/Koenkk/zigbee2mqtt/issues/5562 */
logger.error(`Configure failed: ${e}`, NS);
}
},
extend: [
ota(),
forcePowerSource({powerSource: 'Battery'}),
battery({percentage: true}),
temperature(),
humidity(),
bindCluster({cluster: 'genPollCtrl', clusterType: 'input'}),
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion test/sonoff.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as index from "../src/index";
import {Definition, Fz, KeyValueAny, Tz, Zh} from "../lib/types";
import {Definition, Fz, KeyValueAny, Tz, Zh} from "../src/lib/types";
import {Endpoint, Entity} from "zigbee-herdsman/dist/controller/model";

interface State {
Expand Down

0 comments on commit 825a7fa

Please sign in to comment.