mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Allow put as method (#8004)
This commit is contained in:
parent
2bf781185f
commit
843f8ce9ee
@ -12,41 +12,48 @@ import async_timeout
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
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
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.template import Template
|
from homeassistant.helpers.template import Template
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_BODY_OFF = 'body_off'
|
CONF_BODY_OFF = 'body_off'
|
||||||
CONF_BODY_ON = 'body_on'
|
CONF_BODY_ON = 'body_on'
|
||||||
CONF_IS_ON_TEMPLATE = 'is_on_template'
|
CONF_IS_ON_TEMPLATE = 'is_on_template'
|
||||||
|
|
||||||
|
DEFAULT_METHOD = 'post'
|
||||||
DEFAULT_BODY_OFF = Template('OFF')
|
DEFAULT_BODY_OFF = Template('OFF')
|
||||||
DEFAULT_BODY_ON = Template('ON')
|
DEFAULT_BODY_ON = Template('ON')
|
||||||
DEFAULT_NAME = 'REST Switch'
|
DEFAULT_NAME = 'REST Switch'
|
||||||
DEFAULT_TIMEOUT = 10
|
DEFAULT_TIMEOUT = 10
|
||||||
|
|
||||||
|
SUPPORT_REST_METHODS = ['post', 'put']
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_RESOURCE): cv.url,
|
vol.Required(CONF_RESOURCE): cv.url,
|
||||||
vol.Optional(CONF_BODY_OFF, default=DEFAULT_BODY_OFF): cv.template,
|
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_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_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,
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
||||||
})
|
})
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=unused-argument,
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the RESTful switch."""
|
"""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)
|
name = config.get(CONF_NAME)
|
||||||
resource = config.get(CONF_RESOURCE)
|
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)
|
websession = async_get_clientsession(hass)
|
||||||
|
|
||||||
if is_on_template is not None:
|
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
|
return False
|
||||||
|
|
||||||
async_add_devices(
|
async_add_devices(
|
||||||
[RestSwitch(hass, name, resource, body_on, body_off,
|
[RestSwitch(hass, name, resource, method, body_on, body_off,
|
||||||
is_on_template, timeout)])
|
is_on_template, timeout)])
|
||||||
|
|
||||||
|
|
||||||
class RestSwitch(SwitchDevice):
|
class RestSwitch(SwitchDevice):
|
||||||
"""Representation of a switch that can be toggled using REST."""
|
"""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):
|
is_on_template, timeout):
|
||||||
"""Initialize the REST switch."""
|
"""Initialize the REST switch."""
|
||||||
self._state = None
|
self._state = None
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
self._resource = resource
|
self._resource = resource
|
||||||
|
self._method = method
|
||||||
self._body_on = body_on
|
self._body_on = body_on
|
||||||
self._body_off = body_off
|
self._body_off = body_off
|
||||||
self._is_on_template = is_on_template
|
self._is_on_template = is_on_template
|
||||||
@ -111,7 +119,7 @@ class RestSwitch(SwitchDevice):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with async_timeout.timeout(self._timeout, loop=self.hass.loop):
|
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'))
|
self._resource, data=bytes(body_on_t, 'utf-8'))
|
||||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||||
_LOGGER.error("Error while turn on %s", self._resource)
|
_LOGGER.error("Error while turn on %s", self._resource)
|
||||||
@ -131,7 +139,7 @@ class RestSwitch(SwitchDevice):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with async_timeout.timeout(self._timeout, loop=self.hass.loop):
|
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'))
|
self._resource, data=bytes(body_off_t, 'utf-8'))
|
||||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||||
_LOGGER.error("Error while turn off %s", self._resource)
|
_LOGGER.error("Error while turn off %s", self._resource)
|
||||||
|
@ -97,11 +97,13 @@ class TestRestSwitch:
|
|||||||
"""Setup things to be run when tests are started."""
|
"""Setup things to be run when tests are started."""
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
self.name = 'foo'
|
self.name = 'foo'
|
||||||
|
self.method = 'post'
|
||||||
self.resource = 'http://localhost/'
|
self.resource = 'http://localhost/'
|
||||||
self.body_on = Template('on', self.hass)
|
self.body_on = Template('on', self.hass)
|
||||||
self.body_off = Template('off', self.hass)
|
self.body_off = Template('off', self.hass)
|
||||||
self.switch = rest.RestSwitch(self.hass, self.name, self.resource,
|
self.switch = rest.RestSwitch(
|
||||||
self.body_on, self.body_off, None, 10)
|
self.hass, self.name, self.resource, self.method, self.body_on,
|
||||||
|
self.body_off, None, 10)
|
||||||
|
|
||||||
def teardown_method(self):
|
def teardown_method(self):
|
||||||
"""Stop everything that was started."""
|
"""Stop everything that was started."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user