From 02f89ee377b4ee7add761fb8b2781ce1917a240b Mon Sep 17 00:00:00 2001 From: BottlecapDave Date: Sat, 27 Apr 2024 16:22:46 +0100 Subject: [PATCH] fix: Fixed registration of update target rate config service --- .../carbon_intensity/binary_sensor.py | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/custom_components/carbon_intensity/binary_sensor.py b/custom_components/carbon_intensity/binary_sensor.py index 8020fcf..ffebb46 100644 --- a/custom_components/carbon_intensity/binary_sensor.py +++ b/custom_components/carbon_intensity/binary_sensor.py @@ -1,16 +1,17 @@ from datetime import timedelta import logging -from .target_rates.target_rate import CarbonIntensityTargetRate +import voluptuous as vol + +from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.exceptions import ConfigEntryNotReady from .const import ( DOMAIN, - CONFIG_TARGET_NAME, - DATA_RATES_COORDINATOR ) +from .target_rates.target_rate import CarbonIntensityTargetRate _LOGGER = logging.getLogger(__name__) @@ -35,4 +36,25 @@ async def async_setup_target_sensors(hass, entry, async_add_entities): coordinator = hass.data[DOMAIN][DATA_RATES_COORDINATOR] - async_add_entities([CarbonIntensityTargetRate(coordinator, config)], True) + platform = entity_platform.async_get_current_platform() + platform.async_register_entity_service( + "update_target_config", + vol.All( + vol.Schema( + { + vol.Optional("target_hours"): str, + vol.Optional("target_start_time"): str, + vol.Optional("target_end_time"): str, + vol.Optional("target_offset"): str, + vol.Optional("target_maximum_rate"): str, + }, + extra=vol.ALLOW_EXTRA, + ), + cv.has_at_least_one_key( + "target_hours", "target_start_time", "target_end_time", "target_offset", "target_maximum_rate" + ), + ), + "async_update_config", + ) + + async_add_entities([CarbonIntensityTargetRate(hass, coordinator, config)], True)