From 5410700708dee1d32028de831459914d21e2f26b Mon Sep 17 00:00:00 2001 From: John Arild Berentsen Date: Mon, 6 Nov 2017 15:15:52 +0100 Subject: [PATCH] Zwave save cache to file now. (#10381) * Add save config * Add API to save Z-Wave cache to file immediatley. * lint * remove none assignment * docstring --- homeassistant/components/config/zwave.py | 26 ++++++++++++++++-- tests/components/config/test_zwave.py | 35 +++++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/config/zwave.py b/homeassistant/components/config/zwave.py index 53fa200a1b1..dd8552f374e 100644 --- a/homeassistant/components/config/zwave.py +++ b/homeassistant/components/config/zwave.py @@ -1,14 +1,15 @@ """Provide configuration end points for Z-Wave.""" import asyncio +import logging import homeassistant.core as ha -from homeassistant.const import HTTP_NOT_FOUND +from homeassistant.const import HTTP_NOT_FOUND, HTTP_OK from homeassistant.components.http import HomeAssistantView from homeassistant.components.config import EditKeyBasedConfigView from homeassistant.components.zwave import const, DEVICE_CONFIG_SCHEMA_ENTRY import homeassistant.helpers.config_validation as cv - +_LOGGER = logging.getLogger(__name__) CONFIG_PATH = 'zwave_device_config.yaml' OZW_LOG_FILENAME = 'OZW_Log.txt' URL_API_OZW_LOG = '/api/zwave/ozwlog' @@ -27,10 +28,31 @@ def async_setup(hass): hass.http.register_view(ZWaveUserCodeView) hass.http.register_static_path( URL_API_OZW_LOG, hass.config.path(OZW_LOG_FILENAME), False) + hass.http.register_view(ZWaveConfigWriteView) return True +class ZWaveConfigWriteView(HomeAssistantView): + """View to save the ZWave configuration to zwcfg_xxxxx.xml.""" + + url = "/api/zwave/saveconfig" + name = "api:zwave:saveconfig" + + @ha.callback + def post(self, request): + """Save cache configuration to zwcfg_xxxxx.xml.""" + hass = request.app['hass'] + network = hass.data.get(const.DATA_NETWORK) + if network is None: + return self.json_message('No Z-Wave network data found', + HTTP_NOT_FOUND) + _LOGGER.info("Z-Wave configuration written to file.") + network.write_config() + return self.json_message('Z-Wave configuration saved to file.', + HTTP_OK) + + class ZWaveNodeValueView(HomeAssistantView): """View to return the node values.""" diff --git a/tests/components/config/test_zwave.py b/tests/components/config/test_zwave.py index fc359dc7ff7..81800d709e3 100644 --- a/tests/components/config/test_zwave.py +++ b/tests/components/config/test_zwave.py @@ -9,7 +9,7 @@ from homeassistant.components import config from homeassistant.components.zwave import DATA_NETWORK, const from homeassistant.components.config.zwave import ( ZWaveNodeValueView, ZWaveNodeGroupView, ZWaveNodeConfigView, - ZWaveUserCodeView) + ZWaveUserCodeView, ZWaveConfigWriteView) from tests.common import mock_http_component_app from tests.mock.zwave import MockNode, MockValue, MockEntityValues @@ -417,3 +417,36 @@ def test_get_usercodes_no_genreuser(hass, test_client): result = yield from resp.json() assert result == {} + + +@asyncio.coroutine +def test_save_config_no_network(hass, test_client): + """Test saving configuration without network data.""" + app = mock_http_component_app(hass) + ZWaveConfigWriteView().register(app.router) + + client = yield from test_client(app) + + resp = yield from client.post('/api/zwave/saveconfig') + + assert resp.status == 404 + result = yield from resp.json() + assert result == {'message': 'No Z-Wave network data found'} + + +@asyncio.coroutine +def test_save_config(hass, test_client): + """Test saving configuration.""" + app = mock_http_component_app(hass) + ZWaveConfigWriteView().register(app.router) + + network = hass.data[DATA_NETWORK] = MagicMock() + + client = yield from test_client(app) + + resp = yield from client.post('/api/zwave/saveconfig') + + assert resp.status == 200 + result = yield from resp.json() + assert network.write_config.called + assert result == {'message': 'Z-Wave configuration saved to file.'}