From 843f8ce9eeee4f37827d575ec659a80a3a068023 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 13 Jun 2017 07:27:25 +0200 Subject: [PATCH] Allow put as method (#8004) --- homeassistant/components/switch/rest.py | 32 +++++++++++++++---------- tests/components/switch/test_rest.py | 6 +++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/switch/rest.py b/homeassistant/components/switch/rest.py index 419f4028def..31d4f0f3e06 100644 --- a/homeassistant/components/switch/rest.py +++ b/homeassistant/components/switch/rest.py @@ -12,41 +12,48 @@ import async_timeout import voluptuous as vol from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) -from homeassistant.const import (CONF_NAME, CONF_RESOURCE, CONF_TIMEOUT) +from homeassistant.const import ( + CONF_NAME, CONF_RESOURCE, CONF_TIMEOUT, CONF_METHOD) from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from homeassistant.helpers.template import Template +_LOGGER = logging.getLogger(__name__) + CONF_BODY_OFF = 'body_off' CONF_BODY_ON = 'body_on' CONF_IS_ON_TEMPLATE = 'is_on_template' +DEFAULT_METHOD = 'post' DEFAULT_BODY_OFF = Template('OFF') DEFAULT_BODY_ON = Template('ON') DEFAULT_NAME = 'REST Switch' DEFAULT_TIMEOUT = 10 +SUPPORT_REST_METHODS = ['post', 'put'] + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_RESOURCE): cv.url, vol.Optional(CONF_BODY_OFF, default=DEFAULT_BODY_OFF): cv.template, vol.Optional(CONF_BODY_ON, default=DEFAULT_BODY_ON): cv.template, - vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_IS_ON_TEMPLATE): cv.template, + vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): + vol.All(vol.Lower, vol.In(SUPPORT_REST_METHODS)), + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, }) -_LOGGER = logging.getLogger(__name__) - -# pylint: disable=unused-argument, +# pylint: disable=unused-argument @asyncio.coroutine def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up the RESTful switch.""" + body_off = config.get(CONF_BODY_OFF) + body_on = config.get(CONF_BODY_ON) + is_on_template = config.get(CONF_IS_ON_TEMPLATE) + method = config.get(CONF_METHOD) name = config.get(CONF_NAME) resource = config.get(CONF_RESOURCE) - body_on = config.get(CONF_BODY_ON) - body_off = config.get(CONF_BODY_OFF) - is_on_template = config.get(CONF_IS_ON_TEMPLATE) websession = async_get_clientsession(hass) if is_on_template is not None: @@ -74,20 +81,21 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): return False async_add_devices( - [RestSwitch(hass, name, resource, body_on, body_off, + [RestSwitch(hass, name, resource, method, body_on, body_off, is_on_template, timeout)]) class RestSwitch(SwitchDevice): """Representation of a switch that can be toggled using REST.""" - def __init__(self, hass, name, resource, body_on, body_off, + def __init__(self, hass, name, resource, method, body_on, body_off, is_on_template, timeout): """Initialize the REST switch.""" self._state = None self.hass = hass self._name = name self._resource = resource + self._method = method self._body_on = body_on self._body_off = body_off self._is_on_template = is_on_template @@ -111,7 +119,7 @@ class RestSwitch(SwitchDevice): try: with async_timeout.timeout(self._timeout, loop=self.hass.loop): - request = yield from websession.post( + request = yield from getattr(websession, self._method)( self._resource, data=bytes(body_on_t, 'utf-8')) except (asyncio.TimeoutError, aiohttp.ClientError): _LOGGER.error("Error while turn on %s", self._resource) @@ -131,7 +139,7 @@ class RestSwitch(SwitchDevice): try: with async_timeout.timeout(self._timeout, loop=self.hass.loop): - request = yield from websession.post( + request = yield from getattr(websession, self._method)( self._resource, data=bytes(body_off_t, 'utf-8')) except (asyncio.TimeoutError, aiohttp.ClientError): _LOGGER.error("Error while turn off %s", self._resource) diff --git a/tests/components/switch/test_rest.py b/tests/components/switch/test_rest.py index 5a9ce679edf..97911fccbfd 100644 --- a/tests/components/switch/test_rest.py +++ b/tests/components/switch/test_rest.py @@ -97,11 +97,13 @@ class TestRestSwitch: """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.name = 'foo' + self.method = 'post' self.resource = 'http://localhost/' self.body_on = Template('on', self.hass) self.body_off = Template('off', self.hass) - self.switch = rest.RestSwitch(self.hass, self.name, self.resource, - self.body_on, self.body_off, None, 10) + self.switch = rest.RestSwitch( + self.hass, self.name, self.resource, self.method, self.body_on, + self.body_off, None, 10) def teardown_method(self): """Stop everything that was started."""