Skip to content

Commit

Permalink
Merge pull request #60 from awahlig/master
Browse files Browse the repository at this point in the history
Add support for Coffee machines.
  • Loading branch information
QuickSander authored Dec 19, 2021
2 parents df4a902 + 57b6cdc commit 171aac0
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ It (currently) requires a
- Miele Fridge.
- Miele Freezer (e.g. FN28263).
- Miele Fridge Freezer combination.
- Miele Coffee machine (e.g. CVA7845).

## Features
- Easy setup: guided process to retrieve token via OAuth2 from Miele.
Expand Down Expand Up @@ -67,7 +68,7 @@ Fridge / Freezer / Fridge Freezer combination:
- [Wiki](../../wiki/)

## Planned features
- Add support for oven, hob and coffee machine?
- Add support for oven and hob.
- Add Custom characteristic to display current program running.

## Thanks
Expand Down
72 changes: 71 additions & 1 deletion src/mieleCharacteristics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class MieleBaseCharacteristic implements IMieleCharacteristic {

//-------------------------------------------------------------------------------------------------
// Update value only when not equal to cached value.
protected updateCharacteristic(value: string | number, logInfo = true) {
protected updateCharacteristic(value: CharacteristicValue, logInfo = true) {
if(value!==this.value) {
const logStr = `${this.deviceName}: Updating characteristic ${this.characteristic.name} to ${value}.`;
if(logInfo) {
Expand Down Expand Up @@ -151,6 +151,23 @@ export class MieleInUseCharacteristic extends MieleBinaryStateCharacteristic {
}
}


//-------------------------------------------------------------------------------------------------
// Miele OutletInUse Characteristic.
//-------------------------------------------------------------------------------------------------
export class MieleOutletInUseCharacteristic extends MieleBinaryStateCharacteristic {

constructor(
platform: MieleAtHomePlatform,
service: Service,
inactiveStates: MieleState[] | null,
activeStates: MieleState[] | null,
) {
super(platform, service, inactiveStates, activeStates, platform.Characteristic.OutletInUse, 0, 1);
}
}


//-------------------------------------------------------------------------------------------------
// Miele Current Cooling Characteristic.
//-------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -467,3 +484,56 @@ export class MieleTemperatureUnitCharacteristic extends MieleBaseCharacteristic
}

}


//-------------------------------------------------------------------------------------------------
// Base class: Miele On Characteristic
//-------------------------------------------------------------------------------------------------
export class MieleOnCharacteristic extends MieleBaseCharacteristic {

constructor(
platform: MieleAtHomePlatform,
service: Service,
private serialNumber: string,
) {
super(platform, service, platform.Characteristic.On, false);
}

async set(value: CharacteristicValue, callback: CharacteristicSetCallback) {
this.platform.log.debug(`${this.deviceName}: Set characteristic ${this.characteristic.name} to: ${value}`);

callback(null);

try {
const response = await axios.get(this.platform.getActionsUrl(this.serialNumber),
this.platform.getHttpRequestConfig());

const action = value ? "powerOn" : "powerOff";
if (response.data[action]) {
const data = {};
data[action] = true;
const response = await axios.put(this.platform.getActionsUrl(this.serialNumber), data,
this.platform.getHttpRequestConfig());
this.platform.log.debug(`${this.deviceName}: Process action response code: ${response.status}: "${response.statusText}"`);
this.value = value;

} else {
this.platform.log.info(`${this.deviceName} (${this.serialNumber}): ` +
`Ignoring request to power ${value ? 'on' : 'off'} the device: not allowed in current device state. ` +
`Allowed power actions: on=${response.data.powerOn}, off=${response.data.powerOff}`);
this.undoSetState(value);
}

} catch (error) {
this.platform.log.error(createErrorString(error));
this.undoSetState(value);
}
}

update(response: MieleStatusResponse): void {
this.platform.log.debug(`${this.deviceName}: Update received for ${this.characteristic.name} raw value: ${response.status.value_raw}.`);

const value = response.status.value_raw !== MieleState.Off;
this.updateCharacteristic(value);
}
}
49 changes: 49 additions & 0 deletions src/mieleCoffeeSystemPlatformAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Apacche License
// Copyright (c) 2021, Arkadiusz Wahlig

import { PlatformAccessory } from 'homebridge';

import { MieleAtHomePlatform } from './platform';
import { MieleBasePlatformAccessory, MieleState } from './mieleBasePlatformAccessory';

import { MieleOnCharacteristic, MieleOutletInUseCharacteristic } from './mieleCharacteristics';
import { timeStamp } from 'console';

//-------------------------------------------------------------------------------------------------
// Class Coffee System
//-------------------------------------------------------------------------------------------------
export class MieleCoffeeSystemPlatformAccessory extends MieleBasePlatformAccessory {

//-----------------------------------------------------------------------------------------------
constructor(
platform: MieleAtHomePlatform,
accessory: PlatformAccessory,
) {
super(platform, accessory);

this.mainService = this.accessory.getService(this.platform.Service.Outlet) ||
this.accessory.addService(this.platform.Service.Outlet);

// Set the service name, this is what is displayed as the default name on the Home app
this.mainService.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.displayName);

const onCharacteristic = new MieleOnCharacteristic(this.platform, this.mainService, accessory.context.device.uniqueId);
const inUseCharacteristic = new MieleOutletInUseCharacteristic(this.platform, this.mainService, null,
[ MieleState.InUse, MieleState.Finished, MieleState.Cancelled ]);

this.characteristics.push(onCharacteristic);
this.characteristics.push(inUseCharacteristic);

// Each service must implement at-minimum the "required characteristics" for the given service type
// see https://developers.homebridge.io/#/service/Switch
this.mainService.getCharacteristic(this.platform.Characteristic.On)
.on('get', onCharacteristic.get.bind(onCharacteristic))
.on('set', onCharacteristic.set.bind(onCharacteristic));

this.mainService.getCharacteristic(this.platform.Characteristic.OutletInUse)
.on('get', inUseCharacteristic.get.bind(inUseCharacteristic));
}

}


5 changes: 5 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PLATFORM_NAME, PLUGIN_NAME, DEVICES_INFO_URL, DEFAULT_RECONNECT_EVENT_S
import { MieleHoodPlatformAccessory } from './mieleHoodPlatformAccessory';
import { MieleWasherDryerPlatformAccessory } from './mieleWasherDryerPlatformAccessory';
import { MieleFridgePlatformAccessory } from './mieleFridgePlatformAccessory';
import { MieleCoffeeSystemPlatformAccessory } from './mieleCoffeeSystemPlatformAccessory';
import { Token } from './token';

import axios from 'axios';
Expand All @@ -32,6 +33,7 @@ enum MieleDeviceIds {
Washer = 1,
Dryer = 2,
Dishwasher = 7,
CoffeeSystem = 17,
Hood = 18,
Fridge = 19,
Freezer = 20,
Expand Down Expand Up @@ -211,6 +213,9 @@ export class MieleAtHomePlatform implements DynamicPlatformPlugin {
this.disableStopActionFor.includes(MieleDeviceIds[raw_id]),
this.disableSetTargetTempFor.includes(MieleDeviceIds[raw_id]));

case MieleDeviceIds.CoffeeSystem:
return new MieleCoffeeSystemPlatformAccessory(this, accessory);

default:
return null;

Expand Down

0 comments on commit 171aac0

Please sign in to comment.