Skip to content

Commit

Permalink
Merge pull request #29 from Lngramos/latest
Browse files Browse the repository at this point in the history
Add support for devices with multiple connected nodes
  • Loading branch information
duggan authored Apr 2, 2024
2 parents 36a89df + 850b7bd commit a63aa9c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"files.eol": "\n",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.rulers": [ 140 ],
"eslint.enable": true
}
}
1 change: 1 addition & 0 deletions src/helki_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface Node {
addr: string;
installed?: boolean;
lost?: boolean;
uid?: string;
}

interface GroupedDevices {
Expand Down
87 changes: 53 additions & 34 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,62 @@ export class Technotherm implements DynamicPlatformPlugin {
const home = groups.find(home => home.name === this.config.home);

for (const group of groups) {
// Loop over the devices and register each one
// Loop over the devices in the group
// Sometimes a device contains a single radiator, sometimes multiple
// Each radiator is a node in the Helki API
for (const device of group.devs) {
const uuid = this.api.hap.uuid.generate(device.dev_id); // Use dev_id to generate a UUID
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);

if (existingAccessory) {
// If the existing accessory is not in the configured home, remove it
if (home && existingAccessory?.context.home !== home.name) {
this.log.warn(`Removing accessory that does not match configured home "${home.name}: ${existingAccessory.displayName}"`);
try {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
} catch (error: unknown) {
this.log.warn(`existing accessory: ${error}`);
const nodes = await helki.getNodes(device.dev_id);

if (nodes.length === 0) {
this.log.warn(`No nodes found for device: ${device.name}`);
continue;
}

// Devices can have multiple nodes, register an accessory for each
for (const node of nodes) {
// Use Node attributes for the accessory display name and UUID gen
// but fallback to Device attributes in case they're undefined (i.e. Devices with a single node)
const accessoryName = node.name || device.name;
const accessoryUUID = this.api.hap.uuid.generate(node.uid || device.dev_id);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === accessoryUUID);

if (existingAccessory) {
// If the existing accessory is not in the configured home, remove it
if (home && existingAccessory?.context.home !== home.name) {
this.log.warn(`Removing accessory that does not match configured home "${home.name}: ${existingAccessory.displayName}"`);
try {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
} catch (error: unknown) {
this.log.warn(`existing accessory: ${error}`);
}
} else {
// If the node has been renamed, update the corresponding accessory
if (existingAccessory.displayName !== accessoryName) {
this.log.info(`Renaming accessory from ${existingAccessory.displayName} to ${accessoryName}`);
existingAccessory.displayName = accessoryName;
this.api.updatePlatformAccessories([existingAccessory]);
}

// Accessory exists, restore from cache
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new Radiator(this, existingAccessory, helki);
}
} else {
// Accessory exists, restore from cache
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new Radiator(this, existingAccessory, helki);
}
} else {
// Accessory doesn't exist, add new
const accessoryName = `${device.name}`;
const accessory = new this.api.platformAccessory(accessoryName, uuid);
const nodes = await helki.getNodes(device.dev_id);
const node = nodes[0];
accessory.context.device = device;
accessory.context.node = node;
accessory.context.home = group.name;
if (home !== undefined && home.name === group.name) {
this.log.info('Adding new accessory:', accessoryName);
new Radiator(this, accessory, helki);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
if (home === undefined) {
this.log.info('Adding new accessory:', accessoryName);
new Radiator(this, accessory, helki);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
// Accessory doesn't exist, add new
const accessory = new this.api.platformAccessory(accessoryName, accessoryUUID);
accessory.context.device = device;
accessory.context.node = node;
accessory.context.home = group.name;
if (home !== undefined && home.name === group.name) {
this.log.info('Adding new accessory:', accessoryName);
new Radiator(this, accessory, helki);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
if (home === undefined) {
this.log.info('Adding new accessory:', accessoryName);
new Radiator(this, accessory, helki);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}
Expand Down

0 comments on commit a63aa9c

Please sign in to comment.