From 4c51d0d2cf655518abb3604632c3cfd247e93a1d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 Oct 2021 09:28:39 -1000 Subject: [PATCH] Round tplink energy sensors to prevent insignificant updates (#56999) - These sensors wobble quite a bit and the precision did not have sensible limits which generated a massive amount of data in the database which was not very useful --- homeassistant/components/tplink/sensor.py | 10 ++++++++-- tests/components/tplink/test_sensor.py | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index 0afcf96dba5..9bd4a056d33 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -44,6 +44,7 @@ class TPLinkSensorEntityDescription(SensorEntityDescription): """Describes TPLink sensor entity.""" emeter_attr: str | None = None + precision: int | None = None ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( @@ -54,6 +55,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( state_class=STATE_CLASS_MEASUREMENT, name="Current Consumption", emeter_attr="power", + precision=1, ), TPLinkSensorEntityDescription( key=ATTR_TOTAL_ENERGY_KWH, @@ -62,6 +64,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( state_class=STATE_CLASS_TOTAL_INCREASING, name="Total Consumption", emeter_attr="total", + precision=3, ), TPLinkSensorEntityDescription( key=ATTR_TODAY_ENERGY_KWH, @@ -69,6 +72,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( device_class=DEVICE_CLASS_ENERGY, state_class=STATE_CLASS_TOTAL_INCREASING, name="Today's Consumption", + precision=3, ), TPLinkSensorEntityDescription( key=ATTR_VOLTAGE, @@ -77,6 +81,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( state_class=STATE_CLASS_MEASUREMENT, name="Voltage", emeter_attr="voltage", + precision=1, ), TPLinkSensorEntityDescription( key=ATTR_CURRENT_A, @@ -85,6 +90,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( state_class=STATE_CLASS_MEASUREMENT, name="Current", emeter_attr="current", + precision=2, ), ) @@ -97,11 +103,11 @@ def async_emeter_from_device( val = getattr(device.emeter_realtime, attr) if val is None: return None - return cast(float, val) + return round(cast(float, val), description.precision) # ATTR_TODAY_ENERGY_KWH if (emeter_today := device.emeter_today) is not None: - return cast(float, emeter_today) + return round(cast(float, emeter_today), description.precision) # today's consumption not available, when device was off all the day # bulb's do not report this information, so filter it out return None if device.is_bulb else 0.0 diff --git a/tests/components/tplink/test_sensor.py b/tests/components/tplink/test_sensor.py index 839588d2756..5413e036d96 100644 --- a/tests/components/tplink/test_sensor.py +++ b/tests/components/tplink/test_sensor.py @@ -34,14 +34,14 @@ async def test_color_light_with_an_emeter(hass: HomeAssistant) -> None: voltage=None, current=5, ) - bulb.emeter_today = 5000 + bulb.emeter_today = 5000.0036 with _patch_discovery(device=bulb), _patch_single_discovery(device=bulb): await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}}) await hass.async_block_till_done() await hass.async_block_till_done() expected = { - "sensor.my_bulb_today_s_consumption": 5000, + "sensor.my_bulb_today_s_consumption": 5000.004, "sensor.my_bulb_current": 5, } entity_id = "light.my_bulb" @@ -69,10 +69,10 @@ async def test_plug_with_an_emeter(hass: HomeAssistant) -> None: plug.color_temp = None plug.has_emeter = True plug.emeter_realtime = Mock( - power=100, - total=30, - voltage=121, - current=5, + power=100.06, + total=30.0049, + voltage=121.19, + current=5.035, ) plug.emeter_today = None with _patch_discovery(device=plug), _patch_single_discovery(device=plug): @@ -81,11 +81,11 @@ async def test_plug_with_an_emeter(hass: HomeAssistant) -> None: await hass.async_block_till_done() expected = { - "sensor.my_plug_current_consumption": 100, - "sensor.my_plug_total_consumption": 30, + "sensor.my_plug_current_consumption": 100.1, + "sensor.my_plug_total_consumption": 30.005, "sensor.my_plug_today_s_consumption": 0.0, - "sensor.my_plug_voltage": 121, - "sensor.my_plug_current": 5, + "sensor.my_plug_voltage": 121.2, + "sensor.my_plug_current": 5.04, } entity_id = "switch.my_plug" state = hass.states.get(entity_id)