Return TP-Link sensor & light attributes as float rather than string (#48828)

This commit is contained in:
Philip Allgaier 2021-04-08 16:51:59 +02:00 committed by GitHub
parent 91837f08ce
commit f1c4072d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 16 deletions

View File

@ -382,8 +382,8 @@ class TPLinkSmartBulb(LightEntity):
or self._last_current_power_update + CURRENT_POWER_UPDATE_INTERVAL < now or self._last_current_power_update + CURRENT_POWER_UPDATE_INTERVAL < now
): ):
self._last_current_power_update = now self._last_current_power_update = now
self._emeter_params[ATTR_CURRENT_POWER_W] = "{:.1f}".format( self._emeter_params[ATTR_CURRENT_POWER_W] = round(
self.smartbulb.current_consumption() float(self.smartbulb.current_consumption()), 1
) )
if ( if (
@ -395,11 +395,11 @@ class TPLinkSmartBulb(LightEntity):
daily_statistics = self.smartbulb.get_emeter_daily() daily_statistics = self.smartbulb.get_emeter_daily()
monthly_statistics = self.smartbulb.get_emeter_monthly() monthly_statistics = self.smartbulb.get_emeter_monthly()
try: try:
self._emeter_params[ATTR_DAILY_ENERGY_KWH] = "{:.3f}".format( self._emeter_params[ATTR_DAILY_ENERGY_KWH] = round(
daily_statistics[int(time.strftime("%d"))] float(daily_statistics[int(time.strftime("%d"))]), 3
) )
self._emeter_params[ATTR_MONTHLY_ENERGY_KWH] = "{:.3f}".format( self._emeter_params[ATTR_MONTHLY_ENERGY_KWH] = round(
monthly_statistics[int(time.strftime("%m"))] float(monthly_statistics[int(time.strftime("%m"))]), 3
) )
except KeyError: except KeyError:
# device returned no daily/monthly history # device returned no daily/monthly history

View File

@ -138,23 +138,23 @@ class SmartPlugSwitch(SwitchEntity):
if self.smartplug.has_emeter: if self.smartplug.has_emeter:
emeter_readings = self.smartplug.get_emeter_realtime() emeter_readings = self.smartplug.get_emeter_realtime()
self._emeter_params[ATTR_CURRENT_POWER_W] = "{:.2f}".format( self._emeter_params[ATTR_CURRENT_POWER_W] = round(
emeter_readings["power"] float(emeter_readings["power"]), 2
) )
self._emeter_params[ATTR_TOTAL_ENERGY_KWH] = "{:.3f}".format( self._emeter_params[ATTR_TOTAL_ENERGY_KWH] = round(
emeter_readings["total"] float(emeter_readings["total"]), 3
) )
self._emeter_params[ATTR_VOLTAGE] = "{:.1f}".format( self._emeter_params[ATTR_VOLTAGE] = round(
emeter_readings["voltage"] float(emeter_readings["voltage"]), 1
) )
self._emeter_params[ATTR_CURRENT_A] = "{:.2f}".format( self._emeter_params[ATTR_CURRENT_A] = round(
emeter_readings["current"] float(emeter_readings["current"]), 2
) )
emeter_statics = self.smartplug.get_emeter_daily() emeter_statics = self.smartplug.get_emeter_daily()
with suppress(KeyError): # Device returned no daily history with suppress(KeyError): # Device returned no daily history
self._emeter_params[ATTR_TODAY_ENERGY_KWH] = "{:.3f}".format( self._emeter_params[ATTR_TODAY_ENERGY_KWH] = round(
emeter_statics[int(time.strftime("%e"))] float(emeter_statics[int(time.strftime("%e"))]), 3
) )
return True return True
except (SmartDeviceException, OSError) as ex: except (SmartDeviceException, OSError) as ex: