Update rfxtrx library to handle connection retries (#111366)

Upgrade rfxtrx library to one that leaves reconnection to home assistant and handle loss of connection by reloading the integration.

https://github.com/Danielhiversen/pyRFXtrx/releases/tag/0.31.0
This commit is contained in:
Joakim Plate 2024-02-26 19:30:22 +01:00 committed by GitHub
parent 979fe57f7f
commit a55c56a207
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 200 additions and 134 deletions

View file

@ -1,10 +1,11 @@
"""Common test tools."""
from __future__ import annotations
from unittest.mock import patch
from unittest.mock import Mock, patch
from freezegun import freeze_time
import pytest
from RFXtrx import Connect, RFXtrxTransport
from homeassistant.components import rfxtrx
from homeassistant.components.rfxtrx import DOMAIN
@ -15,26 +16,42 @@ from tests.components.light.conftest import mock_light_profiles # noqa: F401
def create_rfx_test_cfg(
device="abcd", automatic_add=False, protocols=None, devices=None
device="abcd",
automatic_add=False,
protocols=None,
devices=None,
host=None,
port=None,
):
"""Create rfxtrx config entry data."""
return {
"device": device,
"host": None,
"port": None,
"host": host,
"port": port,
"automatic_add": automatic_add,
"protocols": protocols,
"debug": False,
"devices": devices,
"devices": devices or {},
}
async def setup_rfx_test_cfg(
hass, device="abcd", automatic_add=False, devices: dict[str, dict] | None = None
hass,
device="abcd",
automatic_add=False,
devices: dict[str, dict] | None = None,
protocols=None,
host=None,
port=None,
):
"""Construct a rfxtrx config entry."""
entry_data = create_rfx_test_cfg(
device=device, automatic_add=automatic_add, devices=devices
device=device,
automatic_add=automatic_add,
devices=devices,
protocols=protocols,
host=host,
port=port,
)
mock_entry = MockConfigEntry(domain="rfxtrx", unique_id=DOMAIN, data=entry_data)
mock_entry.supports_remove_device = True
@ -46,27 +63,50 @@ async def setup_rfx_test_cfg(
return mock_entry
@pytest.fixture(autouse=True)
async def transport_mock(hass):
"""Fixture that make sure all transports are fake."""
transport = Mock(spec=RFXtrxTransport)
with patch("RFXtrx.PySerialTransport", new=transport), patch(
"RFXtrx.PyNetworkTransport", transport
):
yield transport
@pytest.fixture(autouse=True)
async def connect_mock(hass):
"""Fixture that make sure connect class is mocked."""
with patch("RFXtrx.Connect") as connect:
yield connect
@pytest.fixture(autouse=True, name="rfxtrx")
async def rfxtrx_fixture(hass):
def rfxtrx_fixture(hass, connect_mock):
"""Fixture that cleans up threads from integration."""
with patch("RFXtrx.Connect") as connect, patch("RFXtrx.DummyTransport2"):
rfx = connect.return_value
rfx = Mock(spec=Connect)
async def _signal_event(packet_id):
event = rfxtrx.get_rfx_object(packet_id)
await hass.async_add_executor_job(
rfx.event_callback,
event,
)
def _init(transport, event_callback=None, modes=None):
rfx.event_callback = event_callback
rfx.transport = transport
return rfx
await hass.async_block_till_done()
await hass.async_block_till_done()
return event
connect_mock.side_effect = _init
rfx.signal = _signal_event
async def _signal_event(packet_id):
event = rfxtrx.get_rfx_object(packet_id)
await hass.async_add_executor_job(
rfx.event_callback,
event,
)
yield rfx
await hass.async_block_till_done()
await hass.async_block_till_done()
return event
rfx.signal = _signal_event
return rfx
@pytest.fixture(name="rfxtrx_automatic")