-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: Generate definition for unknown devices #6692
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
Merged
Koenkk
merged 12 commits into
Koenkk:master
from
ffenix113:feature/generate-definitions
Dec 22, 2023
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
432c8ff
feat: Generate definition for unknown devices
ffenix113 3aa1974
add test
ffenix113 c05d9e6
fix definition processing
ffenix113 15226ff
Update definition generation to be dynamic
ffenix113 411aea2
Update generator
ffenix113 53c551b
Add tests
ffenix113 bfd36be
update extenders
ffenix113 1683eb9
update generation tests
ffenix113 3ce49c7
fix lint
ffenix113 eea8c3e
Update generateDefinition.ts
Koenkk 93ec604
Update index.ts
Koenkk b68f1ad
small refactor
Koenkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import {Cluster} from 'zigbee-herdsman/dist/zcl/tstype'; | ||
| import {Definition, ModernExtend, Zh} from './types'; | ||
| import * as e from './modernExtend'; | ||
| import {Endpoint} from 'zigbee-herdsman/dist/controller/model'; | ||
|
|
||
| export function generateDefinition(device: Zh.Device): Definition { | ||
Koenkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const deviceExtenders: ModernExtend[] = []; | ||
|
|
||
| device.endpoints.forEach((endpoint) => { | ||
| const addExtenders = (cluster: Cluster, knownExtenders: extendersObject) => { | ||
| const clusterName = cluster.name || cluster.ID.toString(); | ||
| if (!knownExtenders.hasOwnProperty(clusterName)) { | ||
| return; | ||
| } | ||
|
|
||
| const extenderProviders = knownExtenders[clusterName]; | ||
| const extenders = extenderProviders.map((extender: extenderProvider): ModernExtend => { | ||
| if (typeof extender !== 'function') { | ||
| return extender; | ||
| } | ||
| return extender(endpoint, cluster); | ||
| }); | ||
|
|
||
| deviceExtenders.push(...(extenders)); | ||
| }; | ||
|
|
||
| endpoint.getInputClusters().forEach((cluster) => { | ||
| addExtenders(cluster, inputExtenders); | ||
| }); | ||
| endpoint.getOutputClusters().forEach((cluster) => { | ||
| addExtenders(cluster, outputExtenders); | ||
| }); | ||
| }); | ||
|
|
||
| const definition: Partial<Definition> = { | ||
| model: device.modelID || '', | ||
| vendor: device.manufacturerName || '', | ||
| description: 'Generated from device information', | ||
| extend: deviceExtenders, | ||
| generated: true, | ||
| }; | ||
|
|
||
| return definition as Definition; | ||
| } | ||
|
|
||
| // This configurator type provides some flexibility in terms of how ModernExtend configuration can be obtained. | ||
| // I.e. if cluster has optional attributes - this type can be used | ||
| // to define function that will generate more feature-full extension. | ||
| type extenderConfigurator = (endpoint: Endpoint, cluster: Cluster) => ModernExtend | ||
| // extenderProvider defines a type that will produce a `ModernExtend` | ||
| // either directly, or by calling a function. | ||
| type extenderProvider = ModernExtend | extenderConfigurator | ||
| type extendersObject = {[name: string]: extenderProvider[]} | ||
|
|
||
| const inputExtenders: extendersObject = { | ||
| 'msTemperatureMeasurement': [e.temperature()], | ||
| 'msPressureMeasurement': [e.pressure()], | ||
| 'msRelativeHumidity': [e.humidity()], | ||
| 'genOnOff': [e.onOff({powerOnBehavior: false})], | ||
| }; | ||
|
|
||
| const outputExtenders: extendersObject = { | ||
| 'genIdentify': [e.identify()], | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { Definition } from '../src/lib/types'; | ||
| import fz from '../src/converters/fromZigbee' | ||
|
|
||
| import { repInterval } from '../src/lib/constants'; | ||
| import {assertDefintion, assertDefinitionArgs, mockDevice, reportingItem} from './modernExtend.test'; | ||
| import { findByDevice} from '../src'; | ||
| import Device from 'zigbee-herdsman/dist/controller/model/device'; | ||
|
|
||
| const assertGeneratedDefinition = async (args: assertDefinitionArgs) => { | ||
| const getDefinition = (device: Device): Definition => { | ||
| return findByDevice(device, true); | ||
| } | ||
|
|
||
| const definition = getDefinition(args.device) | ||
|
|
||
| expect(definition.model).toEqual(args.device.modelID) | ||
|
|
||
| return await assertDefintion({findByDeviceFn: getDefinition, ...args}) | ||
| } | ||
|
|
||
| describe('GenerateDefinition', () => { | ||
| test('empty', async () => { | ||
| await assertGeneratedDefinition({ | ||
| device: mockDevice({modelID: 'empty', endpoints: [{inputClusters: [], outputClusters:[]}]}), | ||
| meta: undefined, | ||
| fromZigbee: [], | ||
| toZigbee: [], | ||
| exposes: ['linkquality'], | ||
| bind: [], | ||
| read: [], | ||
| configureReporting: [], | ||
| }); | ||
| }); | ||
|
|
||
| test('input(msTemperatureMeasurement),output(genIdentify)', async () => { | ||
| await assertGeneratedDefinition({ | ||
| device: mockDevice({modelID: 'temp', endpoints: [{inputClusters: ['msTemperatureMeasurement'], outputClusters:['genIdentify']}]}), | ||
| meta: undefined, | ||
| fromZigbee: [fz.temperature], | ||
| toZigbee: ['identify'], | ||
| exposes: ['linkquality', 'temperature'], | ||
| bind: {1: ['msTemperatureMeasurement']}, | ||
| read: {1: [['msTemperatureMeasurement', ['measuredValue']]]}, | ||
| configureReporting: { | ||
| 1: [ | ||
| ['msTemperatureMeasurement', [reportingItem('measuredValue', 0, repInterval.MAX, 1)]], | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('input(msPressureMeasurement)', async () => { | ||
| await assertGeneratedDefinition({ | ||
| device: mockDevice({modelID: 'pressure', endpoints: [{inputClusters: ['msPressureMeasurement'], outputClusters:[]}]}), | ||
| meta: undefined, | ||
| fromZigbee: [fz.pressure], | ||
| toZigbee: [], | ||
| exposes: ['linkquality', 'pressure'], | ||
| bind: {1: ['msPressureMeasurement']}, | ||
| read: {1: [['msPressureMeasurement', ['measuredValue']]]}, | ||
| configureReporting: { | ||
| 1: [ | ||
| ['msPressureMeasurement', [reportingItem('measuredValue', 0, repInterval.MAX, 1)]], | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('input(msRelativeHumidity)', async () => { | ||
| await assertGeneratedDefinition({ | ||
| device: mockDevice({modelID: 'humidity', endpoints: [{inputClusters: ['msRelativeHumidity'], outputClusters:[]}]}), | ||
| meta: undefined, | ||
| fromZigbee: [fz.humidity], | ||
| toZigbee: [], | ||
| exposes: ['humidity', 'linkquality'], | ||
| bind: {1: ['msRelativeHumidity']}, | ||
| read: {1: [['msRelativeHumidity', ['measuredValue']]]}, | ||
| configureReporting: { | ||
| 1: [ | ||
| ['msRelativeHumidity', [reportingItem('measuredValue', 0, repInterval.MAX, 1)]], | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('input(msTemperatureMeasurement, genOnOff)', async () => { | ||
| await assertGeneratedDefinition({ | ||
| device: mockDevice({modelID: 'combo', endpoints: [{inputClusters: ['msTemperatureMeasurement', 'genOnOff'], outputClusters:[]}]}), | ||
| meta: undefined, | ||
| fromZigbee: [fz.temperature, fz.on_off], | ||
| toZigbee: ['state', 'on_time', 'off_wait_time'], | ||
| exposes: ['linkquality', 'switch(state)', 'temperature'], | ||
| bind: {1: ['msTemperatureMeasurement', 'genOnOff']}, | ||
| read: {1: [ | ||
| ['msTemperatureMeasurement', ['measuredValue']], | ||
| ['genOnOff', ['onOff']], | ||
| ]}, | ||
| configureReporting: { | ||
| 1: [ | ||
| ['msTemperatureMeasurement', [reportingItem('measuredValue', 0, repInterval.MAX, 1)]], | ||
| ['genOnOff', [reportingItem('onOff', 0, repInterval.MAX, 1)]], | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.