UNPKG

homebridge-plugin-utils

Version:

Opinionated utilities to provide common capabilities and create rich configuration webUI experiences for Homebridge plugins.

106 lines (105 loc) 5.43 kB
/** * Homebridge service helper utilities. * * @module */ import type { PlatformAccessory, Service, WithUUID } from "homebridge"; import { type Nullable } from "./util.js"; /** * Utility method that either creates a new service on an accessory if needed, or returns an existing one. Optionally, it executes a callback to initialize a new * service instance. Additionally, the various name characteristics of the service are set to the specified name, and optionally added if necessary. * * @param accessory - The Homebridge accessory to check or modify. * @param serviceType - The type of service to instantiate or retrieve. * @param name - Name to be displayed to the end user for this service. * @param subtype - Optional service subtype to uniquely identify the service. * @param onServiceCreate - Optional callback invoked only when a new service is created, receiving the new service as its argument. * * @returns Returns the created or retrieved service, or `null` if service creation failed. * * @remarks * This method ensures that the service's display name and available name characteristics are updated to the specified name. If `onServiceCreate` is provided, * it will only be called for newly created services, not for existing ones. * * The `ConfiguredName` and `Name` characteristics are conditionally added or updated based on the type of service, in accordance with HomeKit requirements. * * @example * ```typescript * // Example: Ensure a Lightbulb service exists with a user-friendly name, and initialize it if newly created. * const lightbulbService = acquireService(accessory, hap.Service.Lightbulb, "Living Room Lamp", undefined, (svc: Service): void => { * * // Called only if the service is newly created. * svc.setCharacteristic(hap.Characteristic.On, false); * }); * * if(lightbulbService) { * * // Service is now available, with display name set and optional characteristics managed. * lightbulbService.updateCharacteristic(hap.Characteristic.Brightness, 75); * } * ``` * * @see setServiceName — updates the newly created (or existing) service’s name-related characteristics. * @see validService — validate or prune services after acquisition. * @category Accessory */ export declare function acquireService(accessory: PlatformAccessory, serviceType: WithUUID<typeof Service>, name: string, subtype?: string, onServiceCreate?: (svc: Service) => void): Nullable<Service>; /** * Validates whether a specific service should exist on the given accessory, removing the service if it fails validation. * * @param accessory - The Homebridge accessory to inspect and potentially modify. * @param serviceType - The type of Homebridge service being checked or instantiated. * @param validate - A boolean or a function that determines if the service should exist. If a function is provided, it receives a boolean indicating whether the * service currently exists, and should return `true` to keep the service, or `false` to remove it. * @param subtype - Optional service subtype to uniquely identify the service. * * @returns `true` if the service is valid (and kept), or `false` if it was removed. * * @remarks * The `validate` parameter can be either: * - a boolean (where `true` means keep the service, `false` means remove it). * - a function (which is called with `hasService: boolean` and returns whether to keep the service). * * If the service should not exist according to `validate`, and it is currently present, this function will remove it from the accessory. * * @example * ```typescript * // Remove a service if it exists * validService(accessory, Service.Switch, false); * * // Only keep a service if a configuration flag is true * validService(accessory, Service.Switch, config.enableSwitch); * * // Keep a service if it currently exists, or add it if a certain condition is met * validService(accessory, Service.Switch, (hasService) => hasService || config.enableSwitch); * ``` * * @see acquireService — to add or retrieve services. * @category Accessory */ export declare function validService(accessory: PlatformAccessory, serviceType: WithUUID<typeof Service>, validate: boolean | ((hasService: boolean) => boolean), subtype?: string): boolean; /** * Retrieves the primary name of a service, preferring the ConfiguredName characteristic over the Name characteristic. * * @param service - The service from which to retrieve the name. * @returns The configured or display name of the service, or `undefined` if neither is set. * * @see setServiceName — to update the current name n a service. * @category Accessory */ export declare function getServiceName(service?: Service): string | undefined; /** * Updates the displayName and applicable name characteristics of a service to the specified value. * * @param service - The service to update. * @param name - The new name to apply to the service. * * @remarks * This function ensures the name is validated, updates the service's `displayName`, and sets the `ConfiguredName` and `Name` * characteristics when supported by the service type. * * @see acquireService — to add or retrieve services. * @see getServiceName — to retrieve the current name set on a service. * @category Accessory */ export declare function setServiceName(service: Service, name: string): void;