Cleanup add_to_hass method in Shelly tests (part 1) (#140075)

This commit is contained in:
Simone Chemelli 2025-03-07 23:04:49 +01:00 committed by GitHub
parent aa556d8678
commit 99b5adaef1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 38 deletions

View File

@ -40,13 +40,15 @@ async def init_integration(
sleep_period=0,
options: dict[str, Any] | None = None,
skip_setup: bool = False,
data: dict[str, Any] | None = None,
) -> MockConfigEntry:
"""Set up the Shelly integration in Home Assistant."""
data = {
CONF_HOST: "192.168.1.37",
CONF_SLEEP_PERIOD: sleep_period,
"model": model,
}
if data is None:
data = {
CONF_HOST: "192.168.1.37",
CONF_SLEEP_PERIOD: sleep_period,
"model": model,
}
if gen is not None:
data[CONF_GEN] = gen

View File

@ -25,7 +25,7 @@ from homeassistant.setup import async_setup_component
from . import init_integration
from tests.common import MockConfigEntry, async_get_device_automations
from tests.common import async_get_device_automations
@pytest.mark.parametrize(
@ -162,8 +162,7 @@ async def test_get_triggers_for_invalid_device_id(
) -> None:
"""Test error raised for invalid shelly device_id."""
await init_integration(hass, 1)
config_entry = MockConfigEntry(domain=DOMAIN, data={})
config_entry.add_to_hass(hass)
config_entry = await init_integration(hass, 1, data={}, skip_setup=True)
invalid_device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},

View File

@ -27,16 +27,10 @@ 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,
format_mac,
)
from homeassistant.helpers.device_registry import DeviceRegistry
from homeassistant.setup import async_setup_component
from . import MOCK_MAC, init_integration, mutate_rpc_device_status
from tests.common import MockConfigEntry
from . import init_integration, mutate_rpc_device_status
async def test_custom_coap_port(
@ -121,12 +115,6 @@ async def test_shared_device_mac(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test first time shared device with another domain."""
config_entry = MockConfigEntry(domain="test", data={}, unique_id="some_id")
config_entry.add_to_hass(hass)
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(CONNECTION_NETWORK_MAC, format_mac(MOCK_MAC))},
)
await init_integration(hass, gen, sleep_period=1000)
assert "will resume when device is online" in caplog.text
@ -135,12 +123,7 @@ async def test_setup_entry_not_shelly(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test not Shelly entry."""
entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id) is False
await hass.async_block_till_done()
await init_integration(hass, 1, data={})
assert "probably comes from a custom integration" in caplog.text
@ -247,12 +230,7 @@ async def test_sleeping_block_device_online(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test sleeping block device online."""
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id="shelly")
config_entry.add_to_hass(hass)
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(CONNECTION_NETWORK_MAC, format_mac(MOCK_MAC))},
)
await init_integration(hass, 1, data={})
monkeypatch.setitem(
mock_block_device.settings,
@ -498,8 +476,7 @@ async def test_entry_missing_port(hass: HomeAssistant) -> None:
"model": MODEL_PLUS_2PM,
CONF_GEN: 2,
}
entry = MockConfigEntry(domain=DOMAIN, data=data, unique_id=MOCK_MAC)
entry.add_to_hass(hass)
entry = await init_integration(hass, 2, data=data, skip_setup=True)
with (
patch("homeassistant.components.shelly.RpcDevice.initialize"),
patch(
@ -523,8 +500,7 @@ async def test_rpc_entry_custom_port(hass: HomeAssistant) -> None:
CONF_GEN: 2,
CONF_PORT: 8001,
}
entry = MockConfigEntry(domain=DOMAIN, data=data, unique_id=MOCK_MAC)
entry.add_to_hass(hass)
entry = await init_integration(hass, 2, data=data, skip_setup=True)
with (
patch("homeassistant.components.shelly.RpcDevice.initialize"),
patch(