diff --git a/homeassistant/components/utility_meter/const.py b/homeassistant/components/utility_meter/const.py index 23d39204f9c3..5be7dcf9b697 100644 --- a/homeassistant/components/utility_meter/const.py +++ b/homeassistant/components/utility_meter/const.py @@ -23,6 +23,7 @@ CONF_TARIFF = "tariff" CONF_TARIFF_ENTITY = "tariff_entity" ATTR_TARIFF = "tariff" +ATTR_VALUE = "value" SIGNAL_START_PAUSE_METER = "utility_meter_start_pause" SIGNAL_RESET_METER = "utility_meter_reset" @@ -30,3 +31,4 @@ SIGNAL_RESET_METER = "utility_meter_reset" SERVICE_RESET = "reset" SERVICE_SELECT_TARIFF = "select_tariff" SERVICE_SELECT_NEXT_TARIFF = "next_tariff" +SERVICE_CALIBRATE_METER = "calibrate" diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index 8c47e716b805..ad82cd9e79f5 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -3,6 +3,8 @@ from datetime import date, timedelta from decimal import Decimal, DecimalException import logging +import voluptuous as vol + from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, @@ -11,6 +13,7 @@ from homeassistant.const import ( STATE_UNKNOWN, ) from homeassistant.core import callback +from homeassistant.helpers import entity_platform from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.event import ( async_track_state_change, @@ -20,6 +23,7 @@ from homeassistant.helpers.restore_state import RestoreEntity import homeassistant.util.dt as dt_util from .const import ( + ATTR_VALUE, CONF_METER, CONF_METER_NET_CONSUMPTION, CONF_METER_OFFSET, @@ -32,6 +36,7 @@ from .const import ( HOURLY, MONTHLY, QUARTERLY, + SERVICE_CALIBRATE_METER, SIGNAL_RESET_METER, WEEKLY, YEARLY, @@ -86,6 +91,14 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= async_add_entities(meters) + platform = entity_platform.current_platform.get() + + platform.async_register_entity_service( + SERVICE_CALIBRATE_METER, + {vol.Required(ATTR_VALUE): vol.Coerce(float)}, + "async_calibrate", + ) + class UtilityMeterSensor(RestoreEntity): """Representation of an utility meter sensor.""" @@ -206,6 +219,12 @@ class UtilityMeterSensor(RestoreEntity): self._state = 0 await self.async_update_ha_state() + 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.async_write_ha_state() + async def async_added_to_hass(self): """Handle entity which will be added.""" await super().async_added_to_hass() diff --git a/homeassistant/components/utility_meter/services.yaml b/homeassistant/components/utility_meter/services.yaml index 5437f4b83a69..42522f81876f 100644 --- a/homeassistant/components/utility_meter/services.yaml +++ b/homeassistant/components/utility_meter/services.yaml @@ -23,3 +23,13 @@ select_tariff: tariff: description: Name of the tariff to switch to example: 'offpeak' + +calibrate: + description: calibrates an utility meter. + fields: + entity_id: + description: Name of the entity to calibrate + example: 'utility_meter.energy' + value: + description: Value to which set the meter + example: '100' \ No newline at end of file diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index fcfe97804e41..19742d74f213 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -7,7 +7,9 @@ from unittest.mock import patch from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.utility_meter.const import ( ATTR_TARIFF, + ATTR_VALUE, DOMAIN, + SERVICE_CALIBRATE_METER, SERVICE_SELECT_TARIFF, ) from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START @@ -96,6 +98,17 @@ async def test_state(hass): assert state is not None assert state.state == "3" + await hass.services.async_call( + DOMAIN, + SERVICE_CALIBRATE_METER, + {ATTR_ENTITY_ID: "sensor.energy_bill_midpeak", ATTR_VALUE: "100"}, + blocking=True, + ) + await hass.async_block_till_done() + state = hass.states.get("sensor.energy_bill_midpeak") + assert state is not None + assert state.state == "100" + async def test_net_consumption(hass): """Test utility sensor state."""