diff --git a/fs_src/script.js b/fs_src/script.js index 76d7e61d..9f1d0c2a 100644 --- a/fs_src/script.js +++ b/fs_src/script.js @@ -673,11 +673,7 @@ function updateComponent(cd) { updateInnerText(el(c, "head"), headText); setValueIfNotModified(el(c, "name"), cd.name); el(c, "state").checked = cd.state; - if (cd.apower !== undefined) { - updateInnerText( - el(c, "power_stats"), `${Math.round(cd.apower)}W, ${cd.aenergy}Wh`); - el(c, "power_stats_container").style.display = "block"; - } + updatePowerStats(c, cd); if (cd.type == Component_Type.kLightBulb) { checkIfNotModified(el(c, "svc_hidden"), cd.svc_hidden); if (cd.hap_optional !== undefined && cd.hap_optional == 0) { @@ -739,12 +735,7 @@ function updateComponent(cd) { updateInnerText(el(c, "head"), headText); setValueIfNotModified(el(c, "name"), cd.name); el(c, "state").checked = cd.state; - if (cd.apower !== undefined) { - updateInnerText( - el(c, "power_stats"), - `${Math.round(cd.apower)}W, ${cd.aenergy}Wh`); - el(c, "power_stats_container").style.display = "block"; - } + updatePowerStats(c, cd); slideIfNotModified(el(c, "color_temperature"), cd.color_temperature); slideIfNotModified(el(c, "hue"), cd.hue); slideIfNotModified(el(c, "saturation"), cd.saturation); @@ -1069,6 +1060,15 @@ function updateElement(key, value, info) { } } +function updatePowerStats(c, cd) { + if (cd.apower === undefined) return; + + apower = Math.round(cd.apower * 10) / 10; + console.log(apower) + updateInnerText(el(c, "power_stats"), `${apower}W, ${cd.aenergy}Wh`); + el(c, "power_stats_container").style.display = "block"; +} + function getInfo() { return new Promise(function(resolve, reject) { if (pendingGetInfo) { diff --git a/src/shelly_switch.cpp b/src/shelly_switch.cpp index c38dd121..13a5191c 100644 --- a/src/shelly_switch.cpp +++ b/src/shelly_switch.cpp @@ -368,10 +368,10 @@ void ShellySwitch::AddPowerMeter(uint16_t *iid) { if (out_pm_ == nullptr) return; // Power - power_char_ = new mgos::hap::UInt16Characteristic( + power_char_ = new mgos::hap::UFloatCharacteristic( (*iid)++, &kHAPCharacteristic_EveConsumption, 0, 65535, 1, [this](HAPAccessoryServerRef *, - const HAPUInt16CharacteristicReadRequest *, uint16_t *value) { + const HAPUFloatCharacteristicReadRequest *, float *value) { auto power = out_pm_->GetPowerW(); if (!power.ok()) return kHAPError_Busy; *value = power.ValueOrDie(); @@ -380,10 +380,10 @@ void ShellySwitch::AddPowerMeter(uint16_t *iid) { true /* supports_notification */, nullptr, "eve-power-consumption"); AddChar(power_char_); // Energy - total_power_char_ = new mgos::hap::UInt16Characteristic( + total_power_char_ = new mgos::hap::UFloatCharacteristic( (*iid)++, &kHAPCharacteristic_EveTotalConsumption, 0, 65535, 1, [this](HAPAccessoryServerRef *, - const HAPUInt16CharacteristicReadRequest *, uint16_t *value) { + const HAPUFloatCharacteristicReadRequest *, float *value) { auto energy = out_pm_->GetEnergyWH(); if (!energy.ok()) return kHAPError_Busy; *value = energy.ValueOrDie() / 1000.0f;