Rewrite mocking in devolo Home Control tests (#53011)
* Rework mocking * Instantiate propertiespull/53085/head
parent
c6e1f8878d
commit
268c7ef768
|
@ -1,91 +1,119 @@
|
|||
"""Mocks for tests."""
|
||||
|
||||
from typing import Any
|
||||
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
|
||||
|
||||
|
||||
class BinarySensorPropertyMock:
|
||||
class BinarySensorPropertyMock(BinarySensorProperty):
|
||||
"""devolo Home Control binary sensor mock."""
|
||||
|
||||
element_uid = "Test"
|
||||
key_count = 1
|
||||
sensor_type = "door"
|
||||
sub_type = ""
|
||||
state = False
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
"""Initialize the mock."""
|
||||
self._logger = MagicMock()
|
||||
self.element_uid = "Test"
|
||||
self.key_count = 1
|
||||
self.sensor_type = "door"
|
||||
self.sub_type = ""
|
||||
self.state = False
|
||||
|
||||
|
||||
class SettingsMock:
|
||||
class SettingsMock(SettingsProperty):
|
||||
"""devolo Home Control settings mock."""
|
||||
|
||||
name = "Test"
|
||||
zone = "Test"
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
"""Initialize the mock."""
|
||||
self._logger = MagicMock()
|
||||
self.name = "Test"
|
||||
self.zone = "Test"
|
||||
|
||||
|
||||
class DeviceMock:
|
||||
class DeviceMock(Zwave):
|
||||
"""devolo Home Control device mock."""
|
||||
|
||||
available = True
|
||||
brand = "devolo"
|
||||
name = "Test Device"
|
||||
uid = "Test"
|
||||
settings_property = {"general_device_settings": SettingsMock()}
|
||||
|
||||
def is_online(self):
|
||||
"""Mock online state of the device."""
|
||||
return DeviceMock.available
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the mock."""
|
||||
self.status = 0
|
||||
self.brand = "devolo"
|
||||
self.name = "Test Device"
|
||||
self.uid = "Test"
|
||||
self.settings_property = {"general_device_settings": SettingsMock()}
|
||||
|
||||
|
||||
class BinarySensorMock(DeviceMock):
|
||||
"""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):
|
||||
"""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):
|
||||
"""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."""
|
||||
|
||||
binary_sensor_devices = []
|
||||
binary_switch_devices = []
|
||||
multi_level_sensor_devices = []
|
||||
multi_level_switch_devices = []
|
||||
devices = {}
|
||||
publisher = MagicMock()
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
"""Initialize the mock."""
|
||||
self.devices = {}
|
||||
self.publisher = MagicMock()
|
||||
|
||||
def websocket_disconnect(self):
|
||||
def websocket_disconnect(self, event: str):
|
||||
"""Mock disconnect of the websocket."""
|
||||
pass
|
||||
|
||||
|
||||
class HomeControlMockBinarySensor(HomeControlMock):
|
||||
"""devolo Home Control gateway mock with binary sensor device."""
|
||||
|
||||
binary_sensor_devices = [BinarySensorMock()]
|
||||
devices = {"Test": BinarySensorMock()}
|
||||
publisher = Publisher(devices.keys())
|
||||
publisher.unregister = MagicMock()
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
"""Initialize the mock."""
|
||||
super().__init__()
|
||||
self.devices = {"Test": BinarySensorMock()}
|
||||
self.publisher = Publisher(self.devices.keys())
|
||||
self.publisher.unregister = MagicMock()
|
||||
|
||||
|
||||
class HomeControlMockRemoteControl(HomeControlMock):
|
||||
"""devolo Home Control gateway mock with remote control device."""
|
||||
|
||||
devices = {"Test": RemoteControlMock()}
|
||||
publisher = Publisher(devices.keys())
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
"""Initialize the mock."""
|
||||
super().__init__()
|
||||
self.devices = {"Test": RemoteControlMock()}
|
||||
self.publisher = Publisher(self.devices.keys())
|
||||
self.publisher.unregister = MagicMock()
|
||||
|
||||
|
||||
class HomeControlMockDisabledBinarySensor(HomeControlMock):
|
||||
"""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()}
|
||||
|
|
|
@ -9,7 +9,6 @@ from homeassistant.core import HomeAssistant
|
|||
|
||||
from . import configure_integration
|
||||
from .mocks import (
|
||||
DeviceMock,
|
||||
HomeControlMock,
|
||||
HomeControlMockBinarySensor,
|
||||
HomeControlMockDisabledBinarySensor,
|
||||
|
@ -21,10 +20,11 @@ from .mocks import (
|
|||
async def test_binary_sensor(hass: HomeAssistant):
|
||||
"""Test setup and state change of a binary sensor device."""
|
||||
entry = configure_integration(hass)
|
||||
DeviceMock.available = True
|
||||
test_gateway = HomeControlMockBinarySensor()
|
||||
test_gateway.devices["Test"].status = 0
|
||||
with patch(
|
||||
"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.async_block_till_done()
|
||||
|
@ -34,13 +34,13 @@ async def test_binary_sensor(hass: HomeAssistant):
|
|||
assert state.state == STATE_OFF
|
||||
|
||||
# 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()
|
||||
assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON
|
||||
|
||||
# Emulate websocket message: device went offline
|
||||
DeviceMock.available = False
|
||||
HomeControlMockBinarySensor.publisher.dispatch("Test", ("Status", False, "status"))
|
||||
test_gateway.devices["Test"].status = 1
|
||||
test_gateway.publisher.dispatch("Test", ("Status", False, "status"))
|
||||
await hass.async_block_till_done()
|
||||
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):
|
||||
"""Test setup and state change of a remote control device."""
|
||||
entry = configure_integration(hass)
|
||||
DeviceMock.available = True
|
||||
test_gateway = HomeControlMockRemoteControl()
|
||||
test_gateway.devices["Test"].status = 0
|
||||
with patch(
|
||||
"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.async_block_till_done()
|
||||
|
@ -62,18 +63,18 @@ async def test_remote_control(hass: HomeAssistant):
|
|||
assert state.state == STATE_OFF
|
||||
|
||||
# Emulate websocket message: button pressed
|
||||
HomeControlMockRemoteControl.publisher.dispatch("Test", ("Test", 1))
|
||||
test_gateway.publisher.dispatch("Test", ("Test", 1))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON
|
||||
|
||||
# Emulate websocket message: button released
|
||||
HomeControlMockRemoteControl.publisher.dispatch("Test", ("Test", 0))
|
||||
test_gateway.publisher.dispatch("Test", ("Test", 0))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(f"{DOMAIN}.test").state == STATE_OFF
|
||||
|
||||
# Emulate websocket message: device went offline
|
||||
DeviceMock.available = False
|
||||
HomeControlMockRemoteControl.publisher.dispatch("Test", ("Status", False, "status"))
|
||||
test_gateway.devices["Test"].status = 1
|
||||
test_gateway.publisher.dispatch("Test", ("Status", False, "status"))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE
|
||||
|
||||
|
@ -84,7 +85,7 @@ async def test_disabled(hass: HomeAssistant):
|
|||
entry = configure_integration(hass)
|
||||
with patch(
|
||||
"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.async_block_till_done()
|
||||
|
@ -96,9 +97,10 @@ async def test_disabled(hass: HomeAssistant):
|
|||
async def test_remove_from_hass(hass: HomeAssistant):
|
||||
"""Test removing entity."""
|
||||
entry = configure_integration(hass)
|
||||
test_gateway = HomeControlMockBinarySensor()
|
||||
with patch(
|
||||
"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.async_block_till_done()
|
||||
|
@ -109,4 +111,4 @@ async def test_remove_from_hass(hass: HomeAssistant):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
HomeControlMockBinarySensor.publisher.unregister.assert_called_once()
|
||||
test_gateway.publisher.unregister.assert_called_once()
|
||||
|
|
Loading…
Reference in New Issue