From da713e206df394ef958bbf236f50b0c0bd1829c2 Mon Sep 17 00:00:00 2001 From: GeoffAtHome Date: Fri, 29 Jan 2021 11:44:56 +0000 Subject: [PATCH] Add override duration for genius hub switches (#45558) --- .../components/geniushub/services.yaml | 12 +++++++ homeassistant/components/geniushub/switch.py | 31 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/geniushub/services.yaml b/homeassistant/components/geniushub/services.yaml index 50cd8d7d01e..fa46c1d4c09 100644 --- a/homeassistant/components/geniushub/services.yaml +++ b/homeassistant/components/geniushub/services.yaml @@ -26,3 +26,15 @@ set_zone_override: description: >- The duration of the override. Optional, default 1 hour, maximum 24 hours. example: '{"minutes": 135}' + +set_switch_override: + description: >- + Override switch for a given duration. + fields: + entity_id: + description: The zone's entity_id. + example: switch.study + duration: + description: >- + The duration of the override. Optional, default 1 hour, maximum 24 hours. + example: '{"minutes": 135}' diff --git a/homeassistant/components/geniushub/switch.py b/homeassistant/components/geniushub/switch.py index e73468321bd..cb45911d250 100644 --- a/homeassistant/components/geniushub/switch.py +++ b/homeassistant/components/geniushub/switch.py @@ -1,13 +1,29 @@ """Support for Genius Hub switch/outlet devices.""" +from datetime import timedelta + +import voluptuous as vol + from homeassistant.components.switch import DEVICE_CLASS_OUTLET, SwitchEntity +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.typing import ConfigType, HomeAssistantType -from . import DOMAIN, GeniusZone - -ATTR_DURATION = "duration" +from . import ATTR_DURATION, DOMAIN, GeniusZone GH_ON_OFF_ZONE = "on / off" +SVC_SET_SWITCH_OVERRIDE = "set_switch_override" + +SET_SWITCH_OVERRIDE_SCHEMA = vol.Schema( + { + vol.Required(ATTR_ENTITY_ID): cv.entity_id, + vol.Optional(ATTR_DURATION): vol.All( + cv.time_period, + vol.Range(min=timedelta(minutes=5), max=timedelta(days=1)), + ), + } +) + async def async_setup_platform( hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None @@ -26,6 +42,15 @@ async def async_setup_platform( ] ) + # Register custom services + platform = entity_platform.current_platform.get() + + platform.async_register_entity_service( + SVC_SET_SWITCH_OVERRIDE, + SET_SWITCH_OVERRIDE_SCHEMA, + "async_turn_on", + ) + class GeniusSwitch(GeniusZone, SwitchEntity): """Representation of a Genius Hub switch."""