diff --git a/homeassistant/components/api.py b/homeassistant/components/api.py index 1e6e66baee02b609186d1a099016a9420e6ae886..6a66a2a110e197521076578b83253c9fcbc34f6b 100644 --- a/homeassistant/components/api.py +++ b/homeassistant/components/api.py @@ -14,14 +14,16 @@ import json import homeassistant.core as ha from homeassistant.helpers.state import TrackStates import homeassistant.remote as rem +from homeassistant.util import template from homeassistant.bootstrap import ERROR_LOG_FILENAME from homeassistant.const import ( 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_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_UNPROCESSABLE_ENTITY) + HTTP_UNPROCESSABLE_ENTITY, HTTP_HEADER_CONTENT_TYPE, + CONTENT_TYPE_TEXT_PLAIN) DOMAIN = 'api' @@ -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_TEMPLATE, + _handle_post_api_template) + return True @@ -359,6 +364,17 @@ def _handle_post_api_log_out(handler, path_match, data): 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): """ Generate services data to JSONify. """ return [{"domain": key, "services": value} diff --git a/homeassistant/const.py b/homeassistant/const.py index 9a17622a7dded1b68bf9f988acc12ceda0ea6bc3..cdb56e60131ec29cd36e568635101376937f3cdf 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -165,6 +165,7 @@ URL_API_COMPONENTS = "/api/components" URL_API_BOOTSTRAP = "/api/bootstrap" URL_API_ERROR_LOG = "/api/error_log" URL_API_LOG_OUT = "/api/log_out" +URL_API_TEMPLATE = "/api/template" HTTP_OK = 200 HTTP_CREATED = 201 diff --git a/tests/components/test_api.py b/tests/components/test_api.py index ab76ed0e3db88780e9ecae769c07c6cc67667bda..cf530c1f3014790651414a39c0594514155f10f2 100644 --- a/tests/components/test_api.py +++ b/tests/components/test_api.py @@ -326,6 +326,20 @@ class TestAPI(unittest.TestCase): 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): """ Test setting up event forwarding. """