Prevent connecting to a Shelly device that is already connected (#122105)
This commit is contained in:
parent
cf0aef079b
commit
d2cc25cee6
8 changed files with 31 additions and 0 deletions
|
@ -669,6 +669,9 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]):
|
||||||
"""Handle device update."""
|
"""Handle device update."""
|
||||||
LOGGER.debug("Shelly %s handle update, type: %s", self.name, update_type)
|
LOGGER.debug("Shelly %s handle update, type: %s", self.name, update_type)
|
||||||
if update_type is RpcUpdateType.ONLINE:
|
if update_type is RpcUpdateType.ONLINE:
|
||||||
|
if self.device.connected:
|
||||||
|
LOGGER.debug("Device %s already connected", self.name)
|
||||||
|
return
|
||||||
self.entry.async_create_background_task(
|
self.entry.async_create_background_task(
|
||||||
self.hass,
|
self.hass,
|
||||||
self._async_device_connect_task(),
|
self._async_device_connect_task(),
|
||||||
|
|
|
@ -353,6 +353,7 @@ def _mock_rpc_device(version: str | None = None):
|
||||||
status=MOCK_STATUS_RPC,
|
status=MOCK_STATUS_RPC,
|
||||||
firmware_version="some fw string",
|
firmware_version="some fw string",
|
||||||
initialized=True,
|
initialized=True,
|
||||||
|
connected=True,
|
||||||
)
|
)
|
||||||
type(device).name = PropertyMock(return_value="Test name")
|
type(device).name = PropertyMock(return_value="Test name")
|
||||||
return device
|
return device
|
||||||
|
|
|
@ -265,6 +265,7 @@ async def test_rpc_sleeping_binary_sensor(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RPC online sleeping binary sensor."""
|
"""Test RPC online sleeping binary sensor."""
|
||||||
entity_id = f"{BINARY_SENSOR_DOMAIN}.test_name_cloud"
|
entity_id = f"{BINARY_SENSOR_DOMAIN}.test_name_cloud"
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
||||||
config_entry = await init_integration(hass, 2, sleep_period=1000)
|
config_entry = await init_integration(hass, 2, sleep_period=1000)
|
||||||
|
|
||||||
|
|
|
@ -1114,6 +1114,7 @@ async def test_zeroconf_sleeping_device_not_triggers_refresh(
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test zeroconf discovery does not triggers refresh for sleeping device."""
|
"""Test zeroconf discovery does not triggers refresh for sleeping device."""
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain="shelly",
|
domain="shelly",
|
||||||
|
|
|
@ -545,6 +545,7 @@ async def test_rpc_update_entry_sleep_period(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RPC update entry sleep period."""
|
"""Test RPC update entry sleep period."""
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 600)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 600)
|
||||||
entry = await init_integration(hass, 2, sleep_period=600)
|
entry = await init_integration(hass, 2, sleep_period=600)
|
||||||
register_entity(
|
register_entity(
|
||||||
|
@ -578,6 +579,7 @@ async def test_rpc_sleeping_device_no_periodic_updates(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RPC sleeping device no periodic updates."""
|
"""Test RPC sleeping device no periodic updates."""
|
||||||
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
|
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
||||||
entry = await init_integration(hass, 2, sleep_period=1000)
|
entry = await init_integration(hass, 2, sleep_period=1000)
|
||||||
register_entity(
|
register_entity(
|
||||||
|
@ -609,6 +611,7 @@ async def test_rpc_sleeping_device_firmware_unsupported(
|
||||||
issue_registry: ir.IssueRegistry,
|
issue_registry: ir.IssueRegistry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RPC sleeping device firmware not supported."""
|
"""Test RPC sleeping device firmware not supported."""
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setattr(mock_rpc_device, "firmware_supported", False)
|
monkeypatch.setattr(mock_rpc_device, "firmware_supported", False)
|
||||||
entry = await init_integration(hass, 2, sleep_period=3600)
|
entry = await init_integration(hass, 2, sleep_period=3600)
|
||||||
|
|
||||||
|
@ -912,6 +915,7 @@ async def test_rpc_sleeping_device_connection_error(
|
||||||
hass, BINARY_SENSOR_DOMAIN, "test_name_cloud", "cloud-cloud", entry
|
hass, BINARY_SENSOR_DOMAIN, "test_name_cloud", "cloud-cloud", entry
|
||||||
)
|
)
|
||||||
mock_restore_cache(hass, [State(entity_id, STATE_ON)])
|
mock_restore_cache(hass, [State(entity_id, STATE_ON)])
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setattr(mock_rpc_device, "initialized", False)
|
monkeypatch.setattr(mock_rpc_device, "initialized", False)
|
||||||
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()
|
||||||
|
@ -939,3 +943,19 @@ async def test_rpc_sleeping_device_connection_error(
|
||||||
|
|
||||||
assert "Sleeping device did not update" in caplog.text
|
assert "Sleeping device did not update" in caplog.text
|
||||||
assert get_entity_state(hass, entity_id) == STATE_UNAVAILABLE
|
assert get_entity_state(hass, entity_id) == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
|
async def test_rpc_already_connected(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_rpc_device: Mock,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test RPC ignore connect event if already connected."""
|
||||||
|
await init_integration(hass, 2)
|
||||||
|
|
||||||
|
mock_rpc_device.mock_online()
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
assert "already connected" in caplog.text
|
||||||
|
mock_rpc_device.initialize.assert_called_once()
|
||||||
|
|
|
@ -279,6 +279,7 @@ async def test_sleeping_rpc_device_online(
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test sleeping RPC device online."""
|
"""Test sleeping RPC device online."""
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", device_sleep)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", device_sleep)
|
||||||
entry = await init_integration(hass, 2, sleep_period=entry_sleep)
|
entry = await init_integration(hass, 2, sleep_period=entry_sleep)
|
||||||
assert "will resume when device is online" in caplog.text
|
assert "will resume when device is online" in caplog.text
|
||||||
|
@ -297,6 +298,7 @@ async def test_sleeping_rpc_device_online_new_firmware(
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test sleeping device Gen2 with firmware 1.0.0 or later."""
|
"""Test sleeping device Gen2 with firmware 1.0.0 or later."""
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
entry = await init_integration(hass, 2, sleep_period=None)
|
entry = await init_integration(hass, 2, sleep_period=None)
|
||||||
assert "will resume when device is online" in caplog.text
|
assert "will resume when device is online" in caplog.text
|
||||||
|
|
||||||
|
|
|
@ -450,6 +450,7 @@ async def test_rpc_sleeping_sensor(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RPC online sleeping sensor."""
|
"""Test RPC online sleeping sensor."""
|
||||||
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
|
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
||||||
entry = await init_integration(hass, 2, sleep_period=1000)
|
entry = await init_integration(hass, 2, sleep_period=1000)
|
||||||
|
|
||||||
|
@ -601,6 +602,7 @@ async def test_rpc_sleeping_update_entity_service(
|
||||||
await async_setup_component(hass, "homeassistant", {})
|
await async_setup_component(hass, "homeassistant", {})
|
||||||
|
|
||||||
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
|
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
||||||
await init_integration(hass, 2, sleep_period=1000)
|
await init_integration(hass, 2, sleep_period=1000)
|
||||||
|
|
||||||
|
|
|
@ -334,6 +334,7 @@ async def test_rpc_sleeping_update(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test RPC sleeping device update entity."""
|
"""Test RPC sleeping device update entity."""
|
||||||
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
|
||||||
monkeypatch.setitem(mock_rpc_device.shelly, "ver", "1")
|
monkeypatch.setitem(mock_rpc_device.shelly, "ver", "1")
|
||||||
monkeypatch.setitem(
|
monkeypatch.setitem(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue