mirror of
https://github.com/home-assistant/core.git
synced 2026-10-07 06:50:41 -04:00
Move utility_meter calibrate registration to services module (#184306)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,11 +5,9 @@ from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from decimal import Decimal, DecimalException, InvalidOperation
|
||||
import logging
|
||||
import math
|
||||
from typing import Any, Self, override
|
||||
|
||||
from cronsim import CronSim
|
||||
import probatio
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
ATTR_LAST_RESET,
|
||||
@@ -40,7 +38,7 @@ from homeassistant.core import (
|
||||
State,
|
||||
callback,
|
||||
)
|
||||
from homeassistant.helpers import entity_platform, entity_registry as er
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.device import async_entity_id_to_device
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import (
|
||||
@@ -59,7 +57,6 @@ from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from .const import (
|
||||
ATTR_NEXT_RESET,
|
||||
ATTR_VALUE,
|
||||
BIMONTHLY,
|
||||
CONF_CRON_PATTERN,
|
||||
CONF_METER,
|
||||
@@ -81,7 +78,6 @@ from .const import (
|
||||
MONTHLY,
|
||||
QUARTER_HOURLY,
|
||||
QUARTERLY,
|
||||
SERVICE_CALIBRATE_METER,
|
||||
SIGNAL_RESET_METER,
|
||||
WEEKLY,
|
||||
YEARLY,
|
||||
@@ -112,16 +108,6 @@ PAUSED = "paused"
|
||||
COLLECTING = "collecting"
|
||||
|
||||
|
||||
def validate_is_number(value):
|
||||
"""Validate value is a number."""
|
||||
try:
|
||||
if math.isfinite(float(value)):
|
||||
return value
|
||||
except ValueError, TypeError:
|
||||
pass
|
||||
raise probatio.Invalid("Value is not a number")
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
@@ -197,14 +183,6 @@ async def async_setup_entry(
|
||||
|
||||
async_add_entities(meters)
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_CALIBRATE_METER,
|
||||
{probatio.Required(ATTR_VALUE): validate_is_number},
|
||||
"async_calibrate",
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
@@ -279,14 +257,6 @@ async def async_setup_platform(
|
||||
|
||||
async_add_entities(meters)
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_CALIBRATE_METER,
|
||||
{probatio.Required(ATTR_VALUE): validate_is_number},
|
||||
"async_calibrate",
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class UtilitySensorExtraStoredData(SensorExtraStoredData):
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
"""Support for tracking consumption over given periods of time."""
|
||||
|
||||
import logging
|
||||
import math
|
||||
|
||||
import probatio
|
||||
|
||||
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback, split_entity_id
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.service import async_register_platform_entity_service
|
||||
|
||||
from .const import DOMAIN, SERVICE_RESET, SIGNAL_RESET_METER
|
||||
from .const import (
|
||||
ATTR_VALUE,
|
||||
DOMAIN,
|
||||
SERVICE_CALIBRATE_METER,
|
||||
SERVICE_RESET,
|
||||
SIGNAL_RESET_METER,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _validate_is_number(value):
|
||||
"""Validate value is a number."""
|
||||
try:
|
||||
if math.isfinite(float(value)):
|
||||
return value
|
||||
except ValueError, TypeError:
|
||||
pass
|
||||
raise probatio.Invalid("Value is not a number")
|
||||
|
||||
|
||||
async def async_reset_meters(service_call: ServiceCall) -> None:
|
||||
"""Reset all sensors of a meter."""
|
||||
meters = service_call.data["entity_id"]
|
||||
@@ -43,3 +61,12 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
{ATTR_ENTITY_ID: probatio.All(probatio.EnsureList(), [cv.entity_id])}
|
||||
),
|
||||
)
|
||||
|
||||
async_register_platform_entity_service(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SERVICE_CALIBRATE_METER,
|
||||
entity_domain=Platform.SENSOR,
|
||||
func="async_calibrate",
|
||||
schema={probatio.Required(ATTR_VALUE): _validate_is_number},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user