Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Commit

Permalink
fix component (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
takeyaqa authored Dec 28, 2021
1 parent e8123b3 commit 87ef71c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Allows for on/off, temperature and mode changing controls. The mode changing onl

### TVs

Allows for on/off controls.
Allows for on/off and volume controls.

## Changelog

Expand Down
8 changes: 5 additions & 3 deletions src/airConAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class NatureNemoAirConAccessory {
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, this.accessory.context.appliance.model.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, this.accessory.context.appliance.model.name)
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.accessory.context.appliance.device.firmware_version)
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.id)
.setCharacteristic(this.platform.Characteristic.Name, this.name);

Expand Down Expand Up @@ -71,22 +72,23 @@ export class NatureNemoAirConAccessory {
this.platform.logger.debug('[%s] Same state. skip sending', this.name);
return;
}
this.state.targetHeatingCoolingState = value;
if (value === this.platform.Characteristic.TargetHeatingCoolingState.AUTO) {
this.platform.logger.error('This plugin does not support auto');
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.INVALID_VALUE_IN_REQUEST);
} else if (value === this.platform.Characteristic.TargetHeatingCoolingState.OFF) {
await this.platform.natureRemoApi.setAirconPowerOff(this.id);
this.platform.logger.info('[%s] Target Heater Cooler State <- OFF', this.name);
this.state.targetHeatingCoolingState = value;
} else {
const mode = this.convertOperationMode(value);
await this.platform.natureRemoApi.setAirconOperationMode(this.id, mode);
this.platform.logger.info('[%s] Target Heater Cooler State <- %s', this.name, mode);
this.state.targetHeatingCoolingState = value;
}
}

async getCurrentTemperature(): Promise<CharacteristicValue> {
const device = await this.platform.natureRemoApi.getSensorValue(this.deviceId);
const device = await this.platform.natureRemoApi.getDevice(this.deviceId);
if (device.newest_events.te) {
this.platform.logger.info('[%s] Current Temperature -> %s', this.name, device.newest_events.te.val);
return device.newest_events.te.val;
Expand Down Expand Up @@ -132,7 +134,7 @@ export class NatureNemoAirConAccessory {
}
}

private convertOperationMode(state: number): string {
private convertOperationMode(state: number): 'warm' | 'cool' {
if (state === this.platform.Characteristic.TargetHeatingCoolingState.HEAT) {
return 'warm';
} else if (state === this.platform.Characteristic.TargetHeatingCoolingState.COOL) {
Expand Down
6 changes: 4 additions & 2 deletions src/lightAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class NatureNemoLightAccessory {
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, this.accessory.context.appliance.model.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, this.accessory.context.appliance.model.name)
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.accessory.context.appliance.device.firmware_version)
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.id)
.setCharacteristic(this.platform.Characteristic.Name, this.name);

Expand All @@ -41,7 +42,8 @@ export class NatureNemoLightAccessory {
if (typeof value !== 'boolean') {
throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.INVALID_VALUE_IN_REQUEST);
}
await this.platform.natureRemoApi.setLight(this.id, value);
this.platform.logger.info('[%s] On <- %s', this.name, value);
const power = value ? 'on' : 'off';
await this.platform.natureRemoApi.setLight(this.id, power);
this.platform.logger.info('[%s] Power <- %s', this.name, power);
}
}
16 changes: 8 additions & 8 deletions src/natureRemoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { URLSearchParams } from 'url';
import axios, { Axios, AxiosError } from 'axios';
import { Mutex } from 'async-mutex';
import { API, HapStatusError, Logger } from 'homebridge';
import { AirConParams, Appliance, Device, LIGHTState } from './types';
import { ACButton, AirConParams, Appliance, Device, LIGHTState, OperationMode } from './types';
import { API_URL, CACHE_THRESHOLD } from './settings';

interface Cache {
updated: number;
Expand All @@ -16,8 +17,7 @@ interface DeviceCache extends Cache {
devices: Device[] | null;
}

const API_URL = 'https://api.nature.global/1';
const CACHE_THRESHOLD = 10 * 1000;
type AirConSettings = { button?: ACButton; operation_mode?: OperationMode; temperature?: string };

export class NatureRemoApi {

Expand Down Expand Up @@ -77,7 +77,7 @@ export class NatureRemoApi {
return appliance.light.state;
}

async getSensorValue(id: string): Promise<Device> {
async getDevice(id: string): Promise<Device> {
const devices = await this.getAllDevices();
const device = devices.find(val => val.id === id);
if (device === undefined) {
Expand All @@ -86,16 +86,16 @@ export class NatureRemoApi {
return device;
}

async setLight(applianceId: string, power: boolean): Promise<void> {
async setLight(applianceId: string, power: 'on' | 'off'): Promise<void> {
const url = `/appliances/${applianceId}/light`;
this.postMessage(url, { 'button': power ? 'on' : 'off' });
this.postMessage(url, { 'button': power });
}

async setAirconPowerOff(applianceId: string): Promise<void> {
this.setAirconSettings(applianceId, { 'button': 'power-off'});
}

async setAirconOperationMode(applianceId: string, operationMode: string): Promise<void> {
async setAirconOperationMode(applianceId: string, operationMode: OperationMode): Promise<void> {
this.setAirconSettings(applianceId, { 'operation_mode': operationMode, 'button': '' });
}

Expand All @@ -108,7 +108,7 @@ export class NatureRemoApi {
this.postMessage(url, { 'button': button });
}

private async setAirconSettings(applianceId: string, settings: Record<string, string>): Promise<void> {
private async setAirconSettings(applianceId: string, settings: AirConSettings): Promise<void> {
const url = `/appliances/${applianceId}/aircon_settings`;
this.postMessage(url, settings);
}
Expand Down
11 changes: 5 additions & 6 deletions src/sensorAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
import { NatureRemoPlatform } from './platform';

const UPDATE_INTERVAL = 1000 * 60 * 5;
import { UPDATE_INTERVAL } from './settings';

export class NatureNemoSensorAccessory {
private readonly tempertureSensorservice?: Service;
Expand Down Expand Up @@ -55,7 +54,7 @@ export class NatureNemoSensorAccessory {

setInterval(async () => {
this.platform.logger.info('[%s] Update sensor values', this.name);
const device = await this.platform.natureRemoApi.getSensorValue(this.id);
const device = await this.platform.natureRemoApi.getDevice(this.id);
if (device.newest_events.te) {
const teVal = device.newest_events.te.val;
this.platform.logger.info('[%s] Current Temperature -> %s', this.name, teVal);
Expand All @@ -76,7 +75,7 @@ export class NatureNemoSensorAccessory {

async getCurrentTemperature(): Promise<CharacteristicValue> {
this.platform.logger.debug('getCurrentTemperature called');
const device = await this.platform.natureRemoApi.getSensorValue(this.id);
const device = await this.platform.natureRemoApi.getDevice(this.id);
if (device.newest_events.te) {
this.platform.logger.info('[%s] Current Temperature -> %s', this.name, device.newest_events.te.val);
return device.newest_events.te.val;
Expand All @@ -87,7 +86,7 @@ export class NatureNemoSensorAccessory {

async getCurrentHumidity(): Promise<CharacteristicValue> {
this.platform.logger.debug('getCurrentHumidity called');
const device = await this.platform.natureRemoApi.getSensorValue(this.id);
const device = await this.platform.natureRemoApi.getDevice(this.id);
if (device.newest_events.hu) {
this.platform.logger.info('[%s] Current Humidity -> %s', this.name, device.newest_events.hu.val);
return device.newest_events.hu.val;
Expand All @@ -98,7 +97,7 @@ export class NatureNemoSensorAccessory {

async getCurrentLightLevel(): Promise<CharacteristicValue> {
this.platform.logger.debug('getCurrentLightLevel called');
const device = await this.platform.natureRemoApi.getSensorValue(this.id);
const device = await this.platform.natureRemoApi.getDevice(this.id);
if (device.newest_events.il) {
const ilVal = device.newest_events.il.val >= 0.0001 ? device.newest_events.il.val : 0.0001;
this.platform.logger.info('[%s] Current Light Level -> %s', this.name, ilVal);
Expand Down
3 changes: 3 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const PLATFORM_NAME = 'NatureRemoPlatformPlugin';
export const PLUGIN_NAME = '@takeya0x86/homebridge-nature-remo-platform';
export const API_URL = 'https://api.nature.global/1';
export const CACHE_THRESHOLD = 10 * 1000;
export const UPDATE_INTERVAL = 1000 * 60 * 5;
1 change: 1 addition & 0 deletions src/tvAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class NatureNemoTvAccessory {
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, this.accessory.context.appliance.model.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, this.accessory.context.appliance.model.name)
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.accessory.context.appliance.device.firmware_version)
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.id)
.setCharacteristic(this.platform.Characteristic.Name, this.name);

Expand Down
18 changes: 9 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
type ApplianceType = 'AC' | 'TV' | 'LIGHT' | 'IR' | 'EL_SMART_METER';
type Id = string;
type Image = string;
type DateTime = string;
type OperationMode = '' | 'cool' | 'warm' | 'dry' | 'blow' | 'auto';
type Temperature = string;
type AirVolume = '' | 'auto' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10';
type AirDirection = string;
type ACButton = '' | 'power-off';
export type ApplianceType = 'AC' | 'TV' | 'LIGHT' | 'IR' | 'EL_SMART_METER';
export type Id = string;
export type Image = string;
export type DateTime = string;
export type OperationMode = '' | 'cool' | 'warm' | 'dry' | 'blow' | 'auto';
export type Temperature = string;
export type AirVolume = '' | 'auto' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10';
export type AirDirection = string;
export type ACButton = '' | 'power-off';

export interface User {
id: Id;
Expand Down

0 comments on commit 87ef71c

Please sign in to comment.