Allow put as method (#8004)

pull/8020/head
Fabian Affolter 2017-06-13 07:27:25 +02:00 committed by Paulus Schoutsen
parent 2bf781185f
commit 843f8ce9ee
2 changed files with 24 additions and 14 deletions

View File

@ -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)

View File

@ -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."""