core/homeassistant/components/rest/switch.py

256 lines
7.9 KiB
Python
Raw Normal View History

"""Support for RESTful switches."""
import asyncio
import logging
2016-02-19 05:27:50 +00:00
import aiohttp
import async_timeout
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
2017-06-13 05:27:25 +00:00
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_HEADERS,
CONF_METHOD,
2019-07-31 19:25:30 +00:00
CONF_NAME,
CONF_PARAMS,
CONF_PASSWORD,
2019-07-31 19:25:30 +00:00
CONF_RESOURCE,
CONF_TIMEOUT,
CONF_USERNAME,
CONF_VERIFY_SSL,
2020-04-09 19:43:42 +00:00
HTTP_BAD_REQUEST,
HTTP_OK,
2019-07-31 19:25:30 +00:00
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.reload import async_setup_reload_service
from . import DOMAIN, PLATFORMS
2017-06-13 05:27:25 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_BODY_OFF = "body_off"
CONF_BODY_ON = "body_on"
CONF_IS_ON_TEMPLATE = "is_on_template"
CONF_STATE_RESOURCE = "state_resource"
2019-07-31 19:25:30 +00:00
DEFAULT_METHOD = "post"
DEFAULT_BODY_OFF = "OFF"
DEFAULT_BODY_ON = "ON"
DEFAULT_NAME = "REST Switch"
DEFAULT_TIMEOUT = 10
DEFAULT_VERIFY_SSL = True
2019-07-31 19:25:30 +00:00
SUPPORT_REST_METHODS = ["post", "put"]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_STATE_RESOURCE): cv.url,
2019-07-31 19:25:30 +00:00
vol.Optional(CONF_HEADERS): {cv.string: cv.string},
vol.Optional(CONF_PARAMS): {cv.string: cv.string},
2019-07-31 19:25:30 +00:00
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_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.Inclusive(CONF_USERNAME, "authentication"): cv.string,
vol.Inclusive(CONF_PASSWORD, "authentication"): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
}
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the RESTful switch."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
body_off = config.get(CONF_BODY_OFF)
2017-06-13 05:27:25 +00:00
body_on = config.get(CONF_BODY_ON)
is_on_template = config.get(CONF_IS_ON_TEMPLATE)
2017-06-13 05:27:25 +00:00
method = config.get(CONF_METHOD)
headers = config.get(CONF_HEADERS)
params = config.get(CONF_PARAMS)
2017-06-13 05:27:25 +00:00
name = config.get(CONF_NAME)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
2017-06-13 05:27:25 +00:00
resource = config.get(CONF_RESOURCE)
state_resource = config.get(CONF_STATE_RESOURCE) or resource
verify_ssl = config.get(CONF_VERIFY_SSL)
auth = None
if username:
auth = aiohttp.BasicAuth(username, password=password)
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)
try:
2019-07-31 19:25:30 +00:00
switch = RestSwitch(
name,
resource,
state_resource,
2019-07-31 19:25:30 +00:00
method,
headers,
params,
2019-07-31 19:25:30 +00:00
auth,
body_on,
body_off,
is_on_template,
timeout,
verify_ssl,
)
req = await switch.get_device_state(hass)
2020-04-09 19:43:42 +00:00
if req.status >= HTTP_BAD_REQUEST:
2017-05-03 08:11:39 +00:00
_LOGGER.error("Got non-ok response from resource: %s", req.status)
else:
async_add_entities([switch])
except (TypeError, ValueError):
2019-07-31 19:25:30 +00:00
_LOGGER.error(
"Missing resource or schema in configuration. "
"Add http:// or https:// to your URL"
)
except (asyncio.TimeoutError, aiohttp.ClientError):
2015-12-19 09:06:54 +00:00
_LOGGER.error("No route to resource/endpoint: %s", resource)
class RestSwitch(SwitchEntity):
2016-03-08 12:35:39 +00:00
"""Representation of a switch that can be toggled using REST."""
2019-07-31 19:25:30 +00:00
def __init__(
self,
name,
resource,
state_resource,
2019-07-31 19:25:30 +00:00
method,
headers,
params,
2019-07-31 19:25:30 +00:00
auth,
body_on,
body_off,
is_on_template,
timeout,
verify_ssl,
):
2016-03-08 12:35:39 +00:00
"""Initialize the REST switch."""
self._state = None
self._name = name
self._resource = resource
self._state_resource = state_resource
2017-06-13 05:27:25 +00:00
self._method = method
self._headers = headers
self._params = params
self._auth = auth
self._body_on = body_on
self._body_off = body_off
self._is_on_template = is_on_template
self._timeout = timeout
self._verify_ssl = verify_ssl
@property
def name(self):
2017-05-03 08:11:39 +00:00
"""Return the name of the switch."""
return self._name
@property
def is_on(self):
2016-03-10 07:34:38 +00:00
"""Return true if device is on."""
return self._state
async def async_turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
body_on_t = self._body_on.async_render(parse_result=False)
try:
req = await self.set_device_state(body_on_t)
if req.status == HTTP_OK:
self._state = True
else:
_LOGGER.error(
2019-07-31 19:25:30 +00:00
"Can't turn on %s. Is resource/endpoint offline?", self._resource
)
except (asyncio.TimeoutError, aiohttp.ClientError):
2018-10-01 10:51:39 +00:00
_LOGGER.error("Error while switching on %s", self._resource)
async def async_turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device off."""
body_off_t = self._body_off.async_render(parse_result=False)
try:
req = await self.set_device_state(body_off_t)
if req.status == HTTP_OK:
self._state = False
else:
_LOGGER.error(
2019-07-31 19:25:30 +00:00
"Can't turn off %s. Is resource/endpoint offline?", self._resource
)
except (asyncio.TimeoutError, aiohttp.ClientError):
2018-10-01 10:51:39 +00:00
_LOGGER.error("Error while switching off %s", self._resource)
async def set_device_state(self, body):
"""Send a state update to the device."""
websession = async_get_clientsession(self.hass, self._verify_ssl)
with async_timeout.timeout(self._timeout):
req = await getattr(websession, self._method)(
2019-07-31 19:25:30 +00:00
self._resource,
auth=self._auth,
data=bytes(body, "utf-8"),
headers=self._headers,
params=self._params,
2019-07-31 19:25:30 +00:00
)
return req
async def async_update(self):
"""Get the current state, catching errors."""
try:
await self.get_device_state(self.hass)
2018-10-01 10:51:39 +00:00
except asyncio.TimeoutError:
_LOGGER.exception("Timed out while fetching data")
except aiohttp.ClientError as err:
_LOGGER.exception("Error while fetching data: %s", err)
async def get_device_state(self, hass):
"""Get the latest data from REST API and update the state."""
websession = async_get_clientsession(hass, self._verify_ssl)
with async_timeout.timeout(self._timeout):
2019-07-31 19:25:30 +00:00
req = await websession.get(
self._state_resource,
auth=self._auth,
headers=self._headers,
params=self._params,
2019-07-31 19:25:30 +00:00
)
text = await req.text()
if self._is_on_template is not None:
text = self._is_on_template.async_render_with_possible_json_value(
2019-07-31 19:25:30 +00:00
text, "None"
)
text = text.lower()
2019-07-31 19:25:30 +00:00
if text == "true":
self._state = True
2019-07-31 19:25:30 +00:00
elif text == "false":
self._state = False
else:
self._state = None
else:
if text == self._body_on.template:
self._state = True
elif text == self._body_off.template:
self._state = False
else:
self._state = None
return req