From 5f87c7082d7ef7ed4d4d4aa460c6fddbc84cd26c Mon Sep 17 00:00:00 2001 From: David McClosky Date: Sun, 16 Mar 2025 11:10:29 -0400 Subject: [PATCH 1/2] Add basic get/set schedule actions This is primarily intended for demonstration/discussion purposes. --- custom_components/kumo/climate.py | 53 ++++++++++++++++++++- custom_components/kumo/services.yaml | 16 +++++++ custom_components/kumo/translations/en.json | 16 +++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 custom_components/kumo/services.yaml diff --git a/custom_components/kumo/climate.py b/custom_components/kumo/climate.py index 670ac84..ab508de 100644 --- a/custom_components/kumo/climate.py +++ b/custom_components/kumo/climate.py @@ -15,7 +15,7 @@ except ImportError: from homeassistant.components.climate import ClimateDevice as ClimateEntity -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.components.climate.const import ( ATTR_HVAC_MODE, ATTR_TARGET_TEMP_HIGH, @@ -26,7 +26,10 @@ ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_BATTERY_LEVEL, ATTR_TEMPERATURE, UnitOfTemperature -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, SupportsResponse +from homeassistant.util.json import JsonObjectType + +from pykumo.schedule import ScheduleEvent from .const import KUMO_DATA, KUMO_DATA_COORDINATORS @@ -50,6 +53,20 @@ } ) +SCHEDULE_SETTINGS_SCHEMA = vol.Schema({ + "mode": cv.string, + "vaneDir": cv.string, + "fanSpeed": cv.string, +}, extra=vol.ALLOW_EXTRA) + +SCHEDULE_EVENT_SCHEMA = { + "active": cv.boolean, + "inUse": cv.boolean, + "day": cv.string, + "time": cv.string, + "settings": SCHEDULE_SETTINGS_SCHEMA, +} + KUMO_STATE_AUTO = "auto" KUMO_STATE_AUTO_COOL = "autoCool" KUMO_STATE_AUTO_HEAT = "autoHeat" @@ -106,6 +123,24 @@ async def async_setup_entry( raise ConfigEntryNotReady("Kumo integration found no indoor units") async_add_entities(entities, True) + platform = entity_platform.async_get_current_platform() + platform.async_register_entity_service( + name="get_hvac_schedule", + schema=None, + func="get_hvac_schedule", + supports_response=SupportsResponse.ONLY, + ) + platform.async_register_entity_service( + name="set_hvac_schedule", + schema={ + vol.Required("entity_id"): str, + # TODO: This could be a lot tighter... + vol.Required("schedule"): vol.Schema({}, extra=vol.ALLOW_EXTRA), + }, + func="set_hvac_schedule", + supports_response=SupportsResponse.ONLY, + ) + class KumoThermostat(CoordinatedKumoEntity, ClimateEntity): """Representation of a Kumo Thermostat device.""" @@ -534,3 +569,17 @@ def set_fan_mode(self, fan_mode): def turn_off(self): """Turn the climate off. This implements https://www.home-assistant.io/integrations/climate/#action-climateturn_off.""" self.set_hvac_mode(HVACMode.OFF, caller="turn_off") + + def get_hvac_schedule(self) -> JsonObjectType: + """Fetch the schedule for this entity as a JSON-encodable dictionary.""" + unit_schedule = self._pykumo.get_unit_schedule() + all_events = unit_schedule.to_json_dict(slots=unit_schedule.keys()) + return all_events.get("events", {}) + + def set_hvac_schedule(self, schedule: JsonObjectType): + """Set the schedule for this entity from a JSON-encodable dictionary.""" + unit_schedule = self._pykumo.get_unit_schedule() + for slot, schedule_event_json in schedule.items(): + schedule_event = ScheduleEvent.from_json(schedule_event_json) + unit_schedule[slot] = schedule_event + unit_schedule.push() diff --git a/custom_components/kumo/services.yaml b/custom_components/kumo/services.yaml new file mode 100644 index 0000000..9b7ebec --- /dev/null +++ b/custom_components/kumo/services.yaml @@ -0,0 +1,16 @@ +get_hvac_schedule: + description: Get HVAC schedule + target: + entity: + domain: "climate" + integration: "kumo" + +set_hvac_schedule: + description: Set HVAC schedule + target: + entity: + domain: "climate" + integration: "kumo" + fields: + schedule: + required: true diff --git a/custom_components/kumo/translations/en.json b/custom_components/kumo/translations/en.json index 7ec546c..8e0b803 100755 --- a/custom_components/kumo/translations/en.json +++ b/custom_components/kumo/translations/en.json @@ -47,5 +47,21 @@ } } } + }, + "services": { + "get_hvac_schedule": { + "name": "Get HVAC schedule (advanced)", + "description": "Fetch the HVAC schedule for this unit. This is intended for advanced usage in scripts only." + }, + "set_hvac_schedule": { + "name": "Set HVAC schedule (advanced)", + "description": "Set the HVAC schedule for this unit. This is intended for advanced usage in scripts only.", + "fields": { + "schedule": { + "name": "Schedule", + "description": "Schedule (in a JSON structure) to set." + } + } + } } } From 4afd920891235d52db5ad4637d4b853803afcf9a Mon Sep 17 00:00:00 2001 From: David McClosky Date: Sun, 16 Mar 2025 11:17:06 -0400 Subject: [PATCH 2/2] Remove currently unused schema structures --- custom_components/kumo/climate.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/custom_components/kumo/climate.py b/custom_components/kumo/climate.py index ab508de..e0b07bf 100644 --- a/custom_components/kumo/climate.py +++ b/custom_components/kumo/climate.py @@ -53,20 +53,6 @@ } ) -SCHEDULE_SETTINGS_SCHEMA = vol.Schema({ - "mode": cv.string, - "vaneDir": cv.string, - "fanSpeed": cv.string, -}, extra=vol.ALLOW_EXTRA) - -SCHEDULE_EVENT_SCHEMA = { - "active": cv.boolean, - "inUse": cv.boolean, - "day": cv.string, - "time": cv.string, - "settings": SCHEDULE_SETTINGS_SCHEMA, -} - KUMO_STATE_AUTO = "auto" KUMO_STATE_AUTO_COOL = "autoCool" KUMO_STATE_AUTO_HEAT = "autoHeat"