Skip to content

Commit

Permalink
fix: handle missing fields from device event
Browse files Browse the repository at this point in the history
  • Loading branch information
junbongwe committed Jan 13, 2025
1 parent aaa88e9 commit a2dec31
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
18 changes: 9 additions & 9 deletions src/aws/interfaces/device-event.inteface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export interface NavienDesiredState {
export interface NavienReportedState {
info: DeviceInfo;
connected: boolean;
errorCode: number;
operationMode: OperationMode;
heater: HeaterState;
childLock: boolean;
schedule: ScheduleState;
errorCode?: number;
operationMode?: OperationMode;
heater?: HeaterState;
childLock?: boolean;
schedule?: ScheduleState;
}

export enum OperationMode {
Expand All @@ -51,16 +51,16 @@ export enum OperationMode {
export type HeaterState = SingleHeaterState | DoubleHeaterState;

export interface SingleHeaterState {
single: HeaterItemState;
single?: HeaterItemState;
}

export interface DoubleHeaterState {
left: HeaterItemState;
right: HeaterItemState;
left?: HeaterItemState;
right?: HeaterItemState;
}

export interface HeaterItemState {
enable: boolean;
enable?: boolean;
temperature: {
set: number; // celcius
};
Expand Down
46 changes: 30 additions & 16 deletions src/navien/navien.device-status.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from 'homebridge';
import { BehaviorSubject, Subscription } from 'rxjs';

import { OperationMode } from '../aws/interfaces/index.js';
import { DoubleHeaterState, OperationMode, SingleHeaterState } from '../aws/interfaces/index.js';
import { AwsPubSub } from '../aws/pubsub.js';
import { NavienDevice } from './navien.device.js';

Expand Down Expand Up @@ -36,21 +36,35 @@ export class NavienDeviceStatusRepository {
}

// status update
const isActive = state.operationMode === OperationMode.ON;
const temperature = ('left' in state.heater ?
state.heater.left : // TODO: handle left and right
state.heater.single
).temperature.set;
const doubleTemperature = 'left' in state.heater ?
{ left: state.heater.left.temperature.set, right: state.heater.right.temperature.set } :
temperature;
const isLocked = state.childLock;
this.log.info('[AWS PubSub] current status:', { name: this.device.name, isActive, temperature: doubleTemperature, isLocked });

this.isActive = isActive;
this.temperature = temperature;
this.isLocked = isLocked;
this._isDouble = 'left' in state.heater;
if ('heater' in state) {
const heater = state.heater!;
if ('left' in heater || 'right' in heater) {
this._isDouble = true;
}
if ('single' in heater) {
this._isDouble = false;
}
const temperature = (this._isDouble ?
// TODO: handle left and right
(heater as DoubleHeaterState)?.left:
(heater as SingleHeaterState)?.single
)?.temperature?.set;
if (temperature !== undefined) {
this.temperature = temperature;
}
}
if ('childLock' in state) {
this.isLocked = state.childLock!;
}
if ('operationMode' in state) {
this.isActive = (state.operationMode === OperationMode.ON);
}

// updated status
this.log.info(
'[AWS PubSub] current status:',
{ name: this.device.name, isActive: this.isActive, temperature: this.temperature, isLocked: this.isLocked },
);
});
}

Expand Down

0 comments on commit a2dec31

Please sign in to comment.