From 53c1aba7dfd804766dd2c284cac17f93bb3d893e Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Tue, 5 May 2020 01:44:00 +0100 Subject: [PATCH] Fix utility_meter calibration with float values (#35186) --- homeassistant/components/utility_meter/sensor.py | 4 ++-- tests/components/utility_meter/test_sensor.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index c891c698cf6..c5b6e9a292c 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -95,7 +95,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= platform.async_register_entity_service( SERVICE_CALIBRATE_METER, - {vol.Required(ATTR_VALUE): vol.Coerce(float)}, + {vol.Required(ATTR_VALUE): vol.Coerce(Decimal)}, "async_calibrate", ) @@ -222,7 +222,7 @@ class UtilityMeterSensor(RestoreEntity): async def async_calibrate(self, value): """Calibrate the Utility Meter with a given value.""" _LOGGER.debug("Calibrate %s = %s", self._name, value) - self._state = Decimal(value) + self._state = value self.async_write_ha_state() async def async_added_to_hass(self): diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index d7c888802ed..09145fc4e4e 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -119,6 +119,17 @@ async def test_state(hass): assert state is not None assert state.state == "100" + await hass.services.async_call( + DOMAIN, + SERVICE_CALIBRATE_METER, + {ATTR_ENTITY_ID: "sensor.energy_bill_midpeak", ATTR_VALUE: "0.123"}, + blocking=True, + ) + await hass.async_block_till_done() + state = hass.states.get("sensor.energy_bill_midpeak") + assert state is not None + assert state.state == "0.123" + async def test_net_consumption(hass): """Test utility sensor state."""