From f1c4072d3c8502932c60e546ad03ae3c528c417e Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Thu, 8 Apr 2021 16:51:59 +0200 Subject: [PATCH] Return TP-Link sensor & light attributes as `float` rather than `string` (#48828) --- homeassistant/components/tplink/light.py | 12 ++++++------ homeassistant/components/tplink/switch.py | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/tplink/light.py b/homeassistant/components/tplink/light.py index 88c24d7cf30..8880373955f 100644 --- a/homeassistant/components/tplink/light.py +++ b/homeassistant/components/tplink/light.py @@ -382,8 +382,8 @@ class TPLinkSmartBulb(LightEntity): or self._last_current_power_update + CURRENT_POWER_UPDATE_INTERVAL < now ): self._last_current_power_update = now - self._emeter_params[ATTR_CURRENT_POWER_W] = "{:.1f}".format( - self.smartbulb.current_consumption() + self._emeter_params[ATTR_CURRENT_POWER_W] = round( + float(self.smartbulb.current_consumption()), 1 ) if ( @@ -395,11 +395,11 @@ class TPLinkSmartBulb(LightEntity): daily_statistics = self.smartbulb.get_emeter_daily() monthly_statistics = self.smartbulb.get_emeter_monthly() try: - self._emeter_params[ATTR_DAILY_ENERGY_KWH] = "{:.3f}".format( - daily_statistics[int(time.strftime("%d"))] + self._emeter_params[ATTR_DAILY_ENERGY_KWH] = round( + float(daily_statistics[int(time.strftime("%d"))]), 3 ) - self._emeter_params[ATTR_MONTHLY_ENERGY_KWH] = "{:.3f}".format( - monthly_statistics[int(time.strftime("%m"))] + self._emeter_params[ATTR_MONTHLY_ENERGY_KWH] = round( + float(monthly_statistics[int(time.strftime("%m"))]), 3 ) except KeyError: # device returned no daily/monthly history diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index 4d7dce37447..11b86d6254f 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -138,23 +138,23 @@ class SmartPlugSwitch(SwitchEntity): if self.smartplug.has_emeter: emeter_readings = self.smartplug.get_emeter_realtime() - self._emeter_params[ATTR_CURRENT_POWER_W] = "{:.2f}".format( - emeter_readings["power"] + self._emeter_params[ATTR_CURRENT_POWER_W] = round( + float(emeter_readings["power"]), 2 ) - self._emeter_params[ATTR_TOTAL_ENERGY_KWH] = "{:.3f}".format( - emeter_readings["total"] + self._emeter_params[ATTR_TOTAL_ENERGY_KWH] = round( + float(emeter_readings["total"]), 3 ) - self._emeter_params[ATTR_VOLTAGE] = "{:.1f}".format( - emeter_readings["voltage"] + self._emeter_params[ATTR_VOLTAGE] = round( + float(emeter_readings["voltage"]), 1 ) - self._emeter_params[ATTR_CURRENT_A] = "{:.2f}".format( - emeter_readings["current"] + self._emeter_params[ATTR_CURRENT_A] = round( + float(emeter_readings["current"]), 2 ) emeter_statics = self.smartplug.get_emeter_daily() with suppress(KeyError): # Device returned no daily history - self._emeter_params[ATTR_TODAY_ENERGY_KWH] = "{:.3f}".format( - emeter_statics[int(time.strftime("%e"))] + self._emeter_params[ATTR_TODAY_ENERGY_KWH] = round( + float(emeter_statics[int(time.strftime("%e"))]), 3 ) return True except (SmartDeviceException, OSError) as ex: