Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions homeassistant/components/rest_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from homeassistant.const import (
CONF_TIMEOUT, CONF_USERNAME, CONF_PASSWORD, CONF_URL, CONF_PAYLOAD,
CONF_METHOD, CONF_HEADERS)
CONF_METHOD, CONF_HEADERS, CONF_VERIFY_SSL)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv

Expand All @@ -24,6 +24,7 @@

DEFAULT_TIMEOUT = 10
DEFAULT_METHOD = 'get'
DEFAULT_VERIFY_SSL = True

SUPPORT_REST_METHODS = [
'get',
Expand All @@ -43,7 +44,8 @@
vol.Inclusive(CONF_PASSWORD, 'authentication'): cv.string,
vol.Optional(CONF_PAYLOAD): cv.template,
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(int),
vol.Optional(CONF_CONTENT_TYPE): cv.string
vol.Optional(CONF_CONTENT_TYPE): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
})

CONFIG_SCHEMA = vol.Schema({
Expand All @@ -55,10 +57,12 @@

async def async_setup(hass, config):
"""Set up the REST command component."""
websession = async_get_clientsession(hass)

def async_register_rest_command(name, command_config):
"""Create service for rest command."""
websession = async_get_clientsession(
hass,
command_config.get(CONF_VERIFY_SSL)
)
timeout = command_config[CONF_TIMEOUT]
method = command_config[CONF_METHOD]

Expand Down
14 changes: 9 additions & 5 deletions homeassistant/components/switch/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
CONF_HEADERS, CONF_NAME, CONF_RESOURCE, CONF_TIMEOUT, CONF_METHOD,
CONF_USERNAME, CONF_PASSWORD)
CONF_USERNAME, CONF_PASSWORD, CONF_VERIFY_SSL)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv

Expand All @@ -29,6 +29,7 @@
DEFAULT_BODY_ON = 'ON'
DEFAULT_NAME = 'REST Switch'
DEFAULT_TIMEOUT = 10
DEFAULT_VERIFY_SSL = True

SUPPORT_REST_METHODS = ['post', 'put']

Expand All @@ -44,6 +45,7 @@
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,
})


Expand All @@ -59,6 +61,7 @@ async def async_setup_platform(hass, config, async_add_entities,
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
resource = config.get(CONF_RESOURCE)
verify_ssl = config.get(CONF_VERIFY_SSL)

auth = None
if username:
Expand All @@ -74,7 +77,7 @@ async def async_setup_platform(hass, config, async_add_entities,

try:
switch = RestSwitch(name, resource, method, headers, auth, body_on,
body_off, is_on_template, timeout)
body_off, is_on_template, timeout, verify_ssl)

req = await switch.get_device_state(hass)
if req.status >= 400:
Expand All @@ -92,7 +95,7 @@ class RestSwitch(SwitchDevice):
"""Representation of a switch that can be toggled using REST."""

def __init__(self, name, resource, method, headers, auth, body_on,
body_off, is_on_template, timeout):
body_off, is_on_template, timeout, verify_ssl):
"""Initialize the REST switch."""
self._state = None
self._name = name
Expand All @@ -104,6 +107,7 @@ def __init__(self, name, resource, method, headers, auth, 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):
Expand Down Expand Up @@ -148,7 +152,7 @@ async def async_turn_off(self, **kwargs):

async def set_device_state(self, body):
"""Send a state update to the device."""
websession = async_get_clientsession(self.hass)
websession = async_get_clientsession(self.hass, self._verify_ssl)

with async_timeout.timeout(self._timeout, loop=self.hass.loop):
req = await getattr(websession, self._method)(
Expand All @@ -167,7 +171,7 @@ async def async_update(self):

async def get_device_state(self, hass):
"""Get the latest data from REST API and update the state."""
websession = async_get_clientsession(hass)
websession = async_get_clientsession(hass, self._verify_ssl)

with async_timeout.timeout(self._timeout, loop=hass.loop):
req = await websession.get(self._resource, auth=self._auth,
Expand Down
2 changes: 1 addition & 1 deletion tests/components/switch/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def setup_method(self):
self.body_off = Template('off', self.hass)
self.switch = rest.RestSwitch(
self.name, self.resource, self.method, self.headers, self.auth,
self.body_on, self.body_off, None, 10)
self.body_on, self.body_off, None, 10, True)
self.switch.hass = self.hass

def teardown_method(self):
Expand Down