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
This commit is contained in:
J. Nick Koston 2021-10-03 09:28:39 -10:00 committed by GitHub
parent 57851e9623
commit 4c51d0d2cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View File

@ -44,6 +44,7 @@ class TPLinkSensorEntityDescription(SensorEntityDescription):
"""Describes TPLink sensor entity.""" """Describes TPLink sensor entity."""
emeter_attr: str | None = None emeter_attr: str | None = None
precision: int | None = None
ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = ( ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
@ -54,6 +55,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
name="Current Consumption", name="Current Consumption",
emeter_attr="power", emeter_attr="power",
precision=1,
), ),
TPLinkSensorEntityDescription( TPLinkSensorEntityDescription(
key=ATTR_TOTAL_ENERGY_KWH, key=ATTR_TOTAL_ENERGY_KWH,
@ -62,6 +64,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=STATE_CLASS_TOTAL_INCREASING,
name="Total Consumption", name="Total Consumption",
emeter_attr="total", emeter_attr="total",
precision=3,
), ),
TPLinkSensorEntityDescription( TPLinkSensorEntityDescription(
key=ATTR_TODAY_ENERGY_KWH, key=ATTR_TODAY_ENERGY_KWH,
@ -69,6 +72,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=STATE_CLASS_TOTAL_INCREASING,
name="Today's Consumption", name="Today's Consumption",
precision=3,
), ),
TPLinkSensorEntityDescription( TPLinkSensorEntityDescription(
key=ATTR_VOLTAGE, key=ATTR_VOLTAGE,
@ -77,6 +81,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
name="Voltage", name="Voltage",
emeter_attr="voltage", emeter_attr="voltage",
precision=1,
), ),
TPLinkSensorEntityDescription( TPLinkSensorEntityDescription(
key=ATTR_CURRENT_A, key=ATTR_CURRENT_A,
@ -85,6 +90,7 @@ ENERGY_SENSORS: tuple[TPLinkSensorEntityDescription, ...] = (
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
name="Current", name="Current",
emeter_attr="current", emeter_attr="current",
precision=2,
), ),
) )
@ -97,11 +103,11 @@ def async_emeter_from_device(
val = getattr(device.emeter_realtime, attr) val = getattr(device.emeter_realtime, attr)
if val is None: if val is None:
return None return None
return cast(float, val) return round(cast(float, val), description.precision)
# ATTR_TODAY_ENERGY_KWH # ATTR_TODAY_ENERGY_KWH
if (emeter_today := device.emeter_today) is not None: 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 # today's consumption not available, when device was off all the day
# bulb's do not report this information, so filter it out # bulb's do not report this information, so filter it out
return None if device.is_bulb else 0.0 return None if device.is_bulb else 0.0

View File

@ -34,14 +34,14 @@ async def test_color_light_with_an_emeter(hass: HomeAssistant) -> None:
voltage=None, voltage=None,
current=5, current=5,
) )
bulb.emeter_today = 5000 bulb.emeter_today = 5000.0036
with _patch_discovery(device=bulb), _patch_single_discovery(device=bulb): with _patch_discovery(device=bulb), _patch_single_discovery(device=bulb):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}}) await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done() await hass.async_block_till_done()
expected = { expected = {
"sensor.my_bulb_today_s_consumption": 5000, "sensor.my_bulb_today_s_consumption": 5000.004,
"sensor.my_bulb_current": 5, "sensor.my_bulb_current": 5,
} }
entity_id = "light.my_bulb" entity_id = "light.my_bulb"
@ -69,10 +69,10 @@ async def test_plug_with_an_emeter(hass: HomeAssistant) -> None:
plug.color_temp = None plug.color_temp = None
plug.has_emeter = True plug.has_emeter = True
plug.emeter_realtime = Mock( plug.emeter_realtime = Mock(
power=100, power=100.06,
total=30, total=30.0049,
voltage=121, voltage=121.19,
current=5, current=5.035,
) )
plug.emeter_today = None plug.emeter_today = None
with _patch_discovery(device=plug), _patch_single_discovery(device=plug): 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() await hass.async_block_till_done()
expected = { expected = {
"sensor.my_plug_current_consumption": 100, "sensor.my_plug_current_consumption": 100.1,
"sensor.my_plug_total_consumption": 30, "sensor.my_plug_total_consumption": 30.005,
"sensor.my_plug_today_s_consumption": 0.0, "sensor.my_plug_today_s_consumption": 0.0,
"sensor.my_plug_voltage": 121, "sensor.my_plug_voltage": 121.2,
"sensor.my_plug_current": 5, "sensor.my_plug_current": 5.04,
} }
entity_id = "switch.my_plug" entity_id = "switch.my_plug"
state = hass.states.get(entity_id) state = hass.states.get(entity_id)