Skip to content
Snippets Groups Projects
Commit 47b5fbfa authored by Paulus Schoutsen's avatar Paulus Schoutsen
Browse files

Add template API endpoint

parent d1383ac9
Branches
Tags
No related merge requests found
...@@ -14,14 +14,16 @@ import json ...@@ -14,14 +14,16 @@ import json
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.helpers.state import TrackStates from homeassistant.helpers.state import TrackStates
import homeassistant.remote as rem import homeassistant.remote as rem
from homeassistant.util import template
from homeassistant.bootstrap import ERROR_LOG_FILENAME from homeassistant.bootstrap import ERROR_LOG_FILENAME
from homeassistant.const import ( from homeassistant.const import (
URL_API, URL_API_STATES, URL_API_EVENTS, URL_API_SERVICES, URL_API_STREAM, URL_API, URL_API_STATES, URL_API_EVENTS, URL_API_SERVICES, URL_API_STREAM,
URL_API_EVENT_FORWARD, URL_API_STATES_ENTITY, URL_API_COMPONENTS, URL_API_EVENT_FORWARD, URL_API_STATES_ENTITY, URL_API_COMPONENTS,
URL_API_CONFIG, URL_API_BOOTSTRAP, URL_API_ERROR_LOG, URL_API_LOG_OUT, URL_API_CONFIG, URL_API_BOOTSTRAP, URL_API_ERROR_LOG, URL_API_LOG_OUT,
EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP, MATCH_ALL, URL_API_TEMPLATE, EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP, MATCH_ALL,
HTTP_OK, HTTP_CREATED, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_OK, HTTP_CREATED, HTTP_BAD_REQUEST, HTTP_NOT_FOUND,
HTTP_UNPROCESSABLE_ENTITY) HTTP_UNPROCESSABLE_ENTITY, HTTP_HEADER_CONTENT_TYPE,
CONTENT_TYPE_TEXT_PLAIN)
DOMAIN = 'api' DOMAIN = 'api'
...@@ -91,6 +93,9 @@ def setup(hass, config): ...@@ -91,6 +93,9 @@ def setup(hass, config):
hass.http.register_path('POST', URL_API_LOG_OUT, _handle_post_api_log_out) hass.http.register_path('POST', URL_API_LOG_OUT, _handle_post_api_log_out)
hass.http.register_path('POST', URL_API_TEMPLATE,
_handle_post_api_template)
return True return True
...@@ -359,6 +364,17 @@ def _handle_post_api_log_out(handler, path_match, data): ...@@ -359,6 +364,17 @@ def _handle_post_api_log_out(handler, path_match, data):
handler.end_headers() handler.end_headers()
def _handle_post_api_template(handler, path_match, data):
""" Log user out. """
template_string = data.get('template', '')
handler.send_response(HTTP_OK)
handler.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_TEXT_PLAIN)
handler.end_headers()
handler.wfile.write(
template.render(handler.server.hass, template_string).encode('utf-8'))
def _services_json(hass): def _services_json(hass):
""" Generate services data to JSONify. """ """ Generate services data to JSONify. """
return [{"domain": key, "services": value} return [{"domain": key, "services": value}
......
...@@ -165,6 +165,7 @@ URL_API_COMPONENTS = "/api/components" ...@@ -165,6 +165,7 @@ URL_API_COMPONENTS = "/api/components"
URL_API_BOOTSTRAP = "/api/bootstrap" URL_API_BOOTSTRAP = "/api/bootstrap"
URL_API_ERROR_LOG = "/api/error_log" URL_API_ERROR_LOG = "/api/error_log"
URL_API_LOG_OUT = "/api/log_out" URL_API_LOG_OUT = "/api/log_out"
URL_API_TEMPLATE = "/api/template"
HTTP_OK = 200 HTTP_OK = 200
HTTP_CREATED = 201 HTTP_CREATED = 201
......
...@@ -326,6 +326,20 @@ class TestAPI(unittest.TestCase): ...@@ -326,6 +326,20 @@ class TestAPI(unittest.TestCase):
self.assertEqual(1, len(test_value)) self.assertEqual(1, len(test_value))
def test_api_template(self):
""" Test template API. """
hass.states.set('sensor.temperature', 10)
req = requests.post(
_url(const.URL_API_TEMPLATE),
data=json.dumps({"template":
'{{ states.sensor.temperature.state }}'}),
headers=HA_HEADERS)
hass.pool.block_till_done()
self.assertEqual('10', req.text)
def test_api_event_forward(self): def test_api_event_forward(self):
""" Test setting up event forwarding. """ """ Test setting up event forwarding. """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment