1
mirror of https://github.com/home-assistant/core synced 2024-07-18 12:02:20 +02:00

Add entity_service calibrate_meter to utility_meter (#32658)

* add calibrate service
This commit is contained in:
Diogo Gomes 2020-03-11 08:42:22 +00:00 committed by GitHub
parent 69b19d54e8
commit 16336bf902
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 0 deletions

View File

@ -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"

View File

@ -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()

View File

@ -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'

View File

@ -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."""