Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TemSen property to receive current room temperature #11

Merged
merged 2 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/property-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PROPERTY_VENDOR_CODES = {
mode: 'Mod',
temperatureUnit: 'TemUn',
temperature: 'SetTem',
currentTemperature: 'TemSen',
fanSpeed: 'WdSpd',
air: 'Air',
blow: 'Blo',
Expand All @@ -19,6 +20,19 @@ const PROPERTY_VENDOR_CODES = {
powerSave: 'SvSt',
};

const PROPERTY_OPTIONS = {
currentTemperature: {
readOnly: true,
fromVendorTransformer: function (value) {
// Temperature from the AC should be transformed by subtract 40 to get real temperature
// AC returns temperature+40. I believe it's because it has unsigned data type
// When TemSen=0 it means real temperature is -40 degrees
// @see https://github.com/ddenisyuk/homebridge-gree-heatercooler/blob/3979fc6dad9d1935c59c686eb1764a062246ee7c/index.js#L224-L226
return value - 40;
},
}
}

/**
* Transforms device properties from vendor names to human friendly names and back
* @private
Expand Down Expand Up @@ -52,7 +66,10 @@ class PropertyTransformer {
let ret = {};
for (let [property, value] of Object.entries(properties)) {
const reversedProperty = this._reversedProperties[property];
ret[reversedProperty] = this._reversedValues[reversedProperty][value] || value;
const transformValue = PROPERTY_OPTIONS[reversedProperty]
&& PROPERTY_OPTIONS[reversedProperty].fromVendorTransformer || (v => v);
const reversedValue = this._reversedValues[reversedProperty][value] || value;
ret[reversedProperty] = transformValue(reversedValue);
}
return ret;
}
Expand All @@ -65,6 +82,10 @@ class PropertyTransformer {
toVendor(properties) {
let ret = {};
for (let [property, value] of Object.entries(properties)) {
const propertyOptions = PROPERTY_OPTIONS[property] || {};
if (propertyOptions.readOnly) {
throw new Error(`Cannot set read-only property ${property}`)
}
const vendorProperty = this._properties[property];
const values = this._values[property] || {};
ret[vendorProperty] = values[value] !== undefined ? values[value] : value;
Expand Down
1 change: 1 addition & 0 deletions lib/property-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const PROPERTY_VALUE = {
fahrenheit: 'fahrenheit'
},
temperature: {},
currentTemperature: {},
fanSpeed: {
auto: 'auto',
low: 'low',
Expand Down
1 change: 1 addition & 0 deletions lib/property-vendor-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const PROPERTY_VALUE = {
fahrenheit: 1
},
temperature: {},
currentTemperature: {},
fanSpeed: {
auto: 0,
low: 1,
Expand Down
2 changes: 2 additions & 0 deletions lib/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @property {string} mode - Mode of operation
* @property {string} temperatureUnit - Temperature unit (must be together with set temperature)
* @property {string} temperature - Set temperature (must be together with temperature unit)
* @property {string} currentTemperature - Get current temperature from the internal (?) sensor (This value can not be set, only received)
* @property {string} fanSpeed - Fan speed
* @property {string} air - Fresh air valve
* @property {string} blow - Keeps the fan running for a while after shutting down (also called "X-Fan", only usable in Dry and Cool mode)
Expand All @@ -25,6 +26,7 @@ const PROPERTY = {
mode: 'mode',
temperatureUnit: 'temperatureUnit',
temperature: 'temperature',
currentTemperature: 'currentTemperature',
fanSpeed: 'fanSpeed',
air: 'air',
blow: 'blow',
Expand Down
17 changes: 15 additions & 2 deletions test/property-transformer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ describe('PropertyTransformer', function () {
const SUT = new PropertyTransformer();
const result = SUT.fromVendor({
Mod: 1,
SetTem: 25
SetTem: 25,
TemSen: 67,
});

assert.deepEqual(result, {
mode: 'cool',
temperature: 25
temperature: 25,
currentTemperature: 27
});
});
});
Expand All @@ -29,5 +31,16 @@ describe('PropertyTransformer', function () {
SetTem: 25
});
});
it('should not allow to change read-only property', function () {
const SUT = new PropertyTransformer();
assert.throws(() => {
SUT.toVendor({
currentTemperature: 30
})
}, {
name: 'Error',
message: 'Cannot set read-only property currentTemperature'
});
});
});
});