@@ -3,12 +3,14 @@ import { HomebridgeAccessory } from 'fritz-light';
33import type { FritzBox } from './fritzbox.js' ;
44import { XMLClient } from './XMLClient.js' ;
55import { Template } from './accessories/template.js' ;
6+ import { Switch } from './accessories/switch.js' ;
67import { Thermostat } from './accessories/thermostat.js' ;
78import { PlatformAccessory , PlatformConfig } from 'homebridge' ;
89import { FritzLight } from '../platform.js' ;
910
1011// Supported accessory types
1112enum AccessoryType {
13+ Switch = 'switch' ,
1214 Thermostat = 'thermostat' , // "hkr" = "Heizkörperregler"
1315}
1416
@@ -44,7 +46,7 @@ export interface FritzAccessory {
4446}
4547
4648export class SmartHome {
47- private devices : Map < string , FritzAccessory & ( Template | Thermostat ) > = new Map ( ) ;
49+ private devices : Map < string , FritzAccessory & ( Switch | Template | Thermostat ) > = new Map ( ) ;
4850
4951 /**
5052 * @link https://fritz.support/resources/AHA-HTTP-Interface.pdf
@@ -54,7 +56,7 @@ export class SmartHome {
5456 /**
5557 * Get list of currently registered devices, update existing instances
5658 */
57- public async getDevices ( config : PlatformConfig ) : Promise < ( FritzAccessory & ( Template | Thermostat ) ) [ ] > {
59+ public async getDevices ( config : PlatformConfig ) : Promise < ( FritzAccessory & ( Switch | Template | Thermostat ) ) [ ] > {
5860 await this . fritzbox . init ( ) ;
5961 const sid = await this . fritzbox . getSid ( ) ;
6062
@@ -92,10 +94,16 @@ export class SmartHome {
9294 ] ;
9395 }
9496
95- private getDeviceFromApi ( device : AHADevice ) : FritzAccessory & Thermostat {
97+ private getDeviceFromApi ( device : AHADevice ) : FritzAccessory & ( Switch | Thermostat ) {
9698 // Nothing else implemented yet
97- //switch (this.getDeviceType(device)) { ... }
98- return this . getThermostat ( device ) ;
99+ switch ( this . getDeviceType ( device ) ) {
100+ case AccessoryType . Switch :
101+ return this . getSwitch ( device ) ;
102+ case AccessoryType . Thermostat :
103+ return this . getThermostat ( device ) ;
104+ }
105+
106+ throw new Error ( `Unknown device type: ${ this . getDeviceType ( device ) } ` ) ;
99107 }
100108
101109 private getTemplate ( apiTemplate : AHATemplate ) : Template {
@@ -112,6 +120,33 @@ export class SmartHome {
112120 }
113121 return template ;
114122 }
123+
124+ private getSwitch ( device : AHADevice ) : Switch {
125+ const state = {
126+ name : device . name ,
127+ firmwareVersion : device [ '@_fwversion' ] ,
128+ on : < boolean > ( device . switch ?. state || device . simpleonoff ?. state || false ) ,
129+ currentTemperature : device . temperature ? device . temperature . celsius / 10 : undefined ,
130+ } ;
131+
132+ let switchDevice : Switch ;
133+ if ( this . devices . has ( device [ '@_identifier' ] ) ) {
134+ // Update device state
135+ switchDevice = < Switch > this . devices . get ( device [ '@_identifier' ] ) ;
136+ switchDevice . state = state ;
137+
138+ } else {
139+ switchDevice = new Switch (
140+ this . fritzbox ,
141+ device [ '@_identifier' ] ,
142+ device [ '@_manufacturer' ] ,
143+ device [ '@_productname' ] ,
144+ state ,
145+ ) ;
146+ this . devices . set ( switchDevice . ain , switchDevice ) ;
147+ }
148+ return switchDevice ;
149+ }
115150
116151 private getThermostat ( device : AHADevice ) : Thermostat {
117152 if ( ! device . hkr ) {
@@ -152,6 +187,13 @@ export class SmartHome {
152187 return AccessoryType . Thermostat ;
153188 }
154189
190+ if (
191+ ( ( device [ '@_functionbitmask' ] & AHAFunctionBitmask . Outlet ) > 0 && device . switch ) ||
192+ ( ( device [ '@_functionbitmask' ] & AHAFunctionBitmask . OnOff ) > 0 && device . simpleonoff )
193+ ) {
194+ return AccessoryType . Switch ;
195+ }
196+
155197 // Not implemented
156198 return undefined ;
157199 }
0 commit comments