2017-06-05 06:53:25 +00:00
|
|
|
"""Tests for Vanderbilt SPC component."""
|
2019-12-09 13:38:01 +00:00
|
|
|
from unittest.mock import Mock, PropertyMock, patch
|
2017-06-05 06:53:25 +00:00
|
|
|
|
|
|
|
from homeassistant.bootstrap import async_setup_component
|
2018-09-24 08:10:10 +00:00
|
|
|
from homeassistant.components.spc import DATA_API
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import STATE_ALARM_ARMED_AWAY, STATE_ALARM_DISARMED
|
2018-09-24 08:10:10 +00:00
|
|
|
|
|
|
|
from tests.common import mock_coro
|
|
|
|
|
|
|
|
|
|
|
|
async def test_valid_device_config(hass, monkeypatch):
|
|
|
|
"""Test valid device config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"spc": {"api_url": "http://localhost/", "ws_url": "ws://localhost/"}}
|
2017-06-05 06:53:25 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-12-06 14:40:04 +00:00
|
|
|
"homeassistant.components.spc.SpcWebGateway.async_load_parameters",
|
|
|
|
return_value=mock_coro(True),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
assert await async_setup_component(hass, "spc", config) is True
|
2017-06-05 06:53:25 +00:00
|
|
|
|
|
|
|
|
2018-09-24 08:10:10 +00:00
|
|
|
async def test_invalid_device_config(hass, monkeypatch):
|
|
|
|
"""Test valid device config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"spc": {"api_url": "http://localhost/"}}
|
2018-09-24 08:10:10 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-12-06 14:40:04 +00:00
|
|
|
"homeassistant.components.spc.SpcWebGateway.async_load_parameters",
|
|
|
|
return_value=mock_coro(True),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
assert await async_setup_component(hass, "spc", config) is False
|
2018-09-24 08:10:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_alarm_device(hass):
|
|
|
|
"""Test that alarm panel state changes on incoming websocket data."""
|
|
|
|
import pyspcwebgw
|
|
|
|
from pyspcwebgw.const import AreaMode
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"spc": {"api_url": "http://localhost/", "ws_url": "ws://localhost/"}}
|
2018-09-24 08:10:10 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
area_mock = Mock(
|
|
|
|
spec=pyspcwebgw.area.Area,
|
|
|
|
id="1",
|
|
|
|
mode=AreaMode.FULL_SET,
|
|
|
|
last_changed_by="Sven",
|
|
|
|
)
|
|
|
|
area_mock.name = "House"
|
2018-09-24 08:10:10 +00:00
|
|
|
area_mock.verified_alarm = False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-12-06 14:40:04 +00:00
|
|
|
"homeassistant.components.spc.SpcWebGateway.areas", new_callable=PropertyMock
|
2019-07-31 19:25:30 +00:00
|
|
|
) as mock_areas:
|
|
|
|
mock_areas.return_value = {"1": area_mock}
|
|
|
|
with patch(
|
2019-12-06 14:40:04 +00:00
|
|
|
"homeassistant.components.spc.SpcWebGateway.async_load_parameters",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=mock_coro(True),
|
|
|
|
):
|
|
|
|
assert await async_setup_component(hass, "spc", config) is True
|
2018-09-24 08:10:10 +00:00
|
|
|
|
2018-10-07 21:30:09 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "alarm_control_panel.house"
|
2018-09-24 08:10:10 +00:00
|
|
|
|
|
|
|
assert hass.states.get(entity_id).state == STATE_ALARM_ARMED_AWAY
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get(entity_id).attributes["changed_by"] == "Sven"
|
2018-09-24 08:10:10 +00:00
|
|
|
|
|
|
|
area_mock.mode = AreaMode.UNSET
|
2019-07-31 19:25:30 +00:00
|
|
|
area_mock.last_changed_by = "Anna"
|
2018-09-24 08:10:10 +00:00
|
|
|
await hass.data[DATA_API]._async_callback(area_mock)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(entity_id).state == STATE_ALARM_DISARMED
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get(entity_id).attributes["changed_by"] == "Anna"
|