diff --git a/lib/platformAccessory.js b/lib/platformAccessory.js index e6cfdf5..d4a3fb4 100644 --- a/lib/platformAccessory.js +++ b/lib/platformAccessory.js @@ -60,6 +60,12 @@ PlatformAccessory.prototype.addService = function(service) { return service; } +/** + * searchs for a Service in the services collection and returns the first Service object that matches. + * If multiple services of the same type are present in one accessory, use getServiceByUUIDAndSubType instead. + * @param {ServiceConstructor|string} name + * @returns Service + */ PlatformAccessory.prototype.getService = function(name) { for (var index in this.services) { var service = this.services[index]; @@ -71,6 +77,25 @@ PlatformAccessory.prototype.getService = function(name) { } } +/** + * searchs for a Service in the services collection and returns the first Service object that matches. + * If multiple services of the same type are present in one accessory, use getServiceByUUIDAndSubType instead. + * @param {string} UUID Can be an UUID, a service.displayName, or a constructor of a Service + * @param {string} subtype A subtype string to match + * @returns Service + */ +PlatformAccessory.prototype.getServiceByUUIDAndSubType = function(UUID, subtype) { + for (var index in this.services) { + var service = this.services[index]; + + if (typeof UUID === 'string' && (service.displayName === UUID || service.name === UUID) && service.subtype === subtype ) + return service; + else if (typeof name === 'function' && ((service instanceof name) || (name.UUID === service.UUID)) && service.subtype === subtype) + return service; + } +} + + PlatformAccessory.prototype.updateReachability = function(reachable) { this.reachable = reachable; @@ -149,4 +174,4 @@ PlatformAccessory.prototype._configFromData = function(data) { } this.services = services; -} \ No newline at end of file +}