Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two private attributes for SR-ZG9040A/ZG9041A-D. #8210

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 139 additions & 2 deletions src/devices/sunricher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,150 @@ import {
} from '../lib/modernExtend';
import * as reporting from '../lib/reporting';
import * as globalStore from '../lib/store';
import {DefinitionWithExtend, Fz, Zh} from '../lib/types';
import {DefinitionWithExtend, Expose, Fz, ModernExtend, OnEvent, OnEventData, OnEventType, Tz, Zh} from '../lib/types';
import * as utils from '../lib/utils';

const NS = 'zhc:sunricher';
const e = exposes.presets;
const ea = exposes.access;

const sunricherManufacturerCode = 0x1224;

async function readAttributeIfOnline(type: OnEventType, data: OnEventData, device: Zh.Device, attribute: number) {
let isReading = false;
if (type === 'deviceInterview') {
const interviewData = data as {status: string};
if (interviewData.status === 'successful') {
isReading = true;
}
} else if (type === 'deviceAnnounce') {
isReading = true;
}
if (isReading) {
const endpoint = device.getEndpoint(1);
await endpoint.read('genBasic', [attribute], {manufacturerCode: sunricherManufacturerCode});
}
}

function sunricherExternalSwitchType(): ModernExtend {
const attribute = 0x8803;
const dataType = 0x20;
const valueMap: {[key: number]: string} = {
0: 'Push button',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make all snake_case, also valueLookup below

1: 'Normal on/off',
2: 'Three way',
};
const valueLookup: {[key: string]: number} = {
'Push button': 0,
'Normal on/off': 1,
'Three way': 2,
};

const fromZigbee: Fz.Converter[] = [
{
cluster: 'genBasic',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
if (Object.prototype.hasOwnProperty.call(msg.data, attribute)) {
const value = msg.data[attribute];
return {
externalSwitchType: valueMap[value] || 'unknown',
externalSwitchTypeNumeric: value,
};
}
return undefined;
},
} satisfies Fz.Converter,
];

const toZigbee: Tz.Converter[] = [
{
key: ['externalSwitchType'],
convertSet: async (entity, key, value: string, meta) => {
const numericValue = valueLookup[value] ?? parseInt(value, 10);
await entity.write('genBasic', {[attribute]: {value: numericValue, type: dataType}}, {manufacturerCode: sunricherManufacturerCode});
return {state: {externalSwitchType: value}};
},
convertGet: async (entity, key, meta) => {
await entity.read('genBasic', [attribute], {manufacturerCode: sunricherManufacturerCode});
},
} satisfies Tz.Converter,
];

const exposes: Expose[] = [e.enum('externalSwitchType', ea.ALL, ['Push button', 'Normal on/off', 'Three way']).withLabel('External Switch Type')];
Copy link
Owner

@Koenkk Koenkk Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const exposes: Expose[] = [e.enum('externalSwitchType', ea.ALL, ['Push button', 'Normal on/off', 'Three way']).withLabel('External Switch Type')];
const exposes: Expose[] = [e.enum('external_switch_type', ea.ALL, ['push_button', 'normal_on_off', 'three_way']).withLabel('External switch type')];

const onEvent: OnEvent = async (type, data, device) => {
await readAttributeIfOnline(type, data, device, attribute);
};
return {
fromZigbee,
toZigbee,
exposes,
onEvent,
isModernExtend: true,
};
}

function sunricherMinimumPWM(): ModernExtend {
const attribute = 0x7809;
const dataType = 0x20;

const fromZigbee: Fz.Converter[] = [
{
cluster: 'genBasic',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
if (Object.prototype.hasOwnProperty.call(msg.data, attribute)) {
console.log(`from `, msg.data[attribute]);
const value = Math.round(msg.data[attribute] / 5.1);
return {
minimumPWM: value,
};
}
return undefined;
},
},
];

const toZigbee: Tz.Converter[] = [
{
key: ['minimumPWM'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key: ['minimumPWM'],
key: ['minimum_pwm'],

convertSet: async (entity: Zh.Endpoint, key: string, value: number | string, meta) => {
console.log(`to `, value);
const numValue = typeof value === 'string' ? parseInt(value) : value;
const zgValue = Math.round(numValue * 5.1);
await entity.write('genBasic', {[attribute]: {value: zgValue, type: dataType}}, {manufacturerCode: sunricherManufacturerCode});
return {state: {minimumPWM: numValue}};
},
convertGet: async (entity: Zh.Endpoint, key: string, meta) => {
await entity.read('genBasic', [attribute], {manufacturerCode: sunricherManufacturerCode});
},
},
];

const exposes: Expose[] = [
e
.numeric('minimumPWM', ea.ALL)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.numeric('minimumPWM', ea.ALL)
.numeric('minimum_pwm', ea.ALL)

.withLabel('Minimum PWM')
.withDescription('Power off the device and wait for 3 seconds before reconnecting to apply the settings.')
.withValueMin(0)
.withValueMax(50)
.withUnit('%')
.withValueStep(1),
];

const onEvent: OnEvent = async (type, data, device) => {
await readAttributeIfOnline(type, data, device, attribute);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these only need to be read one time after the device has joined? If yes, put them in a configure.

};

return {
fromZigbee,
toZigbee,
exposes,
onEvent,
isModernExtend: true,
};
}

const fzLocal = {
sunricher_SRZGP2801K45C: {
cluster: 'greenPower',
Expand Down Expand Up @@ -378,7 +515,7 @@ const definitions: DefinitionWithExtend[] = [
model: 'SR-ZG9040A/ZG9041A-D',
vendor: 'Sunricher',
description: 'Zigbee micro smart dimmer',
extend: [light({configureReporting: true}), electricityMeter()],
extend: [light({configureReporting: true}), electricityMeter(), sunricherExternalSwitchType(), sunricherMinimumPWM()],
},
{
zigbeeModel: ['HK-ZD-DIM-A'],
Expand Down
Loading