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
This commit is contained in:
John Arild Berentsen 2017-11-06 15:15:52 +01:00 committed by GitHub
parent 131af1fece
commit 5410700708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 3 deletions

View File

@ -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."""

View File

@ -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.'}