From c37100299a6ffb0aacba84ded20aaf14281a1941 Mon Sep 17 00:00:00 2001 From: thecode Date: Sat, 9 May 2020 13:40:01 +0300 Subject: [PATCH] Add service to turn on the switcher device with timer (#33235) * Update services.yaml * Update __init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/switcher_kis/__init__.py Co-authored-by: Martin Hjelmare * Update __init__.py * Update services.yaml Co-authored-by: Martin Hjelmare --- .../components/switcher_kis/__init__.py | 40 ++++++++++++++++--- .../components/switcher_kis/services.yaml | 10 +++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/switcher_kis/__init__.py b/homeassistant/components/switcher_kis/__init__.py index 0545687b003..8369fdd8975 100644 --- a/homeassistant/components/switcher_kis/__init__.py +++ b/homeassistant/components/switcher_kis/__init__.py @@ -7,11 +7,12 @@ from typing import Dict, Optional from aioswitcher.api import SwitcherV2Api from aioswitcher.bridge import SwitcherV2Bridge +from aioswitcher.consts import COMMAND_ON import voluptuous as vol from homeassistant.auth.permissions.const import POLICY_EDIT from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN -from homeassistant.const import CONF_ENTITY_ID, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_STOP from homeassistant.core import callback, split_entity_id from homeassistant.exceptions import Unauthorized, UnknownUser from homeassistant.helpers import config_validation as cv @@ -32,6 +33,7 @@ _LOGGER = getLogger(__name__) DOMAIN = "switcher_kis" CONF_AUTO_OFF = "auto_off" +CONF_TIMER_MINUTES = "timer_minutes" CONF_DEVICE_ID = "device_id" CONF_DEVICE_PASSWORD = "device_password" CONF_PHONE_ID = "phone_id" @@ -60,11 +62,21 @@ CONFIG_SCHEMA = vol.Schema( SERVICE_SET_AUTO_OFF_NAME = "set_auto_off" SERVICE_SET_AUTO_OFF_SCHEMA = vol.Schema( { - vol.Required(CONF_ENTITY_ID): cv.entity_id, + vol.Required(ATTR_ENTITY_ID): cv.entity_id, vol.Required(CONF_AUTO_OFF): cv.time_period_str, } ) +SERVICE_TURN_ON_WITH_TIMER_NAME = "turn_on_with_timer" +SERVICE_TURN_ON_WITH_TIMER_SCHEMA = vol.Schema( + { + vol.Required(ATTR_ENTITY_ID): cv.entity_id, + vol.Required(CONF_TIMER_MINUTES): vol.All( + cv.positive_int, vol.Range(min=1, max=90) + ), + } +) + @bind_hass async def _validate_edit_permission( @@ -76,13 +88,11 @@ async def _validate_edit_permission( raise Unauthorized( context=context, entity_id=entity_id, permission=(POLICY_EDIT,) ) - user = await hass.auth.async_get_user(context.user_id) if user is None: raise UnknownUser( context=context, entity_id=entity_id, permission=(POLICY_EDIT,) ) - if not user.permissions.check_entity(entity_id, POLICY_EDIT): raise Unauthorized( context=context, entity_id=entity_id, permission=(POLICY_EDIT,) @@ -112,7 +122,6 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: _LOGGER.exception("Failed to get response from device") await v2bridge.stop() return False - hass.data[DOMAIN] = {DATA_DEVICE: device_data} async def async_switch_platform_discovered( @@ -126,7 +135,7 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: """Use for handling setting device auto-off service calls.""" await _validate_edit_permission( - hass, service.context, service.data[CONF_ENTITY_ID] + hass, service.context, service.data[ATTR_ENTITY_ID] ) async with SwitcherV2Api( @@ -134,6 +143,18 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: ) as swapi: await swapi.set_auto_shutdown(service.data[CONF_AUTO_OFF]) + async def async_turn_on_with_timer_service(service: ServiceCallType) -> None: + """Use for handling turning device on with a timer service calls.""" + + await _validate_edit_permission( + hass, service.context, service.data[ATTR_ENTITY_ID] + ) + + async with SwitcherV2Api( + hass.loop, device_data.ip_addr, phone_id, device_id, device_password + ) as swapi: + await swapi.control_device(COMMAND_ON, service.data[CONF_TIMER_MINUTES]) + hass.services.async_register( DOMAIN, SERVICE_SET_AUTO_OFF_NAME, @@ -141,6 +162,13 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: schema=SERVICE_SET_AUTO_OFF_SCHEMA, ) + hass.services.async_register( + DOMAIN, + SERVICE_TURN_ON_WITH_TIMER_NAME, + async_turn_on_with_timer_service, + schema=SERVICE_TURN_ON_WITH_TIMER_SCHEMA, + ) + async_listen_platform(hass, SWITCH_DOMAIN, async_switch_platform_discovered) hass.async_create_task(async_load_platform(hass, SWITCH_DOMAIN, DOMAIN, {}, config)) diff --git a/homeassistant/components/switcher_kis/services.yaml b/homeassistant/components/switcher_kis/services.yaml index 39691752445..07e0cfe1198 100644 --- a/homeassistant/components/switcher_kis/services.yaml +++ b/homeassistant/components/switcher_kis/services.yaml @@ -7,3 +7,13 @@ set_auto_off: auto_off: description: "Time period string containing hours and minutes." example: '"02:30"' + +turn_on_with_timer: + description: 'Turn on the Switcher device with timer.' + fields: + entity_id: + description: "Name of the entity id associated with the integration, used for permission validation." + example: "switch.switcher_kis_boiler" + timer_minutes: + description: 'Minutes to turn on (valid range from 1 to 90)' + example: '30'