2015-10-23 21:40:14 +00:00
|
|
|
"""
|
2016-03-08 12:35:39 +00:00
|
|
|
Support for RESTful switches.
|
2015-10-23 21:40:14 +00:00
|
|
|
|
2015-10-25 21:21:25 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/switch.rest/
|
2015-10-23 21:40:14 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2015-10-23 21:40:14 +00:00
|
|
|
import requests
|
2016-08-20 23:28:45 +00:00
|
|
|
import voluptuous as vol
|
2015-10-23 21:40:14 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
2016-10-28 05:34:22 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME, CONF_RESOURCE, CONF_TIMEOUT)
|
2016-08-20 23:28:45 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-10-23 21:40:14 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
CONF_BODY_OFF = 'body_off'
|
|
|
|
CONF_BODY_ON = 'body_on'
|
|
|
|
DEFAULT_BODY_OFF = 'OFF'
|
|
|
|
DEFAULT_BODY_ON = 'ON'
|
|
|
|
DEFAULT_NAME = 'REST Switch'
|
2016-10-28 05:34:22 +00:00
|
|
|
DEFAULT_TIMEOUT = 10
|
|
|
|
CONF_IS_ON_TEMPLATE = 'is_on_template'
|
2016-08-20 23:28:45 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_RESOURCE): cv.url,
|
2016-10-28 05:34:22 +00:00
|
|
|
vol.Optional(CONF_BODY_OFF, default=DEFAULT_BODY_OFF): cv.template,
|
|
|
|
vol.Optional(CONF_BODY_ON, default=DEFAULT_BODY_ON): cv.template,
|
2016-08-20 23:28:45 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-10-28 05:34:22 +00:00
|
|
|
vol.Optional(CONF_IS_ON_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
2016-08-20 23:28:45 +00:00
|
|
|
})
|
2015-10-23 21:40:14 +00:00
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-10-23 21:40:14 +00:00
|
|
|
|
|
|
|
|
2015-12-19 09:06:54 +00:00
|
|
|
# pylint: disable=unused-argument,
|
2015-10-23 21:40:14 +00:00
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2016-08-20 23:28:45 +00:00
|
|
|
"""Setup the RESTful switch."""
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
resource = config.get(CONF_RESOURCE)
|
|
|
|
body_on = config.get(CONF_BODY_ON)
|
|
|
|
body_off = config.get(CONF_BODY_OFF)
|
2016-10-28 05:34:22 +00:00
|
|
|
is_on_template = config.get(CONF_IS_ON_TEMPLATE)
|
|
|
|
|
|
|
|
if is_on_template is not None:
|
|
|
|
is_on_template.hass = hass
|
|
|
|
if body_on is not None:
|
|
|
|
body_on.hass = hass
|
|
|
|
if body_off is not None:
|
|
|
|
body_off.hass = hass
|
|
|
|
timeout = config.get(CONF_TIMEOUT)
|
2015-10-23 21:40:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
requests.get(resource, timeout=10)
|
|
|
|
except requests.exceptions.MissingSchema:
|
|
|
|
_LOGGER.error("Missing resource or schema in configuration. "
|
2015-12-19 09:06:54 +00:00
|
|
|
"Add http:// or https:// to your URL")
|
2015-10-23 21:40:14 +00:00
|
|
|
return False
|
|
|
|
except requests.exceptions.ConnectionError:
|
2015-12-19 09:06:54 +00:00
|
|
|
_LOGGER.error("No route to resource/endpoint: %s", resource)
|
2015-10-23 21:40:14 +00:00
|
|
|
return False
|
|
|
|
|
2016-10-28 05:34:22 +00:00
|
|
|
add_devices_callback(
|
|
|
|
[RestSwitch(hass, name, resource,
|
|
|
|
body_on, body_off, is_on_template, timeout)])
|
2015-10-23 21:40:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
class RestSwitch(SwitchDevice):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a switch that can be toggled using REST."""
|
|
|
|
|
2016-10-28 05:34:22 +00:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
def __init__(self, hass, name, resource, body_on, body_off,
|
|
|
|
is_on_template, timeout):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the REST switch."""
|
2015-10-23 21:40:14 +00:00
|
|
|
self._state = None
|
|
|
|
self._hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._resource = resource
|
|
|
|
self._body_on = body_on
|
|
|
|
self._body_off = body_off
|
2016-10-28 05:34:22 +00:00
|
|
|
self._is_on_template = is_on_template
|
|
|
|
self._timeout = timeout
|
2015-10-23 21:40:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""The name of the switch."""
|
2015-10-23 21:40:14 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-10 07:34:38 +00:00
|
|
|
"""Return true if device is on."""
|
2015-10-23 21:40:14 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the device on."""
|
2016-10-28 05:34:22 +00:00
|
|
|
body_on_t = self._body_on.render()
|
2015-10-23 21:40:14 +00:00
|
|
|
request = requests.post(self._resource,
|
2016-10-28 05:34:22 +00:00
|
|
|
data=body_on_t,
|
|
|
|
timeout=self._timeout)
|
2015-10-23 21:40:14 +00:00
|
|
|
if request.status_code == 200:
|
|
|
|
self._state = True
|
|
|
|
else:
|
2015-10-25 21:21:25 +00:00
|
|
|
_LOGGER.error("Can't turn on %s. Is resource/endpoint offline?",
|
2015-10-23 21:40:14 +00:00
|
|
|
self._resource)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the device off."""
|
2016-10-28 05:34:22 +00:00
|
|
|
body_off_t = self._body_off.render()
|
2015-10-23 21:40:14 +00:00
|
|
|
request = requests.post(self._resource,
|
2016-10-28 05:34:22 +00:00
|
|
|
data=body_off_t,
|
|
|
|
timeout=self._timeout)
|
2015-10-23 21:40:14 +00:00
|
|
|
if request.status_code == 200:
|
|
|
|
self._state = False
|
|
|
|
else:
|
2015-10-25 21:21:25 +00:00
|
|
|
_LOGGER.error("Can't turn off %s. Is resource/endpoint offline?",
|
2015-10-23 21:40:14 +00:00
|
|
|
self._resource)
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Get the latest data from REST API and update the state."""
|
2016-10-28 05:34:22 +00:00
|
|
|
request = requests.get(self._resource, timeout=self._timeout)
|
|
|
|
|
|
|
|
if self._is_on_template is not None:
|
|
|
|
response = self._is_on_template.render_with_possible_json_value(
|
|
|
|
request.text, 'None')
|
|
|
|
response = response.lower()
|
|
|
|
if response == 'true':
|
|
|
|
self._state = True
|
|
|
|
elif response == 'false':
|
|
|
|
self._state = False
|
|
|
|
else:
|
|
|
|
self._state = None
|
2015-10-23 21:40:14 +00:00
|
|
|
else:
|
2016-10-28 05:34:22 +00:00
|
|
|
if request.text == self._body_on.template:
|
|
|
|
self._state = True
|
|
|
|
elif request.text == self._body_off.template:
|
|
|
|
self._state = False
|
|
|
|
else:
|
|
|
|
self._state = None
|