UNPKG

homebridge-rinnai-touch-platform

Version:

Homebridge Plugin to control heating/cooling via a Rinnai Touch WiFi Module

281 lines 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AccessoryService = void 0; const settings_1 = require("../settings"); const Thermostat_1 = require("./Thermostat"); const HeaterCooler_1 = require("./HeaterCooler"); const Fan_1 = require("./Fan"); const ZoneSwitch_1 = require("./ZoneSwitch"); const AdvanceSwitch_1 = require("./AdvanceSwitch"); const ManualSwitch_1 = require("./ManualSwitch"); const Pump_1 = require("./Pump"); class AccessoryService { constructor(platform) { this.platform = platform; this.accessories = new Map(); this.deviceModes = []; this.modeNames = { A: '', H: 'Heat', C: 'Cool', E: 'Cool', F: 'Fan', }; } discover(devices) { this.platform.log.debug(this.constructor.name, 'discover'); try { if (this.platform.settings.seperateModeAccessories) { if ('heat' in devices) { this.deviceModes.push('H'); } if ('cool' in devices) { this.deviceModes.push('C'); } if ('evap' in devices) { this.deviceModes.push('E'); } } else { this.deviceModes.push('A'); } if (this.platform.settings.showFan && devices.controllers.length > 1) { this.deviceModes.push('F'); } this.discoverThermostats(devices); this.discoverHeaterCoolers(devices); this.discoverFan(); this.discoverZoneSwitches(devices); this.discoverAdvanceSwitches(devices); this.discoverManualSwitches(devices); this.discoverPump(devices); } catch (error) { if (error instanceof Error) { this.platform.log.error(error.message); } } } discoverThermostats(devices) { this.platform.log.debug(this.constructor.name, 'discoverThermostats', devices); const zones = []; if (this.platform.settings.controllerType === 'T') { zones.push(...devices.controllers); } for (const zone of this.platform.service.AllZones) { if (zones.includes(zone)) { const displayName = this.platform.service.getHasMultiSetPoint() ? this.platform.service.getZoneName(zone) : this.platform.settings.name; this.addAccessory(Thermostat_1.Thermostat, Thermostat_1.Thermostat.name, displayName, zone); } else { this.removeAccessory(Thermostat_1.Thermostat.name, zone); } } } discoverHeaterCoolers(devices) { this.platform.log.debug(this.constructor.name, 'discoverHeaterCoolers', devices); const zones = []; if (this.platform.settings.controllerType === 'H') { zones.push(...devices.controllers); } if (this.platform.settings.zoneType === 'H' && 'heat' in devices) { for (const zone of devices.heat) { if (!zones.includes(zone)) { zones.push(zone); } } } for (const zone of this.platform.service.AllZones) { if (zones.includes(zone)) { const displayName = zone !== 'U' ? this.platform.service.getZoneName(zone) : this.platform.settings.name; this.addAccessory(HeaterCooler_1.HeaterCooler, HeaterCooler_1.HeaterCooler.name, displayName, zone); } else { this.removeAccessory(HeaterCooler_1.HeaterCooler.name, zone); } } } discoverFan() { this.platform.log.debug(this.constructor.name, 'discoverFan'); if (this.platform.settings.showFan) { this.addAccessory(Fan_1.Fan, Fan_1.Fan.name, 'Circulation Fan'); } else { this.removeAccessory(Fan_1.Fan.name); } } discoverZoneSwitches(devices) { this.platform.log.debug(this.constructor.name, 'discoverZoneSwitches', devices); const modeZones = { A: [], H: [], C: [], E: [], F: [], }; if (this.platform.settings.zoneType === 'S') { if (this.platform.settings.seperateModeAccessories) { modeZones['H'] = 'heat' in devices ? devices.heat : []; modeZones['C'] = 'cool' in devices ? devices.cool : []; modeZones['E'] = 'evap' in devices ? devices.evap : []; } else { modeZones['A'] = [...new Set([ ...('heat' in devices ? devices.heat : []), ...('cool' in devices ? devices.cool : []), ...('evap' in devices ? devices.evap : []), ])]; } } if (this.platform.settings.seperateFanZoneSwitches) { modeZones['F'] = [...new Set([ ...('heat' in devices ? devices.heat : []), ...('cool' in devices ? devices.cool : []), ])]; } // Remove the old zone switches that don't have mode for (const zone of ['A', 'B', 'C', 'D']) { this.removeAccessory(ZoneSwitch_1.ZoneSwitch.name, zone); } for (const mode of Object.keys(modeZones)) { for (const zone of ['A', 'B', 'C', 'D']) { if (modeZones[mode].includes(zone)) { const displayName = this.platform.service.getZoneName(zone) + ' ' + this.modeNames[mode]; this.addAccessory(ZoneSwitch_1.ZoneSwitch, ZoneSwitch_1.ZoneSwitch.name, displayName.trim(), zone, mode); } else { this.removeAccessory(ZoneSwitch_1.ZoneSwitch.name, zone, mode); } } } } discoverAdvanceSwitches(devices) { this.platform.log.debug(this.constructor.name, 'discoverAdvanceSwitches', devices); const zones = []; if (this.platform.settings.showAdvanceSwitches && 'heat' in devices) { zones.push(...devices.controllers); } // Remove the old zone switches that don't have mode for (const zone of this.platform.service.AllZones) { this.removeAccessory(AdvanceSwitch_1.AdvanceSwitch.name, zone); } for (const mode of ['A', 'H', 'C']) { for (const zone of this.platform.service.AllZones) { if (zones.includes(zone) && this.deviceModes.includes(mode)) { let displayName = 'Advance Period'; if (this.platform.service.getHasMultiSetPoint()) { displayName += ` ${this.platform.service.getZoneName(zone)}`; } displayName += ` ${this.modeNames[mode]}`; this.addAccessory(AdvanceSwitch_1.AdvanceSwitch, AdvanceSwitch_1.AdvanceSwitch.name, displayName.trim(), zone, mode); } else { this.removeAccessory(AdvanceSwitch_1.AdvanceSwitch.name, zone, mode); } } } } discoverManualSwitches(devices) { this.platform.log.debug(this.constructor.name, 'discoverManualSwitches', devices); const zones = []; if (this.platform.settings.showManualSwitches) { zones.push(...devices.controllers); } // Remove the old zone switches that don't have mode for (const zone of this.platform.service.AllZones) { this.removeAccessory(ManualSwitch_1.ManualSwitch.name, zone); } for (const mode of ['A', 'H', 'C', 'E']) { for (const zone of this.platform.service.AllZones) { if (zones.includes(zone) && this.deviceModes.includes(mode)) { let displayName = 'Manual'; if (this.platform.service.getHasMultiSetPoint()) { displayName += ` ${this.platform.service.getZoneName(zone)}`; } displayName += ` ${this.modeNames[mode]}`; this.addAccessory(ManualSwitch_1.ManualSwitch, ManualSwitch_1.ManualSwitch.name, displayName.trim(), zone, mode); } else { this.removeAccessory(ManualSwitch_1.ManualSwitch.name, zone, mode); } } } } discoverPump(devices) { this.platform.log.debug(this.constructor.name, 'discoverPump', devices); if ('evap' in devices) { this.addAccessory(Pump_1.Pump, Pump_1.Pump.name, 'Evaporative Pump'); } else { this.removeAccessory(Pump_1.Pump.name); } } addAccessory(Accessory, name, displayName, zone, mode) { this.platform.log.debug(this.constructor.name, 'addAccessory', 'Accessory', name, displayName, zone, mode); const key = this.getKey(name, zone, mode); if (this.accessories.has(key)) { return; } const uuid = this.platform.api.hap.uuid.generate(key); const platformAccessory = new this.platform.api.platformAccessory(displayName, uuid); platformAccessory.context.type = name.toLowerCase(); platformAccessory.context.zone = zone; platformAccessory.context.mode = mode; platformAccessory.context.key = key; const accessory = new Accessory(this.platform, platformAccessory); this.accessories.set(key, accessory); this.platform.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [platformAccessory]); this.platform.log.info(`Add ${name}: ${platformAccessory.displayName}`); } removeAccessory(name, zone, mode) { this.platform.log.debug(this.constructor.name, 'removeAccessory', name, zone, mode); const key = this.getKey(name, zone, mode); if (!this.accessories.has(key)) { return; } const accessory = this.accessories.get(key); const platformAccessory = accessory.platformAccessory; this.platform.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [platformAccessory]); this.accessories.delete(key); this.platform.log.info(`Remove ${name}: ${platformAccessory.displayName}`); } // Called from configureAccessory configure(platformAccessory) { this.platform.log.debug(this.constructor.name, 'configure', 'platformAccessory'); this.platform.log.info(`Configure ${platformAccessory.displayName}`); const accessory = this.createAccessory(platformAccessory); if (accessory) { this.accessories.set(platformAccessory.context.key, accessory); } } createAccessory(platformAccessory) { this.platform.log.debug(this.constructor.name, 'createAccessory', 'platformAccessory'); switch (platformAccessory.context.type) { case 'thermostat': return new Thermostat_1.Thermostat(this.platform, platformAccessory); case 'heatercooler': return new HeaterCooler_1.HeaterCooler(this.platform, platformAccessory); case 'fan': return new Fan_1.Fan(this.platform, platformAccessory); case 'zoneswitch': return new ZoneSwitch_1.ZoneSwitch(this.platform, platformAccessory); case 'advanceswitch': return new AdvanceSwitch_1.AdvanceSwitch(this.platform, platformAccessory); case 'manualswitch': return new ManualSwitch_1.ManualSwitch(this.platform, platformAccessory); case 'pump': return new Pump_1.Pump(this.platform, platformAccessory); } return; } getKey(name, zone, mode) { this.platform.log.debug(this.constructor.name, 'getKey', zone); return name + (zone ? `_${zone}` : '') + (mode ? `_${mode}` : ''); } } exports.AccessoryService = AccessoryService; //# sourceMappingURL=AccessoryService.js.map