UNPKG

homebridge-freeathome-local-api

Version:

Control your free@home setup using the local API provided by your System Access Point

178 lines (168 loc) 5.73 kB
import { Channel, Device } from "freeathome-local-api-client"; import { Characteristic, CharacteristicValue, PlatformAccessory, Service, WithUUID, } from "homebridge"; import { ContactSensorAccessory } from "../src/contactSensorAccessory.js"; import { FreeAtHomeContext } from "../src/freeAtHomeContext.js"; import { createPlatformAccessory, MockFreeAtHomeHomebridgePlatform, } from "./platform.mock.js"; describe("Contact Sensor Accessory", () => { let channel: Channel; let device: Device; let platform: MockFreeAtHomeHomebridgePlatform; let platformAccessory: PlatformAccessory<FreeAtHomeContext>; beforeEach(() => { channel = {}; device = {}; platform = new MockFreeAtHomeHomebridgePlatform(); platformAccessory = createPlatformAccessory("Contact Sensor Accessory"); platformAccessory.context = { channel: channel, channelId: "ch1234", device: device, deviceSerial: "ABB7xxxxxxxx", }; }); afterEach(() => { platform.resetLoggerCalls(); jasmine.clock().uninstall(); }); it("should throw if expected data points are missing on channel", () => { channel.outputs = undefined; expect( () => new ContactSensorAccessory(platform, platformAccessory) ).toThrowError("Channel lacks expected input or output data points."); }); it("should be created", async () => { channel.outputs = { odp0000: { pairingID: 53, }, }; const accessory = new ContactSensorAccessory(platform, platformAccessory); expect(accessory).toBeTruthy(); const instance = accessory as unknown as { service: Service; }; const characteristic = instance.service.getCharacteristic( accessory.platform.Characteristic.ContactSensorState ); expect(await characteristic.handleGetRequest()).toBe( platform.Characteristic.ContactSensorState.CONTACT_DETECTED ); }); it("should be created with non-default state", async () => { channel.outputs = { odp0000: { pairingID: 53, value: "1", }, }; const accessory = new ContactSensorAccessory(platform, platformAccessory); expect(accessory).toBeTruthy(); const instance = accessory as unknown as { service: Service; }; const characteristic = instance.service.getCharacteristic( accessory.platform.Characteristic.ContactSensorState ); expect(await characteristic.handleGetRequest()).toBe( platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED ); }); it("should ignore update if datapoint is unknown", async () => { channel.outputs = { odp0000: { pairingID: 53, }, }; const accessory = new ContactSensorAccessory(platform, platformAccessory); const instance = accessory as unknown as { service: Service; doUpdateDatapoint: ( acccessoryDisplayType: string, service: Service, characteristic: WithUUID<new () => Characteristic>, characteristicValue: CharacteristicValue ) => void; }; const spy = spyOn(instance, "doUpdateDatapoint"); const characteristic = instance.service.getCharacteristic( accessory.platform.Characteristic.ContactSensorState ); expect(await characteristic.handleGetRequest()).toBe( platform.Characteristic.ContactSensorState.CONTACT_DETECTED ); accessory.updateDatapoint("test", "0"); expect(spy).not.toHaveBeenCalled(); }); it("should process update to when contact is not detected", async () => { channel.outputs = { odp0123: { pairingID: 53, }, }; const accessory = new ContactSensorAccessory(platform, platformAccessory); const instance = accessory as unknown as { service: Service; doUpdateDatapoint: ( acccessoryDisplayType: string, service: Service, characteristic: WithUUID<new () => Characteristic>, characteristicValue: CharacteristicValue ) => void; }; const spy = spyOn(instance, "doUpdateDatapoint"); const characteristic = instance.service.getCharacteristic( accessory.platform.Characteristic.ContactSensorState ); expect(await characteristic.handleGetRequest()).toBe( platform.Characteristic.ContactSensorState.CONTACT_DETECTED ); accessory.updateDatapoint("odp0123", "1"); expect(spy).toHaveBeenCalledWith( "Contact Sensor", instance.service, accessory.platform.Characteristic.ContactSensorState, platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED ); }); it("should process update to when contact is detected", async () => { channel.outputs = { odp0123: { pairingID: 53, value: "1", }, }; const accessory = new ContactSensorAccessory(platform, platformAccessory); const instance = accessory as unknown as { service: Service; doUpdateDatapoint: ( acccessoryDisplayType: string, service: Service, characteristic: WithUUID<new () => Characteristic>, characteristicValue: CharacteristicValue ) => void; }; const spy = spyOn(instance, "doUpdateDatapoint"); const characteristic = instance.service.getCharacteristic( accessory.platform.Characteristic.ContactSensorState ); expect(await characteristic.handleGetRequest()).toBe( platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED ); accessory.updateDatapoint("odp0123", "0"); expect(spy).toHaveBeenCalledWith( "Contact Sensor", instance.service, accessory.platform.Characteristic.ContactSensorState, platform.Characteristic.ContactSensorState.CONTACT_DETECTED ); }); });