diff --git a/custom_components/kumo/climate.py b/custom_components/kumo/climate.py index 670ac84..e0b07bf 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 @@ -106,6 +109,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 +555,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." + } + } + } } }