Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -8,54 +8,57 @@ import async_timeout
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_TIMEOUT, CONF_USERNAME, CONF_PASSWORD, CONF_URL, CONF_PAYLOAD,
|
||||
CONF_METHOD, CONF_HEADERS, CONF_VERIFY_SSL)
|
||||
CONF_TIMEOUT,
|
||||
CONF_USERNAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_URL,
|
||||
CONF_PAYLOAD,
|
||||
CONF_METHOD,
|
||||
CONF_HEADERS,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
DOMAIN = 'rest_command'
|
||||
DOMAIN = "rest_command"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_TIMEOUT = 10
|
||||
DEFAULT_METHOD = 'get'
|
||||
DEFAULT_METHOD = "get"
|
||||
DEFAULT_VERIFY_SSL = True
|
||||
|
||||
SUPPORT_REST_METHODS = [
|
||||
'get',
|
||||
'post',
|
||||
'put',
|
||||
'delete',
|
||||
]
|
||||
SUPPORT_REST_METHODS = ["get", "post", "put", "delete"]
|
||||
|
||||
CONF_CONTENT_TYPE = 'content_type'
|
||||
CONF_CONTENT_TYPE = "content_type"
|
||||
|
||||
COMMAND_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_URL): cv.template,
|
||||
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD):
|
||||
vol.All(vol.Lower, vol.In(SUPPORT_REST_METHODS)),
|
||||
vol.Optional(CONF_HEADERS): vol.Schema({cv.string: cv.string}),
|
||||
vol.Inclusive(CONF_USERNAME, 'authentication'): cv.string,
|
||||
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_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
||||
})
|
||||
COMMAND_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_URL): cv.template,
|
||||
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): vol.All(
|
||||
vol.Lower, vol.In(SUPPORT_REST_METHODS)
|
||||
),
|
||||
vol.Optional(CONF_HEADERS): vol.Schema({cv.string: cv.string}),
|
||||
vol.Inclusive(CONF_USERNAME, "authentication"): cv.string,
|
||||
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_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: cv.schema_with_slug_keys(COMMAND_SCHEMA),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{DOMAIN: cv.schema_with_slug_keys(COMMAND_SCHEMA)}, extra=vol.ALLOW_EXTRA
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the REST command component."""
|
||||
|
||||
def async_register_rest_command(name, command_config):
|
||||
"""Create service for rest command."""
|
||||
websession = async_get_clientsession(
|
||||
hass,
|
||||
command_config.get(CONF_VERIFY_SSL)
|
||||
)
|
||||
websession = async_get_clientsession(hass, command_config.get(CONF_VERIFY_SSL))
|
||||
timeout = command_config[CONF_TIMEOUT]
|
||||
method = command_config[CONF_METHOD]
|
||||
|
||||
@@ -65,7 +68,7 @@ async def async_setup(hass, config):
|
||||
auth = None
|
||||
if CONF_USERNAME in command_config:
|
||||
username = command_config[CONF_USERNAME]
|
||||
password = command_config.get(CONF_PASSWORD, '')
|
||||
password = command_config.get(CONF_PASSWORD, "")
|
||||
auth = aiohttp.BasicAuth(username, password=password)
|
||||
|
||||
template_payload = None
|
||||
@@ -88,8 +91,8 @@ async def async_setup(hass, config):
|
||||
payload = None
|
||||
if template_payload:
|
||||
payload = bytes(
|
||||
template_payload.async_render(variables=service.data),
|
||||
'utf-8')
|
||||
template_payload.async_render(variables=service.data), "utf-8"
|
||||
)
|
||||
|
||||
try:
|
||||
with async_timeout.timeout(timeout):
|
||||
@@ -97,14 +100,13 @@ async def async_setup(hass, config):
|
||||
template_url.async_render(variables=service.data),
|
||||
data=payload,
|
||||
auth=auth,
|
||||
headers=headers
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
if request.status < 400:
|
||||
_LOGGER.info("Success call %s.", request.url)
|
||||
else:
|
||||
_LOGGER.warning(
|
||||
"Error %d on call %s.", request.status, request.url)
|
||||
_LOGGER.warning("Error %d on call %s.", request.status, request.url)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
_LOGGER.warning("Timeout call %s.", request.url)
|
||||
|
||||
Reference in new issue
Block a user