Refactor Harmony tests to better follow Home Assistant conventions (#47141)

This commit is contained in:
Mike Keesey 2021-03-10 11:19:04 -07:00 committed by GitHub
parent 7c8851264f
commit 78c974d527
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 199 additions and 148 deletions

View file

@ -37,10 +37,10 @@ IDS_TO_DEVICES = {
class FakeHarmonyClient:
"""FakeHarmonyClient to mock away network calls."""
def __init__(
def initialize(
self, ip_address: str = "", callbacks: ClientCallbackType = MagicMock()
):
"""Initialize FakeHarmonyClient class."""
"""Initialize FakeHarmonyClient class to capture callbacks."""
self._activity_name = "Watch TV"
self.close = AsyncMock()
self.send_commands = AsyncMock()
@ -49,6 +49,8 @@ class FakeHarmonyClient:
self._callbacks = callbacks
self.fw_version = "123.456"
return self
async def connect(self):
"""Connect and call the appropriate callbacks."""
self._callbacks.connect(None)
@ -130,13 +132,28 @@ class FakeHarmonyClient:
)
return config
def mock_reconnection(self):
"""Simulate reconnection to the hub."""
self._callbacks.connect(None)
def mock_disconnection(self):
"""Simulate disconnection to the hub."""
self._callbacks.disconnect(None)
@pytest.fixture()
def mock_hc():
"""Create a mock HarmonyClient."""
def harmony_client():
"""Create the FakeHarmonyClient instance."""
return FakeHarmonyClient()
@pytest.fixture()
def mock_hc(harmony_client):
"""Patch the real HarmonyClient with initialization side effect."""
with patch(
"homeassistant.components.harmony.data.HarmonyClient",
side_effect=FakeHarmonyClient,
side_effect=harmony_client.initialize,
) as fake:
yield fake