Skip to content

Commit

Permalink
Add on/off and temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
irinel-nistor-lego committed Jul 6, 2021
1 parent 2392d41 commit 7535f29
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"eslint": "^7.21.0",
"homebridge": "^1.3.1",
"homebridge": "^1.3.4",
"nodemon": "^2.0.7",
"rimraf": "^3.0.2",
"ts-node": "^9.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { API } from 'homebridge';

import { PLATFORM_NAME } from './settings';
import { ExampleHomebridgePlatform } from './platform';
import { SamsungAC } from './platform';

/**
* This method registers the platform with Homebridge
*/
export = (api: API) => {
api.registerPlatform(PLATFORM_NAME, ExampleHomebridgePlatform);
api.registerPlatform(PLATFORM_NAME, SamsungAC);
};
54 changes: 28 additions & 26 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class SamsungAC implements DynamicPlatformPlugin {
public readonly config: PlatformConfig,
public readonly api: API,
) {
this.log.debug('Finished initializing platform:', this.config.name);
this.log.info('Finished initializing platform:');

this.api.on('didFinishLaunching', async () => {
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
await this.discoverDevices();
this.discoverDevices();
});
}

Expand All @@ -30,29 +30,31 @@ export class SamsungAC implements DynamicPlatformPlugin {
this.accessories.push(accessory);
}

async discoverDevices() {
const samsungDevices = await SamsungAPI.getDevices('ef7a9c71-ef1a-4864-8bc9-7265b5deb355');

for (const device of samsungDevices) {
const uuid = this.api.hap.uuid.generate(device.deviceId);

const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);

if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);

new SamsungACPlatformAccessory(this, existingAccessory, this.log);
} else {
this.log.info('Adding new accessory:', device.label);

const accessory = new this.api.platformAccessory(device.label, uuid);

accessory.context.device = device;

new SamsungACPlatformAccessory(this, accessory, this.log);

this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
discoverDevices() {
const token = 'ef7a9c71-ef1a-4864-8bc9-7265b5deb355';
SamsungAPI.getDevices(token).then(samsungDevices => {
for (const device of samsungDevices) {
const uuid = this.api.hap.uuid.generate(device.deviceId.toString());

const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);

if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);

new SamsungACPlatformAccessory(this, existingAccessory);
} else {
this.log.info('Adding new accessory:', device.label);

const accessory = new this.api.platformAccessory(device.label, uuid);

accessory.context.device = device;
accessory.context.token = token;

new SamsungACPlatformAccessory(this, accessory);

this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
});
}
}
24 changes: 12 additions & 12 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Service, PlatformAccessory } from 'homebridge';

import { SamsungAC } from './platform';

import { SamsungAPI } from './samsungApi';

export class SamsungACPlatformAccessory {
private service: Service;
Expand All @@ -16,7 +16,6 @@ export class SamsungACPlatformAccessory {
private readonly accessory: PlatformAccessory,
// public readonly log: Logger,
) {

this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.context.device.manufacturerName)
.setCharacteristic(this.platform.Characteristic.Model, accessory.context.device.deviceTypeName)
Expand All @@ -28,7 +27,7 @@ export class SamsungACPlatformAccessory {
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.label);

// register handlers for the On/Off Characteristic
this.service.getCharacteristic(this.platform.Characteristic.On)
this.service.getCharacteristic(this.platform.Characteristic.Active)
.onSet(this.handleActiveSet.bind(this))
.onGet(this.handleActiveGet.bind(this));

Expand All @@ -46,18 +45,20 @@ export class SamsungACPlatformAccessory {
/**
* Handle requests to get the current value of the "Active" characteristic
*/
handleActiveGet() {
async handleActiveGet() {
// set this to a valid value for Active
const currentValue = this.platform.Characteristic.Active.INACTIVE;

return currentValue;
let status = await SamsungAPI.getDeviceStatus(this.accessory.context.device.deviceId, this.accessory.context.token);
return status === this.states.Off ? currentValue : this.platform.Characteristic.Active.ACTIVE;
}

/**
* Handle requests to set the "Active" characteristic
*/
handleActiveSet(value) {
// do something
async handleActiveSet(value) {
console.log(value + " value");
let statusValue = value === 1 ? this.states.On : this.states.Off ;
await SamsungAPI.setDeviceStatus(this.accessory.context.device.deviceId, statusValue, this.accessory.context.token);
}

/**
Expand Down Expand Up @@ -91,10 +92,9 @@ export class SamsungACPlatformAccessory {
/**
* Handle requests to get the current value of the "Current Temperature" characteristic
*/
handleCurrentTemperatureGet() {
async handleCurrentTemperatureGet() {
// set this to a valid value for CurrentTemperature
const currentValue = -270;

return currentValue;
let temperature = await SamsungAPI.getDeviceTemperature(this.accessory.context.device.deviceId, this.accessory.context.token);
return temperature;
}
}
20 changes: 19 additions & 1 deletion src/samsungApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ export class SamsungAPI {
static async getDevices(token) {
const { data: { items = [] } = {} } = await Axios.get('https://api.smartthings.com/v1/devices', this.setToken(token));

return items.map(item => item.name === AC_DEVICE_NAME);
return items.filter(item => item.name === AC_DEVICE_NAME);
}

static async getDeviceStatus(deviceId, token) {
const response = await Axios.get(`https://api.smartthings.com/v1/devices/${deviceId}/components/main/capabilities/switch/status`, this.setToken(token));
return response.data.switch.value;
}

static async setDeviceStatus(deviceId, status, token) {
let data = {
"commands" : [{"capability": "switch", "command": status}]
};

await Axios.post(`https://api.smartthings.com/v1/devices/${deviceId}/commands`, data, this.setToken(token));
}

static async getDeviceTemperature(deviceId, token) {
const { data: { temperature = { } } = {} } = await Axios.get(`https://api.smartthings.com/v1/devices/${deviceId}/components/main/capabilities/temperatureMeasurement/status`, this.setToken(token));
return temperature.value;
}
}

0 comments on commit 7535f29

Please sign in to comment.