Add test for adding a device to HomematicIP Cloud (#31224)

* Add test for adding a hmip device

* refactor get_mock_hap to use config_entry setup

* remove unused parameters
This commit is contained in:
SukramJ 2020-01-28 08:44:30 +01:00 committed by Martin Hjelmare
parent 9d8b4de09c
commit ec2d378a19
3 changed files with 66 additions and 22 deletions

View File

@ -17,7 +17,9 @@ from homeassistant.components.homematicip_cloud.const import (
HMIPC_PIN,
)
from homeassistant.components.homematicip_cloud.hap import HomematicipHAP
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.setup import async_setup_component
from .helper import AUTH_TOKEN, HAPID, HAPPIN, HomeTemplate
@ -56,7 +58,7 @@ def hmip_config_entry_fixture() -> config_entries.ConfigEntry:
title=HAPID,
unique_id=HAPID,
data=entry_data,
source="import",
source=SOURCE_IMPORT,
connection_class=config_entries.CONN_CLASS_CLOUD_PUSH,
system_options={"disable_new_entities": False},
)
@ -84,23 +86,25 @@ async def get_mock_hap(
hmip_config_entry: config_entries.ConfigEntry,
) -> HomematicipHAP:
"""Create a mocked homematic access point."""
hass.config.components.add(HMIPC_DOMAIN)
hap = HomematicipHAP(hass, hmip_config_entry)
home_name = hmip_config_entry.data["name"]
mock_home = (
HomeTemplate(connection=mock_connection, home_name=home_name)
.init_home()
.get_async_home_mock()
)
with patch.object(hap, "get_hap", return_value=mock_home):
assert await hap.async_setup()
mock_home.on_update(hap.async_update)
mock_home.on_create(hap.async_create_entity)
hass.data[HMIPC_DOMAIN] = {HAPID: hap}
hmip_config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipHAP.get_hap",
return_value=mock_home,
):
assert await async_setup_component(hass, HMIPC_DOMAIN, {}) is True
await hass.async_block_till_done()
hap = hass.data[HMIPC_DOMAIN][HAPID]
mock_home.on_update(hap.async_update)
mock_home.on_create(hap.async_create_entity)
return hap

View File

@ -1,9 +1,14 @@
"""Common tests for HomematicIP devices."""
from asynctest import patch
from homematicip.base.enums import EventType
from homeassistant.components.homematicip_cloud import DOMAIN as HMIPC_DOMAIN
from homeassistant.components.homematicip_cloud.hap import HomematicipHAP
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE
from homeassistant.helpers import device_registry as dr, entity_registry as er
from .conftest import get_mock_hap
from .helper import async_manipulate_test_data, get_and_check_entity_basics
from .helper import HAPID, async_manipulate_test_data, get_and_check_entity_basics
async def test_hmip_remove_device(hass, default_mock_hap):
@ -35,6 +40,51 @@ async def test_hmip_remove_device(hass, default_mock_hap):
assert len(default_mock_hap.hmip_device_by_entity_id) == pre_mapping_count - 3
async def test_hmip_add_device(hass, default_mock_hap, hmip_config_entry):
"""Test Remove of hmip device."""
entity_id = "light.treppe"
entity_name = "Treppe"
device_model = "HmIP-BSL"
ha_state, hmip_device = get_and_check_entity_basics(
hass, default_mock_hap, entity_id, entity_name, device_model
)
assert ha_state.state == STATE_ON
assert hmip_device
device_registry = await dr.async_get_registry(hass)
entity_registry = await er.async_get_registry(hass)
pre_device_count = len(device_registry.devices)
pre_entity_count = len(entity_registry.entities)
pre_mapping_count = len(default_mock_hap.hmip_device_by_entity_id)
hmip_device.fire_remove_event()
await hass.async_block_till_done()
assert len(device_registry.devices) == pre_device_count - 1
assert len(entity_registry.entities) == pre_entity_count - 3
assert len(default_mock_hap.hmip_device_by_entity_id) == pre_mapping_count - 3
reloaded_hap = HomematicipHAP(hass, hmip_config_entry)
with patch(
"homeassistant.components.homematicip_cloud.HomematicipHAP",
return_value=reloaded_hap,
), patch.object(reloaded_hap, "async_connect"), patch.object(
reloaded_hap, "get_hap", return_value=default_mock_hap.home
), patch(
"homeassistant.components.homematicip_cloud.hap.asyncio.sleep"
):
default_mock_hap.home.fire_create_event(event_type=EventType.DEVICE_ADDED)
await hass.async_block_till_done()
assert len(device_registry.devices) == pre_device_count
assert len(entity_registry.entities) == pre_entity_count
new_hap = hass.data[HMIPC_DOMAIN][HAPID]
assert len(new_hap.hmip_device_by_entity_id) == pre_mapping_count
async def test_hmip_remove_group(hass, default_mock_hap):
"""Test Remove of hmip group."""
entity_id = "switch.strom_group"
@ -56,7 +106,6 @@ async def test_hmip_remove_group(hass, default_mock_hap):
pre_mapping_count = len(default_mock_hap.hmip_device_by_entity_id)
hmip_device.fire_remove_event()
await hass.async_block_till_done()
assert len(device_registry.devices) == pre_device_count

View File

@ -17,13 +17,11 @@ from homeassistant.components.homematicip_cloud.hap import (
HomematicipAuth,
HomematicipHAP,
)
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
from homeassistant.config_entries import ENTRY_STATE_NOT_LOADED
from homeassistant.exceptions import ConfigEntryNotReady
from .helper import HAPID, HAPPIN
from tests.common import MockConfigEntry
async def test_auth_setup(hass):
"""Test auth setup for client registration."""
@ -72,7 +70,7 @@ async def test_auth_auth_check_and_register_with_exception(hass):
assert await hmip_auth.async_register() is False
async def test_hap_setup_works(aioclient_mock):
async def test_hap_setup_works():
"""Test a successful setup of a accesspoint."""
hass = Mock()
entry = Mock()
@ -109,15 +107,8 @@ async def test_hap_setup_connection_error():
assert not hass.config_entries.flow.async_init.mock_calls
async def test_hap_reset_unloads_entry_if_setup(hass, default_mock_hap, hmip_config):
async def test_hap_reset_unloads_entry_if_setup(hass, default_mock_hap):
"""Test calling reset while the entry has been setup."""
MockConfigEntry(
domain=HMIPC_DOMAIN,
unique_id=HAPID,
data=hmip_config[HMIPC_DOMAIN][0],
state=ENTRY_STATE_LOADED,
).add_to_hass(hass)
assert hass.data[HMIPC_DOMAIN][HAPID] == default_mock_hap
config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
assert len(config_entries) == 1