From 189023821bd492b8631382b0b4b076b39b24620e Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Fri, 12 May 2017 20:27:44 -0700 Subject: [PATCH] Tests for zwave setup features (#7570) * Tests for zwave setup features * Add test for frontend panel register --- tests/components/zwave/test_init.py | 68 ++++++++++++++++++++++++++++- tests/conftest.py | 3 +- tests/mock/zwave.py | 20 ++++++++- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/tests/components/zwave/test_init.py b/tests/components/zwave/test_init.py index a3327d6d558..9f4e9b92c68 100644 --- a/tests/components/zwave/test_init.py +++ b/tests/components/zwave/test_init.py @@ -1,6 +1,7 @@ """Tests for the Z-Wave init.""" import asyncio from collections import OrderedDict +from datetime import datetime from homeassistant.bootstrap import async_setup_component from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START @@ -14,7 +15,8 @@ import pytest import unittest from unittest.mock import patch, MagicMock -from tests.common import get_test_home_assistant +from tests.common import ( + get_test_home_assistant, async_fire_time_changed, mock_http_component) from tests.mock.zwave import MockNetwork, MockNode, MockValue, MockEntityValues @@ -69,6 +71,70 @@ def test_config_access_error(): assert result is None +@asyncio.coroutine +def test_network_options(hass, mock_openzwave): + """Test network options.""" + result = yield from async_setup_component(hass, 'zwave', { + 'zwave': { + 'usb_path': 'mock_usb_path', + 'config_path': 'mock_config_path', + }}) + + assert result + + network = hass.data[zwave.ZWAVE_NETWORK] + assert network.options.device == 'mock_usb_path' + assert network.options.config_path == 'mock_config_path' + + +@asyncio.coroutine +def test_auto_heal_midnight(hass, mock_openzwave): + """Test network auto-heal at midnight.""" + assert (yield from async_setup_component(hass, 'zwave', { + 'zwave': { + 'autoheal': True, + }})) + network = hass.data[zwave.ZWAVE_NETWORK] + assert not network.heal.called + + time = datetime(2017, 5, 6, 0, 0, 0) + async_fire_time_changed(hass, time) + yield from hass.async_block_till_done() + assert network.heal.called + assert len(network.heal.mock_calls) == 1 + + +@asyncio.coroutine +def test_auto_heal_disabled(hass, mock_openzwave): + """Test network auto-heal disabled.""" + assert (yield from async_setup_component(hass, 'zwave', { + 'zwave': { + 'autoheal': False, + }})) + network = hass.data[zwave.ZWAVE_NETWORK] + assert not network.heal.called + + time = datetime(2017, 5, 6, 0, 0, 0) + async_fire_time_changed(hass, time) + yield from hass.async_block_till_done() + assert not network.heal.called + + +@asyncio.coroutine +def test_frontend_panel_register(hass, mock_openzwave): + """Test network auto-heal disabled.""" + mock_http_component(hass) + hass.config.components |= set(['frontend']) + with patch('homeassistant.components.zwave.' + 'register_built_in_panel') as mock_register: + assert (yield from async_setup_component(hass, 'zwave', { + 'zwave': { + 'autoheal': False, + }})) + assert mock_register.called + assert len(mock_register.mock_calls) == 1 + + @asyncio.coroutine def test_setup_platform(hass, mock_openzwave): """Test invalid device config.""" diff --git a/tests/conftest.py b/tests/conftest.py index b6c9795f127..07564e86c79 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,7 @@ from homeassistant.components import mqtt from tests.common import async_test_home_assistant, mock_coro from tests.test_util.aiohttp import mock_aiohttp_client -from tests.mock.zwave import MockNetwork +from tests.mock.zwave import MockNetwork, MockOption if os.environ.get('UVLOOP') == '1': import uvloop @@ -101,6 +101,7 @@ def mock_openzwave(): libopenzwave = base_mock.libopenzwave libopenzwave.__file__ = 'test' base_mock.network.ZWaveNetwork = MockNetwork + base_mock.option.ZWaveOption = MockOption with patch.dict('sys.modules', { 'libopenzwave': libopenzwave, diff --git a/tests/mock/zwave.py b/tests/mock/zwave.py index 513c606aab2..672cc884904 100644 --- a/tests/mock/zwave.py +++ b/tests/mock/zwave.py @@ -32,6 +32,23 @@ def notification(node_id, network=None): ) +class MockOption(MagicMock): + """Mock Z-Wave options.""" + + def __init__(self, device=None, config_path=None, user_path=None, + cmd_line=None): + """Initialize a Z-Wave mock options.""" + super().__init__() + self.device = device + self.config_path = config_path + self.user_path = user_path + self.cmd_line = cmd_line + + def _get_child_mock(self, **kw): + """Create child mocks with right MagicMock class.""" + return MagicMock(**kw) + + class MockNetwork(MagicMock): """Mock Z-Wave network.""" @@ -84,9 +101,10 @@ class MockNetwork(MagicMock): STATE_AWAKED = 7 STATE_READY = 10 - def __init__(self, *args, **kwargs): + def __init__(self, options=None, *args, **kwargs): """Initialize a Z-Wave mock network.""" super().__init__() + self.options = options self.state = MockNetwork.STATE_STOPPED