Skip to content

Commit

Permalink
Update P1Service.js
Browse files Browse the repository at this point in the history
- Remove separate ElectricityCapability accessory, see #72.
- Expose ElectricityCapacity as _Air Pressure_ on Electricity accessory, incl. Eve history, see #72;
- Expose _Current Consumption_ (computed from delta in _Total Consumption_) on Gas and Water accessories.
- Expose all accessories using _Outlet_ service.
  • Loading branch information
ebaauw committed Feb 24, 2023
1 parent a7bb3de commit 1d41d17
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions lib/P1Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@ class P1Service extends homebridgeLib.ServiceDelegate {
constructor (p1Accessory, data, subtype = '') {
const params = {
name: p1Accessory.name + (subtype === '' ? '' : ' ' + subtype),
Service: p1Accessory.platform.config.outlet
? p1Accessory.Services.hap.Outlet
: p1Accessory.Services.my.Resource,
Service: p1Accessory.Services.hap.Outlet,
subtype
}
super(p1Accessory, params)
if (this.platform.config.outlet) {
this.addCharacteristicDelegate({
key: 'on',
Characteristic: this.Characteristics.hap.On,
value: false
}).on('didSet', (value) => {
if (value) {
setTimeout(() => {
this.values.on = false
}, 500)
}
})
}
this.addCharacteristicDelegate({
key: 'on',
Characteristic: this.Characteristics.hap.On,
value: false
}).on('didSet', async (value) => {
if (value) {
await homebridgeLib.timeout(50)
this.values.on = false
}
})
}

static get Electricity () { return Electricity }
static get ElectricityCapacity () { return ElectricityCapacity }
static get Phase () { return Phase }
static get Gas () { return Gas }
static get Water () { return Water }
Expand Down Expand Up @@ -82,6 +76,15 @@ class Electricity extends P1Service {
Characteristic: this.Characteristics.my.Tariff
})
if (this.name === 'Electricity') {
if (data.avg_power != null) {
this.addCharacteristicDelegate({
key: 'airPressure',
Characteristic: this.Characteristics.eve.AirPressure,
unit: ' W',
props: { minValue: 0, maxValue: 10000, minStep: 0.1 },
value: data.avg_power
})
}
this.addCharacteristicDelegate({
key: 'logLevel',
Characteristic: this.Characteristics.my.LogLevel,
Expand All @@ -98,6 +101,9 @@ class Electricity extends P1Service {
this.values.consumptionLow = data.consumption.low
this.values.tariff = data.tariff
this.values.power = data.power
if (data.avg_power != null) {
this.values.airPressure = data.avg_power
}
if (data.l2 == null && data.l3 == null) {
if (data.l1.current != null) {
this.values.current = data.l1.current
Expand All @@ -111,34 +117,6 @@ class Electricity extends P1Service {
}
}

class ElectricityCapacity extends P1Service {
constructor (p1Accessory, data) {
super(p1Accessory, data)
this.addCharacteristicDelegate({
key: 'consumption',
Characteristic: this.Characteristics.eve.TotalConsumption
})
this.addCharacteristicDelegate({
key: 'power',
Characteristic: this.Characteristics.eve.CurrentConsumption
})
this.addCharacteristicDelegate({
key: 'lastUpdated',
Characteristic: this.Characteristics.my.LastUpdated,
silent: true
})
this.check(data)
}

check (data) {
this.values.consumption =
Math.round(1000 * (data.consumption.low + data.consumption.normal)) / 1000
this.values.power = data.avg_power
const date = data.lastupdated == null ? new Date() : new Date(data.lastupdated)
this.values.lastUpdated = String(date).slice(0, 24)
}
}

class Phase extends P1Service {
constructor (p1Accessory, data, subtype) {
super(p1Accessory, data, subtype)
Expand Down Expand Up @@ -180,6 +158,11 @@ class Gas extends P1Service {
Characteristic: this.Characteristics.eve.TotalConsumption,
unit: ' m³'
})
this.addCharacteristicDelegate({
key: 'power',
unit: ' l/h',
Characteristic: this.Characteristics.eve.CurrentConsumption
})
this.addCharacteristicDelegate({
key: 'lastUpdated',
Characteristic: this.Characteristics.my.LastUpdated,
Expand All @@ -203,6 +186,11 @@ class Water extends P1Service {
Characteristic: this.Characteristics.eve.TotalConsumption,
unit: ' m³'
})
this.addCharacteristicDelegate({
key: 'power',
unit: ' l/h',
Characteristic: this.Characteristics.eve.CurrentConsumption
})
this.addCharacteristicDelegate({
key: 'lastUpdated',
Characteristic: this.Characteristics.my.LastUpdated,
Expand Down

0 comments on commit 1d41d17

Please sign in to comment.