Rewrite mocking in devolo Home Control tests (#53011)

* Rework mocking

* Instantiate properties
pull/53085/head
Guido Schmitz 2021-07-16 11:40:08 +02:00 committed by GitHub
parent c6e1f8878d
commit 268c7ef768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 53 deletions

View File

@ -1,91 +1,119 @@
"""Mocks for tests.""" """Mocks for tests."""
from typing import Any
from unittest.mock import MagicMock from unittest.mock import MagicMock
from devolo_home_control_api.devices.zwave import Zwave
from devolo_home_control_api.homecontrol import HomeControl
from devolo_home_control_api.properties.binary_sensor_property import (
BinarySensorProperty,
)
from devolo_home_control_api.properties.settings_property import SettingsProperty
from devolo_home_control_api.publisher.publisher import Publisher from devolo_home_control_api.publisher.publisher import Publisher
class BinarySensorPropertyMock: class BinarySensorPropertyMock(BinarySensorProperty):
"""devolo Home Control binary sensor mock.""" """devolo Home Control binary sensor mock."""
element_uid = "Test" def __init__(self, **kwargs: Any) -> None:
key_count = 1 """Initialize the mock."""
sensor_type = "door" self._logger = MagicMock()
sub_type = "" self.element_uid = "Test"
state = False self.key_count = 1
self.sensor_type = "door"
self.sub_type = ""
self.state = False
class SettingsMock: class SettingsMock(SettingsProperty):
"""devolo Home Control settings mock.""" """devolo Home Control settings mock."""
name = "Test" def __init__(self, **kwargs: Any) -> None:
zone = "Test" """Initialize the mock."""
self._logger = MagicMock()
self.name = "Test"
self.zone = "Test"
class DeviceMock: class DeviceMock(Zwave):
"""devolo Home Control device mock.""" """devolo Home Control device mock."""
available = True def __init__(self) -> None:
brand = "devolo" """Initialize the mock."""
name = "Test Device" self.status = 0
uid = "Test" self.brand = "devolo"
settings_property = {"general_device_settings": SettingsMock()} self.name = "Test Device"
self.uid = "Test"
def is_online(self): self.settings_property = {"general_device_settings": SettingsMock()}
"""Mock online state of the device."""
return DeviceMock.available
class BinarySensorMock(DeviceMock): class BinarySensorMock(DeviceMock):
"""devolo Home Control binary sensor device mock.""" """devolo Home Control binary sensor device mock."""
binary_sensor_property = {"Test": BinarySensorPropertyMock()} def __init__(self) -> None:
"""Initialize the mock."""
super().__init__()
self.binary_sensor_property = {"Test": BinarySensorPropertyMock()}
class RemoteControlMock(DeviceMock): class RemoteControlMock(DeviceMock):
"""devolo Home Control remote control device mock.""" """devolo Home Control remote control device mock."""
remote_control_property = {"Test": BinarySensorPropertyMock()} def __init__(self) -> None:
"""Initialize the mock."""
super().__init__()
self.remote_control_property = {"Test": BinarySensorPropertyMock()}
class DisabledBinarySensorMock(DeviceMock): class DisabledBinarySensorMock(DeviceMock):
"""devolo Home Control disabled binary sensor device mock.""" """devolo Home Control disabled binary sensor device mock."""
binary_sensor_property = {"devolo.WarningBinaryFI:Test": BinarySensorPropertyMock()} def __init__(self) -> None:
"""Initialize the mock."""
super().__init__()
self.binary_sensor_property = {
"devolo.WarningBinaryFI:Test": BinarySensorPropertyMock()
}
class HomeControlMock: class HomeControlMock(HomeControl):
"""devolo Home Control gateway mock.""" """devolo Home Control gateway mock."""
binary_sensor_devices = [] def __init__(self, **kwargs: Any) -> None:
binary_switch_devices = [] """Initialize the mock."""
multi_level_sensor_devices = [] self.devices = {}
multi_level_switch_devices = [] self.publisher = MagicMock()
devices = {}
publisher = MagicMock()
def websocket_disconnect(self): def websocket_disconnect(self, event: str):
"""Mock disconnect of the websocket.""" """Mock disconnect of the websocket."""
pass
class HomeControlMockBinarySensor(HomeControlMock): class HomeControlMockBinarySensor(HomeControlMock):
"""devolo Home Control gateway mock with binary sensor device.""" """devolo Home Control gateway mock with binary sensor device."""
binary_sensor_devices = [BinarySensorMock()] def __init__(self, **kwargs: Any) -> None:
devices = {"Test": BinarySensorMock()} """Initialize the mock."""
publisher = Publisher(devices.keys()) super().__init__()
publisher.unregister = MagicMock() self.devices = {"Test": BinarySensorMock()}
self.publisher = Publisher(self.devices.keys())
self.publisher.unregister = MagicMock()
class HomeControlMockRemoteControl(HomeControlMock): class HomeControlMockRemoteControl(HomeControlMock):
"""devolo Home Control gateway mock with remote control device.""" """devolo Home Control gateway mock with remote control device."""
devices = {"Test": RemoteControlMock()} def __init__(self, **kwargs: Any) -> None:
publisher = Publisher(devices.keys()) """Initialize the mock."""
super().__init__()
self.devices = {"Test": RemoteControlMock()}
self.publisher = Publisher(self.devices.keys())
self.publisher.unregister = MagicMock()
class HomeControlMockDisabledBinarySensor(HomeControlMock): class HomeControlMockDisabledBinarySensor(HomeControlMock):
"""devolo Home Control gateway mock with disabled device.""" """devolo Home Control gateway mock with disabled device."""
binary_sensor_devices = [DisabledBinarySensorMock()] def __init__(self, **kwargs: Any) -> None:
"""Initialize the mock."""
super().__init__()
self.devices = {"Test": DisabledBinarySensorMock()}

View File

@ -9,7 +9,6 @@ from homeassistant.core import HomeAssistant
from . import configure_integration from . import configure_integration
from .mocks import ( from .mocks import (
DeviceMock,
HomeControlMock, HomeControlMock,
HomeControlMockBinarySensor, HomeControlMockBinarySensor,
HomeControlMockDisabledBinarySensor, HomeControlMockDisabledBinarySensor,
@ -21,10 +20,11 @@ from .mocks import (
async def test_binary_sensor(hass: HomeAssistant): async def test_binary_sensor(hass: HomeAssistant):
"""Test setup and state change of a binary sensor device.""" """Test setup and state change of a binary sensor device."""
entry = configure_integration(hass) entry = configure_integration(hass)
DeviceMock.available = True test_gateway = HomeControlMockBinarySensor()
test_gateway.devices["Test"].status = 0
with patch( with patch(
"homeassistant.components.devolo_home_control.HomeControl", "homeassistant.components.devolo_home_control.HomeControl",
side_effect=[HomeControlMockBinarySensor, HomeControlMock], side_effect=[test_gateway, HomeControlMock()],
): ):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -34,13 +34,13 @@ async def test_binary_sensor(hass: HomeAssistant):
assert state.state == STATE_OFF assert state.state == STATE_OFF
# Emulate websocket message: sensor turned on # Emulate websocket message: sensor turned on
HomeControlMockBinarySensor.publisher.dispatch("Test", ("Test", True)) test_gateway.publisher.dispatch("Test", ("Test", True))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON
# Emulate websocket message: device went offline # Emulate websocket message: device went offline
DeviceMock.available = False test_gateway.devices["Test"].status = 1
HomeControlMockBinarySensor.publisher.dispatch("Test", ("Status", False, "status")) test_gateway.publisher.dispatch("Test", ("Status", False, "status"))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE
@ -49,10 +49,11 @@ async def test_binary_sensor(hass: HomeAssistant):
async def test_remote_control(hass: HomeAssistant): async def test_remote_control(hass: HomeAssistant):
"""Test setup and state change of a remote control device.""" """Test setup and state change of a remote control device."""
entry = configure_integration(hass) entry = configure_integration(hass)
DeviceMock.available = True test_gateway = HomeControlMockRemoteControl()
test_gateway.devices["Test"].status = 0
with patch( with patch(
"homeassistant.components.devolo_home_control.HomeControl", "homeassistant.components.devolo_home_control.HomeControl",
side_effect=[HomeControlMockRemoteControl, HomeControlMock], side_effect=[test_gateway, HomeControlMock()],
): ):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -62,18 +63,18 @@ async def test_remote_control(hass: HomeAssistant):
assert state.state == STATE_OFF assert state.state == STATE_OFF
# Emulate websocket message: button pressed # Emulate websocket message: button pressed
HomeControlMockRemoteControl.publisher.dispatch("Test", ("Test", 1)) test_gateway.publisher.dispatch("Test", ("Test", 1))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON
# Emulate websocket message: button released # Emulate websocket message: button released
HomeControlMockRemoteControl.publisher.dispatch("Test", ("Test", 0)) test_gateway.publisher.dispatch("Test", ("Test", 0))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(f"{DOMAIN}.test").state == STATE_OFF assert hass.states.get(f"{DOMAIN}.test").state == STATE_OFF
# Emulate websocket message: device went offline # Emulate websocket message: device went offline
DeviceMock.available = False test_gateway.devices["Test"].status = 1
HomeControlMockRemoteControl.publisher.dispatch("Test", ("Status", False, "status")) test_gateway.publisher.dispatch("Test", ("Status", False, "status"))
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE
@ -84,7 +85,7 @@ async def test_disabled(hass: HomeAssistant):
entry = configure_integration(hass) entry = configure_integration(hass)
with patch( with patch(
"homeassistant.components.devolo_home_control.HomeControl", "homeassistant.components.devolo_home_control.HomeControl",
side_effect=[HomeControlMockDisabledBinarySensor, HomeControlMock], side_effect=[HomeControlMockDisabledBinarySensor(), HomeControlMock()],
): ):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -96,9 +97,10 @@ async def test_disabled(hass: HomeAssistant):
async def test_remove_from_hass(hass: HomeAssistant): async def test_remove_from_hass(hass: HomeAssistant):
"""Test removing entity.""" """Test removing entity."""
entry = configure_integration(hass) entry = configure_integration(hass)
test_gateway = HomeControlMockBinarySensor()
with patch( with patch(
"homeassistant.components.devolo_home_control.HomeControl", "homeassistant.components.devolo_home_control.HomeControl",
side_effect=[HomeControlMockBinarySensor, HomeControlMock], side_effect=[test_gateway, HomeControlMock()],
): ):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -109,4 +111,4 @@ async def test_remove_from_hass(hass: HomeAssistant):
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0 assert len(hass.states.async_all()) == 0
HomeControlMockBinarySensor.publisher.unregister.assert_called_once() test_gateway.publisher.unregister.assert_called_once()