Skip to content

Commit

Permalink
Add support for identifying linked services using ServiceLabelIndex i…
Browse files Browse the repository at this point in the history
…n addition to the Identifier characteristic

This will make it easier to support IrrigationSystem accessories.
  • Loading branch information
derek-miller committed Oct 29, 2024
1 parent 920aa04 commit 27ca30c
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ type C4HCSetRequestPayload = C4HCCommonPayload & {
characteristic: string;
value: CharacteristicValue;
identifier?: CharacteristicValue | null;
serviceLabelIndex?: CharacteristicValue | null;
};

type C4HCGetRequestPayload = C4HCCommonPayload & {
name: string;
service: string;
characteristic: string;
identifier?: CharacteristicValue;
serviceLabelIndex?: CharacteristicValue;
};

/**
Expand Down Expand Up @@ -471,6 +473,9 @@ export class C4HCHomebridgePlatform implements DynamicPlatformPlugin {
identifier:
service.characteristics.find((c) => c instanceof this.Characteristic.Identifier)?.value ??
undefined,
serviceLabelIndex:
service.characteristics.find((c) => c instanceof this.Characteristic.ServiceLabelIndex)
?.value ?? undefined,
value,
},
});
Expand Down Expand Up @@ -596,17 +601,19 @@ export class C4HCHomebridgePlatform implements DynamicPlatformPlugin {

const idCharacteristic =
(<C4HCCharacteristicDefinition>characteristicsDefinition.Identifier)?.value ??
<'default' | CharacteristicValue>characteristicsDefinition.Identifier;
(<C4HCCharacteristicDefinition>characteristicsDefinition.ServiceLabelIndex)?.value ??
<'default' | CharacteristicValue>characteristicsDefinition.Identifier ??
<'default' | CharacteristicValue>characteristicsDefinition.ServiceLabelIndex;
const identifier = typeof idCharacteristic !== 'number' ? null : idCharacteristic;
if (parentService && identifier === null) {
return {
error: 'linked services must contain an Identifier characteristic',
error: 'linked services must contain an Identifier or ServiceLabelIndex characteristic',
};
}
if (multipleServiceDefinitions && identifier === null) {
return {
error:
'when specifying multiple services, each must contain an Identifier characteristic',
'when specifying multiple services, each must contain an Identifier or ServiceLabelIndex characteristic',
};
}

Expand Down Expand Up @@ -642,7 +649,6 @@ export class C4HCHomebridgePlatform implements DynamicPlatformPlugin {
error: `unable to add service ${serviceName} to '${accessory.displayName}'`,
};
}
addedServices.push(service);
if (primary !== null) {
service.setPrimaryService(primary);
}
Expand Down Expand Up @@ -686,17 +692,25 @@ export class C4HCHomebridgePlatform implements DynamicPlatformPlugin {
service.removeCharacteristic(characteristic);
});

if (parentService) {
parentService.addLinkedService(service);
}

if (linkedServices !== null && !Array.isArray(linkedServices)) {
return {
error: `invalid type for service ${serviceName} linkedServices; expected an array`,
};
}
for (const linkedServicesDefinition of linkedServices ?? []) {
this.addServicesToAccessory(accessory, linkedServicesDefinition, service, addedServices);
const { error } = this.addServicesToAccessory(
accessory,
linkedServicesDefinition,
service,
addedServices,
);
if (error) {
return { error };
}
}
addedServices.push(service);
if (parentService) {
parentService.addLinkedService(service);
}
}
}
Expand Down Expand Up @@ -882,7 +896,12 @@ export class C4HCHomebridgePlatform implements DynamicPlatformPlugin {
};
}

const identifier = typeof payload.identifier === 'number' ? `${payload.identifier}` : 'default';
const identifier =
typeof payload.identifier === 'number'
? `${payload.identifier}`
: typeof payload.serviceLabelIndex === 'number'
? `${payload.serviceLabelIndex}`
: null;
const service = accessory.getServiceById(
serviceType,
`uuid=${accessory.UUID}|service=${payload.service}|id=${identifier ?? 'default'}`,
Expand Down

2 comments on commit 27ca30c

@fdovarela
Copy link

Choose a reason for hiding this comment

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

Hi! Just tried to update on homebridge and I am getting a code 217 error.

@derek-miller
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Open an issue with relevant information and I can take a look. I have this running fine multiple environments so it must be specific to your installation.

Please sign in to comment.