Add event sensors for risco (#39594)

* Add Risco event sensors

* Fix lint
This commit is contained in:
On Freund authored and GitHub committed 2020-09-04 14:11:07 -05:00
1 parent 9b23d7c2fd
commit ad6e8b2d62
10 files changed
+404 -84

No files matched your search

@@ -11,9 +11,6 @@ from homeassistant.components.alarm_control_panel.const import (
from homeassistant.components.risco import CannotConnectError, UnauthorizedError
from homeassistant.components.risco.const import DOMAIN
from homeassistant.const import (
CONF_PASSWORD,
CONF_PIN,
CONF_USERNAME,
SERVICE_ALARM_ARM_AWAY,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
SERVICE_ALARM_ARM_HOME,
@@ -30,16 +27,11 @@ from homeassistant.const import (
)
from homeassistant.helpers.entity_component import async_update_entity
from .util import TEST_CONFIG, TEST_SITE_UUID, setup_risco
from tests.async_mock import MagicMock, PropertyMock, patch
from tests.common import MockConfigEntry
TEST_CONFIG = {
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
CONF_PIN: "1234",
}
TEST_SITE_UUID = "test-site-uuid"
TEST_SITE_NAME = "test-site-name"
FIRST_ENTITY_ID = "alarm_control_panel.risco_test_site_name_partition_0"
SECOND_ENTITY_ID = "alarm_control_panel.risco_test_site_name_partition_1"
@@ -110,28 +102,6 @@ def two_part_alarm():
yield alarm_mock
async def _setup_risco(hass, options={}):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG, options=options)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.risco.RiscoAPI.login",
return_value=True,
), patch(
"homeassistant.components.risco.RiscoAPI.site_uuid",
new_callable=PropertyMock(return_value=TEST_SITE_UUID),
), patch(
"homeassistant.components.risco.RiscoAPI.site_name",
new_callable=PropertyMock(return_value=TEST_SITE_NAME),
), patch(
"homeassistant.components.risco.RiscoAPI.close"
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry
async def test_cannot_connect(hass):
"""Test connection error."""
@@ -171,7 +141,7 @@ async def test_setup(hass, two_part_alarm):
assert not registry.async_is_registered(FIRST_ENTITY_ID)
assert not registry.async_is_registered(SECOND_ENTITY_ID)
await _setup_risco(hass)
await setup_risco(hass)
assert registry.async_is_registered(FIRST_ENTITY_ID)
assert registry.async_is_registered(SECOND_ENTITY_ID)
@@ -196,7 +166,7 @@ async def _check_state(hass, alarm, property, state, entity_id, partition_id):
async def test_states(hass, two_part_alarm):
"""Test the various alarm states."""
await _setup_risco(hass, CUSTOM_MAPPING_OPTIONS)
await setup_risco(hass, CUSTOM_MAPPING_OPTIONS)
assert hass.states.get(FIRST_ENTITY_ID).state == STATE_UNKNOWN
for partition_id, entity_id in {0: FIRST_ENTITY_ID, 1: SECOND_ENTITY_ID}.items():
@@ -278,7 +248,7 @@ async def _call_alarm_service(hass, service, entity_id, **kwargs):
async def test_sets_custom_mapping(hass, two_part_alarm):
"""Test settings the various modes when mapping some states."""
await _setup_risco(hass, CUSTOM_MAPPING_OPTIONS)
await setup_risco(hass, CUSTOM_MAPPING_OPTIONS)
registry = await hass.helpers.entity_registry.async_get_registry()
entity = registry.async_get(FIRST_ENTITY_ID)
@@ -304,7 +274,7 @@ async def test_sets_custom_mapping(hass, two_part_alarm):
async def test_sets_full_custom_mapping(hass, two_part_alarm):
"""Test settings the various modes when mapping all states."""
await _setup_risco(hass, FULL_CUSTOM_MAPPING)
await setup_risco(hass, FULL_CUSTOM_MAPPING)
registry = await hass.helpers.entity_registry.async_get_registry()
entity = registry.async_get(FIRST_ENTITY_ID)
@@ -338,7 +308,7 @@ async def test_sets_full_custom_mapping(hass, two_part_alarm):
async def test_sets_with_correct_code(hass, two_part_alarm):
"""Test settings the various modes when code is required."""
await _setup_risco(hass, {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
await setup_risco(hass, {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
code = {"code": 1234}
await _test_service_call(
@@ -380,7 +350,7 @@ async def test_sets_with_correct_code(hass, two_part_alarm):
async def test_sets_with_incorrect_code(hass, two_part_alarm):
"""Test settings the various modes when code is required and incorrect."""
await _setup_risco(hass, {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
await setup_risco(hass, {**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS})
code = {"code": 4321}
await _test_no_service_call(
+7 -40
View File
@@ -3,25 +3,14 @@ import pytest
from homeassistant.components.risco import CannotConnectError, UnauthorizedError
from homeassistant.components.risco.const import DOMAIN
from homeassistant.const import (
CONF_PASSWORD,
CONF_PIN,
CONF_USERNAME,
STATE_OFF,
STATE_ON,
)
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.helpers.entity_component import async_update_entity
from .util import TEST_CONFIG, TEST_SITE_UUID, setup_risco
from tests.async_mock import MagicMock, PropertyMock, patch
from tests.common import MockConfigEntry
TEST_CONFIG = {
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
CONF_PIN: "1234",
}
TEST_SITE_UUID = "test-site-uuid"
TEST_SITE_NAME = "test-site-name"
FIRST_ENTITY_ID = "binary_sensor.zone_0"
SECOND_ENTITY_ID = "binary_sensor.zone_1"
@@ -57,28 +46,6 @@ def two_zone_alarm():
yield alarm_mock
async def _setup_risco(hass):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.risco.RiscoAPI.login",
return_value=True,
), patch(
"homeassistant.components.risco.RiscoAPI.site_uuid",
new_callable=PropertyMock(return_value=TEST_SITE_UUID),
), patch(
"homeassistant.components.risco.RiscoAPI.site_name",
new_callable=PropertyMock(return_value=TEST_SITE_NAME),
), patch(
"homeassistant.components.risco.RiscoAPI.close"
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry
async def test_cannot_connect(hass):
"""Test connection error."""
@@ -118,7 +85,7 @@ async def test_setup(hass, two_zone_alarm):
assert not registry.async_is_registered(FIRST_ENTITY_ID)
assert not registry.async_is_registered(SECOND_ENTITY_ID)
await _setup_risco(hass)
await setup_risco(hass)
assert registry.async_is_registered(FIRST_ENTITY_ID)
assert registry.async_is_registered(SECOND_ENTITY_ID)
@@ -153,7 +120,7 @@ async def _check_state(hass, alarm, triggered, bypassed, entity_id, zone_id):
async def test_states(hass, two_zone_alarm):
"""Test the various alarm states."""
await _setup_risco(hass)
await setup_risco(hass)
await _check_state(hass, two_zone_alarm, True, True, FIRST_ENTITY_ID, 0)
await _check_state(hass, two_zone_alarm, True, False, FIRST_ENTITY_ID, 0)
@@ -167,7 +134,7 @@ async def test_states(hass, two_zone_alarm):
async def test_bypass(hass, two_zone_alarm):
"""Test bypassing a zone."""
await _setup_risco(hass)
await setup_risco(hass)
with patch("homeassistant.components.risco.RiscoAPI.bypass_zone") as mock:
data = {"entity_id": FIRST_ENTITY_ID}
@@ -180,7 +147,7 @@ async def test_bypass(hass, two_zone_alarm):
async def test_unbypass(hass, two_zone_alarm):
"""Test unbypassing a zone."""
await _setup_risco(hass)
await setup_risco(hass)
with patch("homeassistant.components.risco.RiscoAPI.bypass_zone") as mock:
data = {"entity_id": FIRST_ENTITY_ID}
+207
View File
@@ -0,0 +1,207 @@
"""Tests for the Risco event sensors."""
import pytest
from homeassistant.components.risco import (
LAST_EVENT_TIMESTAMP_KEY,
CannotConnectError,
UnauthorizedError,
)
from homeassistant.components.risco.const import DOMAIN, EVENTS_COORDINATOR
from .util import TEST_CONFIG, setup_risco
from tests.async_mock import MagicMock, patch
from tests.common import MockConfigEntry
ENTITY_IDS = {
"Alarm": "sensor.risco_test_site_name_alarm_events",
"Status": "sensor.risco_test_site_name_status_events",
"Trouble": "sensor.risco_test_site_name_trouble_events",
"Other": "sensor.risco_test_site_name_other_events",
}
TEST_EVENTS = [
MagicMock(
time="2020-09-02T10:00:00Z",
category_id=4,
category_name="System Status",
type_id=16,
type_name="disarmed",
name="'user' disarmed 'partition'",
text="",
partition_id=0,
zone_id=None,
user_id=3,
group=None,
priority=2,
raw={},
),
MagicMock(
time="2020-09-02T09:00:00Z",
category_id=7,
category_name="Troubles",
type_id=36,
type_name="service needed",
name="Device Fault",
text="Service is needed.",
partition_id=None,
zone_id=None,
user_id=None,
group=None,
priority=1,
raw={},
),
MagicMock(
time="2020-09-02T08:00:00Z",
category_id=2,
category_name="Alarms",
type_id=3,
type_name="triggered",
name="Alarm is on",
text="Yes it is.",
partition_id=0,
zone_id=12,
user_id=None,
group=None,
priority=0,
raw={},
),
MagicMock(
time="2020-09-02T07:00:00Z",
category_id=4,
category_name="System Status",
type_id=119,
type_name="group arm",
name="You armed a group",
text="",
partition_id=0,
zone_id=None,
user_id=1,
group="C",
priority=2,
raw={},
),
MagicMock(
time="2020-09-02T06:00:00Z",
category_id=8,
category_name="Made up",
type_id=200,
type_name="also made up",
name="really made up",
text="",
partition_id=2,
zone_id=None,
user_id=1,
group=None,
priority=2,
raw={},
),
]
CATEGORIES_TO_EVENTS = {
"Alarm": 2,
"Status": 0,
"Trouble": 1,
"Other": 4,
}
@pytest.fixture
def emptry_alarm():
"""Fixture to mock an empty alarm."""
with patch(
"homeassistant.components.risco.RiscoAPI.get_state",
return_value=MagicMock(paritions={}, zones={}),
):
yield
async def test_cannot_connect(hass):
"""Test connection error."""
with patch(
"homeassistant.components.risco.RiscoAPI.login",
side_effect=CannotConnectError,
):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
registry = await hass.helpers.entity_registry.async_get_registry()
for id in ENTITY_IDS.values():
assert not registry.async_is_registered(id)
async def test_unauthorized(hass):
"""Test unauthorized error."""
with patch(
"homeassistant.components.risco.RiscoAPI.login",
side_effect=UnauthorizedError,
):
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
registry = await hass.helpers.entity_registry.async_get_registry()
for id in ENTITY_IDS.values():
assert not registry.async_is_registered(id)
def _check_state(hass, category, entity_id):
event = TEST_EVENTS[CATEGORIES_TO_EVENTS[category]]
assert hass.states.get(entity_id).state == event.time
assert hass.states.get(entity_id).attributes["category_id"] == event.category_id
assert hass.states.get(entity_id).attributes["category_name"] == event.category_name
assert hass.states.get(entity_id).attributes["type_id"] == event.type_id
assert hass.states.get(entity_id).attributes["type_name"] == event.type_name
assert hass.states.get(entity_id).attributes["name"] == event.name
assert hass.states.get(entity_id).attributes["text"] == event.text
assert hass.states.get(entity_id).attributes["partition_id"] == event.partition_id
assert hass.states.get(entity_id).attributes["zone_id"] == event.zone_id
assert hass.states.get(entity_id).attributes["user_id"] == event.user_id
assert hass.states.get(entity_id).attributes["group"] == event.group
assert hass.states.get(entity_id).attributes["priority"] == event.priority
assert hass.states.get(entity_id).attributes["raw"] == event.raw
async def test_setup(hass, emptry_alarm):
"""Test entity setup."""
registry = await hass.helpers.entity_registry.async_get_registry()
for id in ENTITY_IDS.values():
assert not registry.async_is_registered(id)
with patch(
"homeassistant.components.risco.RiscoAPI.get_events",
return_value=TEST_EVENTS,
), patch(
"homeassistant.components.risco.Store.async_save",
) as save_mock:
entry = await setup_risco(hass)
await hass.async_block_till_done()
save_mock.assert_awaited_once_with(
{LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time}
)
for id in ENTITY_IDS.values():
assert registry.async_is_registered(id)
for category, entity_id in ENTITY_IDS.items():
_check_state(hass, category, entity_id)
coordinator = hass.data[DOMAIN][entry.entry_id][EVENTS_COORDINATOR]
with patch(
"homeassistant.components.risco.RiscoAPI.get_events", return_value=[]
) as events_mock, patch(
"homeassistant.components.risco.Store.async_load",
return_value={LAST_EVENT_TIMESTAMP_KEY: TEST_EVENTS[0].time},
):
await coordinator.async_refresh()
await hass.async_block_till_done()
events_mock.assert_awaited_once_with(TEST_EVENTS[0].time, 10)
for category, entity_id in ENTITY_IDS.items():
_check_state(hass, category, entity_id)
+37
View File
@@ -0,0 +1,37 @@
"""Utilities for Risco tests."""
from homeassistant.components.risco.const import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_PIN, CONF_USERNAME
from tests.async_mock import PropertyMock, patch
from tests.common import MockConfigEntry
TEST_CONFIG = {
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
CONF_PIN: "1234",
}
TEST_SITE_UUID = "test-site-uuid"
TEST_SITE_NAME = "test-site-name"
async def setup_risco(hass, options={}):
"""Set up a Risco integration for testing."""
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG, options=options)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.risco.RiscoAPI.login",
return_value=True,
), patch(
"homeassistant.components.risco.RiscoAPI.site_uuid",
new_callable=PropertyMock(return_value=TEST_SITE_UUID),
), patch(
"homeassistant.components.risco.RiscoAPI.site_name",
new_callable=PropertyMock(return_value=TEST_SITE_NAME),
), patch(
"homeassistant.components.risco.RiscoAPI.close"
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry