anthturner / TPLinkSmartDevices

C# Library for Discovering and Operating TP-Link Smart Devices (HS100/105/110, LB100/110/120/130)
Apache License 2.0
40 stars 18 forks source link

Emeter JSON format varies between models/firmware, improvement suggested. #11

Closed cdickens77 closed 2 years ago

cdickens77 commented 2 years ago

The JSON emeter values exposed by a current model KP115 Smart-Plug Mini match the field maps in PowerData.cs:

myPlug.Model = "KP115(US)"
myPlug.SoftwareVersion = "1.0.17 Build 210506 Rel.075231"
myPlug.CurrentPowerUsage._powerData = "{{
  "current_ma": 1542,
  "voltage_mv": 120356,
  "power_mw": 181156,
  "total_wh": 192,
  "err_code": 0
}}"

However, ironically, the latest firmware for HS110 according ot my KASA app does not:

myPlug.Model = "HS110(US)"
myPlug.SoftwareVersion = "1.2.6 Build 200727 Rel.121701"
myPlug.CurrentPowerUsage._powerData = "{{
  "current": 0.013112,
  "voltage": 122.613419,
  "power": 0,
  "total": 0.826,
  "err_code": 0
}}"

The attached PowerData.cs adds coalesce code to allow both formats.

PowerData.zip

cdickens77 commented 2 years ago

I just realized the informational .ToString method actually had a bug in it originally where the mW value was converted twice to W causing invalid readings for devices using the power_mw format. Revised version attached tested with both KP115 and HS110:

PowerData2.zip

anthturner commented 2 years ago

Thanks for the bug report! Can you open a pull request? I'd be happy to accept.

cdickens77 commented 2 years ago

Submitted. Thanks!

cdickens77 commented 2 years ago

Resolved by Pull #12. Closing.