diff --git a/fs_src/index.html b/fs_src/index.html index 9566da54..5e229f04 100644 --- a/fs_src/index.html +++ b/fs_src/index.html @@ -446,8 +446,14 @@

Switch

diff --git a/fs_src/script.js b/fs_src/script.js index d8402269..3226aa38 100644 --- a/fs_src/script.js +++ b/fs_src/script.js @@ -1113,12 +1113,18 @@ 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`); + updateInnerText( + el(c, "power_stats_current"), `${formatFloat(cd.apower, 3)} W`); + updateInnerText( + el(c, "power_stats_total"), `${formatFloat(cd.aenergy, 3)} Wh`); el(c, "power_stats_container").style.display = "block"; } +function formatFloat(number, digits) { + return new Intl.NumberFormat("en-EN", {minimumFractionDigits: digits}) + .format(number); +} + function getInfo() { return new Promise(function(resolve, reject) { if (pendingGetInfo) { diff --git a/src/shelly_switch.cpp b/src/shelly_switch.cpp index 5dff030b..71b4b85f 100644 --- a/src/shelly_switch.cpp +++ b/src/shelly_switch.cpp @@ -382,10 +382,10 @@ void ShellySwitch::AddPowerMeter(uint16_t *iid) { if (out_pm_ == nullptr) return; // Power - power_char_ = new mgos::hap::UInt16Characteristic( - (*iid)++, &kHAPCharacteristic_EveConsumption, 0, 65535, 1, - [this](HAPAccessoryServerRef *, - const HAPUInt16CharacteristicReadRequest *, uint16_t *value) { + power_char_ = new mgos::hap::FloatCharacteristic( + (*iid)++, &kHAPCharacteristic_EveConsumption, 0.0f, 65535.0f, 0.1f, + [this](HAPAccessoryServerRef *, const HAPFloatCharacteristicReadRequest *, + float *value) { auto power = out_pm_->GetPowerW(); if (!power.ok()) return kHAPError_Busy; *value = power.ValueOrDie(); @@ -394,10 +394,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( - (*iid)++, &kHAPCharacteristic_EveTotalConsumption, 0, 65535, 1, - [this](HAPAccessoryServerRef *, - const HAPUInt16CharacteristicReadRequest *, uint16_t *value) { + total_power_char_ = new mgos::hap::FloatCharacteristic( + (*iid)++, &kHAPCharacteristic_EveTotalConsumption, 0.0f, 65535.0f, 0.1f, + [this](HAPAccessoryServerRef *, const HAPFloatCharacteristicReadRequest *, + float *value) { auto energy = out_pm_->GetEnergyWH(); if (!energy.ok()) return kHAPError_Busy; *value = energy.ValueOrDie() / 1000.0f;