Call service api simplified

pull/5/head
Paulus Schoutsen 2014-10-19 18:41:06 -07:00
parent 001f27cdb4
commit 9979a3266e
3 changed files with 20 additions and 17 deletions

View File

@ -263,7 +263,7 @@ class RequestHandler(BaseHTTPRequestHandler):
api_password = data['api_password']
if '_METHOD' in data:
method = data['_METHOD']
method = data.pop('_METHOD')
if url.path.startswith('/api/'):
self.use_json = True
@ -382,10 +382,8 @@ class RequestHandler(BaseHTTPRequestHandler):
"<link rel='shortcut icon' href='/static/favicon.ico' />"
"<link rel='icon' type='image/png' "
" href='/static/favicon-192x192.png' sizes='192x192'>"
"<script src='http://code.jquery.com/jquery-2.1.1.min.js'>"
" </script>"
"<script type='text/javascript' src='/static/script.js'>"
" </script>"
"<script data-main='static/javascripts/app'"
"src='/static/javascripts/require.js'></script>"
"</head>"
"<body>"
"<div class='container'>"
@ -684,13 +682,8 @@ class RequestHandler(BaseHTTPRequestHandler):
"""
domain = path_match.group('domain')
service = path_match.group('service')
service_data = data.get('service_data')
if service_data is not None and not isinstance(service_data, dict):
self._message("service_data should be an object",
HTTP_UNPROCESSABLE_ENTITY)
self.server.hass.call_service(domain, service, service_data)
self.server.hass.call_service(domain, service, data)
self._message("Service {}/{} called.".format(domain, service))
@ -749,7 +742,9 @@ class RequestHandler(BaseHTTPRequestHandler):
def _handle_get_static(self, path_match, data):
""" Returns a static file. """
req_file = util.sanitize_filename(path_match.group('file'))
# req_file = util.sanitize_filename(path_match.group('file'))
# TODO make safe
req_file = path_match.group('file')
path = os.path.join(os.path.dirname(__file__), 'www_static', req_file)

View File

@ -418,8 +418,16 @@ def get_services(api, logger=None):
def call_service(api, domain, service, service_data=None, logger=None):
""" Calls a service at the remote API. """
event_data = service_data or {}
event_data[ha.ATTR_DOMAIN] = domain
event_data[ha.ATTR_SERVICE] = service
try:
req = api(METHOD_POST,
URL_API_SERVICES_SERVICE.format(domain, service),
service_data)
fire_event(api, ha.EVENT_CALL_SERVICE, event_data, logger)
if req.status_code != 200 and logger:
logger.error(
"Error calling service: {} - {}".format(
req.status_code, req.text))
except ha.HomeAssistantError:
if logger:
logger.exception("Error setting state to server")

View File

@ -289,7 +289,7 @@ class TestHTTP(unittest.TestCase):
requests.post(
_url(remote.URL_API_SERVICES_SERVICE.format(
"test_domain", "test_service")),
data=json.dumps({"service_data": {"test": 1}}),
data=json.dumps({"test": 1}),
headers=HA_HEADERS)
# Allow the event to take place