import { AccessoryPlugin, Categories, Service } from 'homebridge' import { EventAdaptorConfig, Shared, YEELIGHT_COMMAND, YEELIGHT_PROPERTY, YEELIGHT_STATE, YEELIGHT_VALUE, YeelightDevice, YeelightIdentify, } from '../../homebridge-common' import { HomeBridgeCodes } from './device.constant' import { AccessoryInformation } from 'hap-nodejs/dist/lib/definitions/ServiceDefinitions' type Props = { identify: YeelightIdentify } export class Device implements AccessoryPlugin { // Requirement private readonly name: string private readonly address: string // Services private readonly informationService: AccessoryInformation private readonly mainLightService: Service private readonly ambientLightService: Service // Device private device: YeelightDevice constructor (props: Props) { // Requirement this.name = props.identify.name this.address = props.identify.address // Information this.informationService = new Shared.hap.Service.AccessoryInformation() .setCharacteristic(Shared.hap.Characteristic.Category, Categories.LIGHTBULB) .setCharacteristic(Shared.hap.Characteristic.Manufacturer, 'Yeelight') .setCharacteristic(Shared.hap.Characteristic.Model, 'Ceiling10') // Services this.mainLightService = new Shared.hap.Service.Lightbulb(`${props.identify.name}.MainLight`, 'MainLight') this.ambientLightService = new Shared.hap.Service.Lightbulb(`${props.identify.name}.AmbientLight`, 'AmbientLight') // Device this.device = new YeelightDevice({ identify: props.identify, service: this.mainLightService }) // Setup this.MainLightSetup() this.AmbientLightSetup() } MainLightSetup = () => { this.device.addEventAdaptor({ type: Shared.hap.Characteristic.On, service: this.mainLightService, get: { defaultValue: HomeBridgeCodes.Switch.Off, formatter: (valueMapping) => { return valueMapping?.[YEELIGHT_PROPERTY.power] === YEELIGHT_STATE[YEELIGHT_PROPERTY.power].POWER_ON ? HomeBridgeCodes.Switch.On : HomeBridgeCodes.Switch.Off } }, set: { property: YEELIGHT_COMMAND.SetPower, formatter: (value) => { return value ? YEELIGHT_VALUE[YEELIGHT_PROPERTY.power].POWER_ON : YEELIGHT_VALUE[YEELIGHT_PROPERTY.power].POWER_OFF } }, related: [this.ambientLightService] } as EventAdaptorConfig) } AmbientLightSetup = () => { this.device.addEventAdaptor({ type: Shared.hap.Characteristic.On, service: this.ambientLightService, get: { defaultValue: HomeBridgeCodes.Switch.Off, formatter: (valueMapping) => { return valueMapping?.[YEELIGHT_PROPERTY.bg_power] === YEELIGHT_STATE[YEELIGHT_PROPERTY.power].POWER_ON ? HomeBridgeCodes.Switch.On : HomeBridgeCodes.Switch.Off }, }, set: { property: YEELIGHT_COMMAND.SetBgPower, formatter: (value) => { return value ? YEELIGHT_VALUE[YEELIGHT_PROPERTY.power].POWER_ON : YEELIGHT_VALUE[YEELIGHT_PROPERTY.power].POWER_OFF } } } as EventAdaptorConfig) } /* * This method is optional to implement. It is called when HomeKit ask to identify the accessory. * Typical this only ever happens at the pairing process. */ identify (): void { Shared.log.info(`Identifying Yeelight device: ${this.name}-${this.address}`) } /* * This method is called directly after creation of this instance. * It should return all services which should be added to the accessory. */ getServices (): Service[] { return [ this.informationService, this.mainLightService, this.ambientLightService, ] } }