mirror of
https://github.com/home-assistant/core
synced 2024-09-03 08:14:07 +02:00
Add entity_service calibrate_meter to utility_meter (#32658)
* add calibrate service
This commit is contained in:
parent
69b19d54e8
commit
16336bf902
@ -23,6 +23,7 @@ CONF_TARIFF = "tariff"
|
|||||||
CONF_TARIFF_ENTITY = "tariff_entity"
|
CONF_TARIFF_ENTITY = "tariff_entity"
|
||||||
|
|
||||||
ATTR_TARIFF = "tariff"
|
ATTR_TARIFF = "tariff"
|
||||||
|
ATTR_VALUE = "value"
|
||||||
|
|
||||||
SIGNAL_START_PAUSE_METER = "utility_meter_start_pause"
|
SIGNAL_START_PAUSE_METER = "utility_meter_start_pause"
|
||||||
SIGNAL_RESET_METER = "utility_meter_reset"
|
SIGNAL_RESET_METER = "utility_meter_reset"
|
||||||
@ -30,3 +31,4 @@ SIGNAL_RESET_METER = "utility_meter_reset"
|
|||||||
SERVICE_RESET = "reset"
|
SERVICE_RESET = "reset"
|
||||||
SERVICE_SELECT_TARIFF = "select_tariff"
|
SERVICE_SELECT_TARIFF = "select_tariff"
|
||||||
SERVICE_SELECT_NEXT_TARIFF = "next_tariff"
|
SERVICE_SELECT_NEXT_TARIFF = "next_tariff"
|
||||||
|
SERVICE_CALIBRATE_METER = "calibrate"
|
||||||
|
@ -3,6 +3,8 @@ from datetime import date, timedelta
|
|||||||
from decimal import Decimal, DecimalException
|
from decimal import Decimal, DecimalException
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
@ -11,6 +13,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.event import (
|
from homeassistant.helpers.event import (
|
||||||
async_track_state_change,
|
async_track_state_change,
|
||||||
@ -20,6 +23,7 @@ from homeassistant.helpers.restore_state import RestoreEntity
|
|||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
ATTR_VALUE,
|
||||||
CONF_METER,
|
CONF_METER,
|
||||||
CONF_METER_NET_CONSUMPTION,
|
CONF_METER_NET_CONSUMPTION,
|
||||||
CONF_METER_OFFSET,
|
CONF_METER_OFFSET,
|
||||||
@ -32,6 +36,7 @@ from .const import (
|
|||||||
HOURLY,
|
HOURLY,
|
||||||
MONTHLY,
|
MONTHLY,
|
||||||
QUARTERLY,
|
QUARTERLY,
|
||||||
|
SERVICE_CALIBRATE_METER,
|
||||||
SIGNAL_RESET_METER,
|
SIGNAL_RESET_METER,
|
||||||
WEEKLY,
|
WEEKLY,
|
||||||
YEARLY,
|
YEARLY,
|
||||||
@ -86,6 +91,14 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
|
|
||||||
async_add_entities(meters)
|
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):
|
class UtilityMeterSensor(RestoreEntity):
|
||||||
"""Representation of an utility meter sensor."""
|
"""Representation of an utility meter sensor."""
|
||||||
@ -206,6 +219,12 @@ class UtilityMeterSensor(RestoreEntity):
|
|||||||
self._state = 0
|
self._state = 0
|
||||||
await self.async_update_ha_state()
|
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):
|
async def async_added_to_hass(self):
|
||||||
"""Handle entity which will be added."""
|
"""Handle entity which will be added."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
|
@ -23,3 +23,13 @@ select_tariff:
|
|||||||
tariff:
|
tariff:
|
||||||
description: Name of the tariff to switch to
|
description: Name of the tariff to switch to
|
||||||
example: 'offpeak'
|
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'
|
@ -7,7 +7,9 @@ from unittest.mock import patch
|
|||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
from homeassistant.components.utility_meter.const import (
|
from homeassistant.components.utility_meter.const import (
|
||||||
ATTR_TARIFF,
|
ATTR_TARIFF,
|
||||||
|
ATTR_VALUE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
SERVICE_CALIBRATE_METER,
|
||||||
SERVICE_SELECT_TARIFF,
|
SERVICE_SELECT_TARIFF,
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START
|
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 is not None
|
||||||
assert state.state == "3"
|
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):
|
async def test_net_consumption(hass):
|
||||||
"""Test utility sensor state."""
|
"""Test utility sensor state."""
|
||||||
|
Loading…
Reference in New Issue
Block a user