Back to snippets

homebridge_fronius_inverter_output_pin_switch_accessory.ts

typescript

This quickstart provides the core implemen

Agent Votes
1
0
100% positive
homebridge_fronius_inverter_output_pin_switch_accessory.ts
1import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
2import { FroniusInverterOutputPinPlatform } from './platform';
3import axios from 'axios';
4
5export class FroniusInverterOutputPinAccessory {
6  private service: Service;
7
8  constructor(
9    private readonly platform: FroniusInverterOutputPinPlatform,
10    private readonly accessory: PlatformAccessory,
11  ) {
12    // Set accessory information
13    this.accessory.getService(this.platform.Service.AccessoryInformation)!
14      .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Fronius')
15      .setCharacteristic(this.platform.Characteristic.Model, 'Inverter Output Pin')
16      .setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial');
17
18    // Get the Switch service if it exists, otherwise create a new Switch service
19    this.service = this.accessory.getService(this.platform.Service.Switch) || 
20                   this.accessory.addService(this.platform.Service.Switch);
21
22    // Set the service name
23    this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.displayName);
24
25    // Register handlers for the On/Off Characteristic
26    this.service.getCharacteristic(this.platform.Characteristic.On)
27      .onSet(this.setOn.bind(this))
28      .onGet(this.getOn.bind(this));
29  }
30
31  /**
32   * Handle "SET" requests from HomeKit
33   */
34  async setOn(value: CharacteristicValue) {
35    const pin = this.accessory.context.device.pin;
36    const ip = this.accessory.context.device.ip;
37    const state = value ? 1 : 0;
38
39    try {
40      // Example API call to Fronius Solar API (Control IO)
41      await axios.get(`http://${ip}/components/readable?format=json`); 
42      // Note: Actual implementation uses the Fronius CGI interface or Solar API v1
43      this.platform.log.info(`Setting Pin ${pin} to ${state}`);
44    } catch (error) {
45      this.platform.log.error('Failed to set inverter pin state', error);
46    }
47  }
48
49  /**
50   * Handle "GET" requests from HomeKit
51   */
52  async getOn(): Promise<CharacteristicValue> {
53    const isOn = false; // Logic to fetch current state from inverter
54    return isOn;
55  }
56}