Bump aioshelly to 9.0.0 (#114025)

* Update Shelly to use initialize from aioshelly

* Save indentation in _async_device_connect

* Use firmware_supported property from aioshelly

* Review comments

* Bump aioshelly

* Fix lint errors

* Test RPC initialized update
This commit is contained in:
Shay Levy
2024-04-14 18:07:26 +03:00
committed by GitHub
parent 33412dd9f6
commit 291df6dafe
19 changed files with 349 additions and 257 deletions

View File

@@ -8,7 +8,6 @@ from aioshelly.common import ConnectionOptions
from aioshelly.const import MODEL_PLUS_2PM
from aioshelly.exceptions import (
DeviceConnectionError,
FirmwareUnsupported,
InvalidAuthError,
MacAddressMismatchError,
)
@@ -27,6 +26,7 @@ from homeassistant.components.shelly.const import (
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.device_registry import (
CONNECTION_NETWORK_MAC,
DeviceRegistry,
@@ -145,27 +145,46 @@ async def test_setup_entry_not_shelly(
@pytest.mark.parametrize("gen", [1, 2, 3])
@pytest.mark.parametrize("side_effect", [DeviceConnectionError, FirmwareUnsupported])
async def test_device_connection_error(
hass: HomeAssistant,
gen: int,
side_effect: Exception,
mock_block_device: Mock,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test device connection error."""
monkeypatch.setattr(
mock_block_device, "initialize", AsyncMock(side_effect=side_effect)
mock_block_device, "initialize", AsyncMock(side_effect=DeviceConnectionError)
)
monkeypatch.setattr(
mock_rpc_device, "initialize", AsyncMock(side_effect=side_effect)
mock_rpc_device, "initialize", AsyncMock(side_effect=DeviceConnectionError)
)
entry = await init_integration(hass, gen)
assert entry.state is ConfigEntryState.SETUP_RETRY
@pytest.mark.parametrize("gen", [1, 2, 3])
async def test_device_unsupported_firmware(
hass: HomeAssistant,
gen: int,
mock_block_device: Mock,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test device init with unsupported firmware."""
monkeypatch.setattr(mock_block_device, "firmware_supported", False)
monkeypatch.setattr(mock_rpc_device, "firmware_supported", False)
entry = await init_integration(hass, gen)
assert entry.state is ConfigEntryState.SETUP_RETRY
assert (
DOMAIN,
"firmware_unsupported_123456789ABC",
) in issue_registry.issues
@pytest.mark.parametrize("gen", [1, 2, 3])
async def test_mac_mismatch_error(
hass: HomeAssistant,
@@ -217,12 +236,13 @@ async def test_device_auth_error(
assert flow["context"].get("entry_id") == entry.entry_id
@pytest.mark.parametrize(("entry_sleep", "device_sleep"), [(None, 0), (1000, 1000)])
@pytest.mark.parametrize(("entry_sleep", "device_sleep"), [(None, 0), (3600, 3600)])
async def test_sleeping_block_device_online(
hass: HomeAssistant,
entry_sleep: int | None,
device_sleep: int,
mock_block_device: Mock,
monkeypatch: pytest.MonkeyPatch,
device_reg: DeviceRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
@@ -234,10 +254,17 @@ async def test_sleeping_block_device_online(
connections={(CONNECTION_NETWORK_MAC, format_mac(MOCK_MAC))},
)
monkeypatch.setitem(
mock_block_device.settings,
"sleep_mode",
{"period": int(device_sleep / 60), "unit": "m"},
)
entry = await init_integration(hass, 1, sleep_period=entry_sleep)
assert "will resume when device is online" in caplog.text
mock_block_device.mock_update()
mock_block_device.mock_online()
await hass.async_block_till_done()
assert "online, resuming setup" in caplog.text
assert entry.data["sleep_period"] == device_sleep
@@ -248,13 +275,17 @@ async def test_sleeping_rpc_device_online(
entry_sleep: int | None,
device_sleep: int,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test sleeping RPC device online."""
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", device_sleep)
entry = await init_integration(hass, 2, sleep_period=entry_sleep)
assert "will resume when device is online" in caplog.text
mock_rpc_device.mock_update()
mock_rpc_device.mock_online()
await hass.async_block_till_done()
assert "online, resuming setup" in caplog.text
assert entry.data["sleep_period"] == device_sleep
@@ -270,7 +301,9 @@ async def test_sleeping_rpc_device_online_new_firmware(
assert "will resume when device is online" in caplog.text
mutate_rpc_device_status(monkeypatch, mock_rpc_device, "sys", "wakeup_period", 1500)
mock_rpc_device.mock_update()
mock_rpc_device.mock_online()
await hass.async_block_till_done()
assert "online, resuming setup" in caplog.text
assert entry.data["sleep_period"] == 1500
@@ -413,9 +446,12 @@ async def test_entry_missing_port(hass: HomeAssistant) -> None:
}
entry = MockConfigEntry(domain=DOMAIN, data=data, unique_id=MOCK_MAC)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.shelly.RpcDevice.create", return_value=Mock()
) as rpc_device_mock:
with (
patch("homeassistant.components.shelly.RpcDevice.initialize"),
patch(
"homeassistant.components.shelly.RpcDevice.create", return_value=Mock()
) as rpc_device_mock,
):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
@@ -435,9 +471,12 @@ async def test_rpc_entry_custom_port(hass: HomeAssistant) -> None:
}
entry = MockConfigEntry(domain=DOMAIN, data=data, unique_id=MOCK_MAC)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.shelly.RpcDevice.create", return_value=Mock()
) as rpc_device_mock:
with (
patch("homeassistant.components.shelly.RpcDevice.initialize"),
patch(
"homeassistant.components.shelly.RpcDevice.create", return_value=Mock()
) as rpc_device_mock,
):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()