Use CONF_* const in Shelly tests (#140189)

This commit is contained in:
Simone Chemelli 2025-03-09 13:07:20 +01:00 committed by GitHub
parent d9d47f7203
commit 03aff0d662
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 163 additions and 100 deletions

View File

@ -18,7 +18,7 @@ from homeassistant.components.shelly.const import (
RPC_SENSORS_POLLING_INTERVAL, RPC_SENSORS_POLLING_INTERVAL,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST, CONF_MODEL
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import ( from homeassistant.helpers.device_registry import (
@ -47,7 +47,7 @@ async def init_integration(
data = { data = {
CONF_HOST: "192.168.1.37", CONF_HOST: "192.168.1.37",
CONF_SLEEP_PERIOD: sleep_period, CONF_SLEEP_PERIOD: sleep_period,
"model": model, CONF_MODEL: model,
} }
if gen is not None: if gen is not None:
data[CONF_GEN] = gen data[CONF_GEN] = gen

View File

@ -18,10 +18,19 @@ from homeassistant import config_entries
from homeassistant.components.shelly import MacAddressMismatchError, config_flow from homeassistant.components.shelly import MacAddressMismatchError, config_flow
from homeassistant.components.shelly.const import ( from homeassistant.components.shelly.const import (
CONF_BLE_SCANNER_MODE, CONF_BLE_SCANNER_MODE,
CONF_GEN,
CONF_SLEEP_PERIOD,
DOMAIN, DOMAIN,
BLEScannerMode, BLEScannerMode,
) )
from homeassistant.components.shelly.coordinator import ENTRY_RELOAD_COOLDOWN from homeassistant.components.shelly.coordinator import ENTRY_RELOAD_COOLDOWN
from homeassistant.const import (
CONF_HOST,
CONF_MODEL,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.zeroconf import ( from homeassistant.helpers.service_info.zeroconf import (
@ -100,18 +109,18 @@ async def test_form(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1", "port": port}, {CONF_HOST: "1.1.1.1", CONF_PORT: port},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Test name" assert result2["title"] == "Test name"
assert result2["data"] == { assert result2["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"port": port, CONF_PORT: port,
"model": model, CONF_MODEL: model,
"sleep_period": 0, CONF_SLEEP_PERIOD: 0,
"gen": gen, CONF_GEN: gen,
} }
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -163,18 +172,18 @@ async def test_user_flow_overrides_existing_discovery(
assert result["errors"] == {} assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1", "port": 80}, {CONF_HOST: "1.1.1.1", CONF_PORT: 80},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Test name" assert result2["title"] == "Test name"
assert result2["data"] == { assert result2["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"port": 80, CONF_PORT: 80,
"model": MODEL_PLUS_2PM, CONF_MODEL: MODEL_PLUS_2PM,
"sleep_period": 0, CONF_SLEEP_PERIOD: 0,
"gen": 2, CONF_GEN: 2,
} }
assert result2["context"]["unique_id"] == "AABBCCDDEEFF" assert result2["context"]["unique_id"] == "AABBCCDDEEFF"
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
@ -220,19 +229,19 @@ async def test_form_gen1_custom_port(
( (
1, 1,
MODEL_1, MODEL_1,
{"username": "test user", "password": "test1 password"}, {CONF_USERNAME: "test user", CONF_PASSWORD: "test1 password"},
"test user", "test user",
), ),
( (
2, 2,
MODEL_PLUS_2PM, MODEL_PLUS_2PM,
{"password": "test2 password"}, {CONF_PASSWORD: "test2 password"},
"admin", "admin",
), ),
( (
3, 3,
MODEL_PLUS_2PM, MODEL_PLUS_2PM,
{"password": "test2 password"}, {CONF_PASSWORD: "test2 password"},
"admin", "admin",
), ),
], ],
@ -259,7 +268,7 @@ async def test_form_auth(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
@ -282,13 +291,13 @@ async def test_form_auth(
assert result3["type"] is FlowResultType.CREATE_ENTRY assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Test name" assert result3["title"] == "Test name"
assert result3["data"] == { assert result3["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"port": DEFAULT_HTTP_PORT, CONF_PORT: DEFAULT_HTTP_PORT,
"model": model, CONF_MODEL: model,
"sleep_period": 0, CONF_SLEEP_PERIOD: 0,
"gen": gen, CONF_GEN: gen,
"username": username, CONF_USERNAME: username,
"password": user_input["password"], CONF_PASSWORD: user_input[CONF_PASSWORD],
} }
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -312,7 +321,7 @@ async def test_form_errors_get_info(
with patch("homeassistant.components.shelly.config_flow.get_info", side_effect=exc): with patch("homeassistant.components.shelly.config_flow.get_info", side_effect=exc):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
@ -333,7 +342,7 @@ async def test_form_missing_model_key(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
@ -356,7 +365,7 @@ async def test_form_missing_model_key_auth_enabled(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
@ -364,7 +373,7 @@ async def test_form_missing_model_key_auth_enabled(
monkeypatch.setattr(mock_rpc_device, "shelly", {"gen": 2}) monkeypatch.setattr(mock_rpc_device, "shelly", {"gen": 2})
result3 = await hass.config_entries.flow.async_configure( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"password": "1234"} result2["flow_id"], {CONF_PASSWORD: "1234"}
) )
assert result3["type"] is FlowResultType.FORM assert result3["type"] is FlowResultType.FORM
assert result3["errors"] == {"base": "firmware_not_fully_provisioned"} assert result3["errors"] == {"base": "firmware_not_fully_provisioned"}
@ -424,7 +433,7 @@ async def test_form_errors_test_connection(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
@ -435,7 +444,7 @@ async def test_form_already_configured(hass: HomeAssistant) -> None:
"""Test we get the form.""" """Test we get the form."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0"} domain="shelly", unique_id="test-mac", data={CONF_HOST: "0.0.0.0"}
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -449,14 +458,14 @@ async def test_form_already_configured(hass: HomeAssistant) -> None:
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.ABORT assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "already_configured" assert result2["reason"] == "already_configured"
# Test config entry got updated with latest IP # Test config entry got updated with latest IP
assert entry.data["host"] == "1.1.1.1" assert entry.data[CONF_HOST] == "1.1.1.1"
async def test_user_setup_ignored_device( async def test_user_setup_ignored_device(
@ -467,7 +476,7 @@ async def test_user_setup_ignored_device(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="test-mac", unique_id="test-mac",
data={"host": "0.0.0.0"}, data={CONF_HOST: "0.0.0.0"},
source=config_entries.SOURCE_IGNORE, source=config_entries.SOURCE_IGNORE,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -491,13 +500,13 @@ async def test_user_setup_ignored_device(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["type"] is FlowResultType.CREATE_ENTRY
# Test config entry got updated with latest IP # Test config entry got updated with latest IP
assert entry.data["host"] == "1.1.1.1" assert entry.data[CONF_HOST] == "1.1.1.1"
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -525,7 +534,7 @@ async def test_form_auth_errors_test_connection_gen1(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
with patch( with patch(
@ -534,7 +543,7 @@ async def test_form_auth_errors_test_connection_gen1(
): ):
result3 = await hass.config_entries.flow.async_configure( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], result2["flow_id"],
{"username": "test username", "password": "test password"}, {CONF_USERNAME: "test username", CONF_PASSWORD: "test password"},
) )
assert result3["type"] is FlowResultType.FORM assert result3["type"] is FlowResultType.FORM
assert result3["errors"] == {"base": base_error} assert result3["errors"] == {"base": base_error}
@ -563,7 +572,7 @@ async def test_form_auth_errors_test_connection_gen2(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
with patch( with patch(
@ -571,7 +580,7 @@ async def test_form_auth_errors_test_connection_gen2(
new=AsyncMock(side_effect=exc), new=AsyncMock(side_effect=exc),
): ):
result3 = await hass.config_entries.flow.async_configure( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"password": "test password"} result2["flow_id"], {CONF_PASSWORD: "test password"}
) )
assert result3["type"] is FlowResultType.FORM assert result3["type"] is FlowResultType.FORM
assert result3["errors"] == {"base": base_error} assert result3["errors"] == {"base": base_error}
@ -642,10 +651,10 @@ async def test_zeroconf(
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Test name" assert result2["title"] == "Test name"
assert result2["data"] == { assert result2["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"model": model, CONF_MODEL: model,
"sleep_period": 0, CONF_SLEEP_PERIOD: 0,
"gen": gen, CONF_GEN: gen,
} }
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -700,10 +709,10 @@ async def test_zeroconf_sleeping_device(
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Test name" assert result2["title"] == "Test name"
assert result2["data"] == { assert result2["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"model": MODEL_1, CONF_MODEL: MODEL_1,
"sleep_period": 600, CONF_SLEEP_PERIOD: 600,
"gen": 1, CONF_GEN: 1,
} }
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -739,7 +748,7 @@ async def test_zeroconf_already_configured(hass: HomeAssistant) -> None:
"""Test we get the form.""" """Test we get the form."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0"} domain="shelly", unique_id="test-mac", data={CONF_HOST: "0.0.0.0"}
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -756,7 +765,7 @@ async def test_zeroconf_already_configured(hass: HomeAssistant) -> None:
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
# Test config entry got updated with latest IP # Test config entry got updated with latest IP
assert entry.data["host"] == "1.1.1.1" assert entry.data[CONF_HOST] == "1.1.1.1"
async def test_zeroconf_ignored(hass: HomeAssistant) -> None: async def test_zeroconf_ignored(hass: HomeAssistant) -> None:
@ -787,7 +796,7 @@ async def test_zeroconf_with_wifi_ap_ip(hass: HomeAssistant) -> None:
"""Test we ignore the Wi-FI AP IP.""" """Test we ignore the Wi-FI AP IP."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "2.2.2.2"} domain="shelly", unique_id="test-mac", data={CONF_HOST: "2.2.2.2"}
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -806,7 +815,7 @@ async def test_zeroconf_with_wifi_ap_ip(hass: HomeAssistant) -> None:
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
# Test config entry was not updated with the wifi ap ip # Test config entry was not updated with the wifi ap ip
assert entry.data["host"] == "2.2.2.2" assert entry.data[CONF_HOST] == "2.2.2.2"
async def test_zeroconf_cannot_connect(hass: HomeAssistant) -> None: async def test_zeroconf_cannot_connect(hass: HomeAssistant) -> None:
@ -852,20 +861,20 @@ async def test_zeroconf_require_auth(
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"username": "test username", "password": "test password"}, {CONF_USERNAME: "test username", CONF_PASSWORD: "test password"},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Test name" assert result2["title"] == "Test name"
assert result2["data"] == { assert result2["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"port": DEFAULT_HTTP_PORT, CONF_PORT: DEFAULT_HTTP_PORT,
"model": MODEL_1, CONF_MODEL: MODEL_1,
"sleep_period": 0, CONF_SLEEP_PERIOD: 0,
"gen": 1, CONF_GEN: 1,
"username": "test username", CONF_USERNAME: "test username",
"password": "test password", CONF_PASSWORD: "test password",
} }
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@ -874,9 +883,9 @@ async def test_zeroconf_require_auth(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("gen", "user_input"), ("gen", "user_input"),
[ [
(1, {"username": "test user", "password": "test1 password"}), (1, {CONF_USERNAME: "test user", CONF_PASSWORD: "test1 password"}),
(2, {"password": "test2 password"}), (2, {CONF_PASSWORD: "test2 password"}),
(3, {"password": "test2 password"}), (3, {CONF_PASSWORD: "test2 password"}),
], ],
) )
async def test_reauth_successful( async def test_reauth_successful(
@ -888,7 +897,9 @@ async def test_reauth_successful(
) -> None: ) -> None:
"""Test starting a reauthentication flow.""" """Test starting a reauthentication flow."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": gen} domain="shelly",
unique_id="test-mac",
data={CONF_HOST: "0.0.0.0", CONF_GEN: gen},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
result = await entry.start_reauth_flow(hass) result = await entry.start_reauth_flow(hass)
@ -912,9 +923,9 @@ async def test_reauth_successful(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("gen", "user_input"), ("gen", "user_input"),
[ [
(1, {"username": "test user", "password": "test1 password"}), (1, {CONF_USERNAME: "test user", CONF_PASSWORD: "test1 password"}),
(2, {"password": "test2 password"}), (2, {CONF_PASSWORD: "test2 password"}),
(3, {"password": "test2 password"}), (3, {CONF_PASSWORD: "test2 password"}),
], ],
) )
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -933,7 +944,9 @@ async def test_reauth_unsuccessful(
) -> None: ) -> None:
"""Test reauthentication flow failed.""" """Test reauthentication flow failed."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": gen} domain="shelly",
unique_id="test-mac",
data={CONF_HOST: "0.0.0.0", CONF_GEN: gen},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
result = await entry.start_reauth_flow(hass) result = await entry.start_reauth_flow(hass)
@ -943,7 +956,12 @@ async def test_reauth_unsuccessful(
with ( with (
patch( patch(
"homeassistant.components.shelly.config_flow.get_info", "homeassistant.components.shelly.config_flow.get_info",
return_value={"mac": "test-mac", "type": MODEL_1, "auth": True, "gen": gen}, return_value={
"mac": "test-mac",
"type": MODEL_1,
"auth": True,
"gen": gen,
},
), ),
patch( patch(
"aioshelly.block_device.BlockDevice.create", new=AsyncMock(side_effect=exc) "aioshelly.block_device.BlockDevice.create", new=AsyncMock(side_effect=exc)
@ -962,7 +980,7 @@ async def test_reauth_unsuccessful(
async def test_reauth_get_info_error(hass: HomeAssistant) -> None: async def test_reauth_get_info_error(hass: HomeAssistant) -> None:
"""Test reauthentication flow failed with error in get_info().""" """Test reauthentication flow failed with error in get_info()."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": 2} domain="shelly", unique_id="test-mac", data={CONF_HOST: "0.0.0.0", CONF_GEN: 2}
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
result = await entry.start_reauth_flow(hass) result = await entry.start_reauth_flow(hass)
@ -975,7 +993,7 @@ async def test_reauth_get_info_error(hass: HomeAssistant) -> None:
): ):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={"password": "test2 password"}, user_input={CONF_PASSWORD: "test2 password"},
) )
assert result["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
@ -1106,7 +1124,12 @@ async def test_zeroconf_already_configured_triggers_refresh_mac_in_name(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="AABBCCDDEEFF", unique_id="AABBCCDDEEFF",
data={"host": "1.1.1.1", "gen": 2, "sleep_period": 0, "model": MODEL_1}, data={
CONF_HOST: "1.1.1.1",
CONF_GEN: 2,
CONF_SLEEP_PERIOD: 0,
CONF_MODEL: MODEL_1,
},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -1141,7 +1164,12 @@ async def test_zeroconf_already_configured_triggers_refresh(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="AABBCCDDEEFF", unique_id="AABBCCDDEEFF",
data={"host": "1.1.1.1", "gen": 2, "sleep_period": 0, "model": MODEL_1}, data={
CONF_HOST: "1.1.1.1",
CONF_GEN: 2,
CONF_SLEEP_PERIOD: 0,
CONF_MODEL: MODEL_1,
},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -1181,7 +1209,12 @@ async def test_zeroconf_sleeping_device_not_triggers_refresh(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="AABBCCDDEEFF", unique_id="AABBCCDDEEFF",
data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, data={
CONF_HOST: "1.1.1.1",
CONF_GEN: 2,
CONF_SLEEP_PERIOD: 1000,
CONF_MODEL: MODEL_1,
},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -1228,7 +1261,12 @@ async def test_zeroconf_sleeping_device_attempts_configure(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="AABBCCDDEEFF", unique_id="AABBCCDDEEFF",
data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, data={
CONF_HOST: "1.1.1.1",
CONF_GEN: 2,
CONF_SLEEP_PERIOD: 1000,
CONF_MODEL: MODEL_1,
},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -1288,7 +1326,12 @@ async def test_zeroconf_sleeping_device_attempts_configure_ws_disabled(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="AABBCCDDEEFF", unique_id="AABBCCDDEEFF",
data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, data={
CONF_HOST: "1.1.1.1",
CONF_GEN: 2,
CONF_SLEEP_PERIOD: 1000,
CONF_MODEL: MODEL_1,
},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -1348,7 +1391,12 @@ async def test_zeroconf_sleeping_device_attempts_configure_no_url_available(
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", domain="shelly",
unique_id="AABBCCDDEEFF", unique_id="AABBCCDDEEFF",
data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, data={
CONF_HOST: "1.1.1.1",
CONF_GEN: 2,
CONF_SLEEP_PERIOD: 1000,
CONF_MODEL: MODEL_1,
},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -1415,20 +1463,20 @@ async def test_sleeping_device_gen2_with_new_firmware(
): ):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"host": "1.1.1.1"}, {CONF_HOST: "1.1.1.1"},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result["data"] == { assert result["data"] == {
"host": "1.1.1.1", CONF_HOST: "1.1.1.1",
"port": DEFAULT_HTTP_PORT, CONF_PORT: DEFAULT_HTTP_PORT,
"model": MODEL_PLUS_2PM, CONF_MODEL: MODEL_PLUS_2PM,
"sleep_period": 666, CONF_SLEEP_PERIOD: 666,
"gen": 2, CONF_GEN: 2,
} }
@pytest.mark.parametrize("gen", [1, 2, 3]) @pytest.mark.parametrize(CONF_GEN, [1, 2, 3])
async def test_reconfigure_successful( async def test_reconfigure_successful(
hass: HomeAssistant, hass: HomeAssistant,
gen: int, gen: int,
@ -1437,7 +1485,9 @@ async def test_reconfigure_successful(
) -> None: ) -> None:
"""Test starting a reconfiguration flow.""" """Test starting a reconfiguration flow."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": gen} domain="shelly",
unique_id="test-mac",
data={CONF_HOST: "0.0.0.0", CONF_GEN: gen},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -1452,12 +1502,12 @@ async def test_reconfigure_successful(
): ):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={"host": "10.10.10.10", "port": 99}, user_input={CONF_HOST: "10.10.10.10", CONF_PORT: 99},
) )
assert result["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful" assert result["reason"] == "reconfigure_successful"
assert entry.data == {"host": "10.10.10.10", "port": 99, "gen": gen} assert entry.data == {CONF_HOST: "10.10.10.10", CONF_PORT: 99, CONF_GEN: gen}
@pytest.mark.parametrize("gen", [1, 2, 3]) @pytest.mark.parametrize("gen", [1, 2, 3])
@ -1469,7 +1519,9 @@ async def test_reconfigure_unsuccessful(
) -> None: ) -> None:
"""Test reconfiguration flow failed.""" """Test reconfiguration flow failed."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": gen} domain="shelly",
unique_id="test-mac",
data={CONF_HOST: "0.0.0.0", CONF_GEN: gen},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -1480,11 +1532,16 @@ async def test_reconfigure_unsuccessful(
with patch( with patch(
"homeassistant.components.shelly.config_flow.get_info", "homeassistant.components.shelly.config_flow.get_info",
return_value={"mac": "another-mac", "type": MODEL_1, "auth": False, "gen": gen}, return_value={
"mac": "another-mac",
"type": MODEL_1,
"auth": False,
"gen": gen,
},
): ):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={"host": "10.10.10.10", "port": 99}, user_input={CONF_HOST: "10.10.10.10", CONF_PORT: 99},
) )
assert result["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
@ -1506,7 +1563,7 @@ async def test_reconfigure_with_exception(
) -> None: ) -> None:
"""Test reconfiguration flow when an exception is raised.""" """Test reconfiguration flow when an exception is raised."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": 2} domain="shelly", unique_id="test-mac", data={CONF_HOST: "0.0.0.0", CONF_GEN: 2}
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -1518,7 +1575,7 @@ async def test_reconfigure_with_exception(
with patch("homeassistant.components.shelly.config_flow.get_info", side_effect=exc): with patch("homeassistant.components.shelly.config_flow.get_info", side_effect=exc):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={"host": "10.10.10.10", "port": 99}, user_input={CONF_HOST: "10.10.10.10", CONF_PORT: 99},
) )
assert result["errors"] == {"base": base_error} assert result["errors"] == {"base": base_error}

View File

@ -562,7 +562,7 @@ async def test_rpc_update_entry_sleep_period(
mock_rpc_device.mock_online() mock_rpc_device.mock_online()
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert entry.data["sleep_period"] == 600 assert entry.data[CONF_SLEEP_PERIOD] == 600
# Move time to generate sleep period update # Move time to generate sleep period update
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 3600) monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 3600)
@ -570,7 +570,7 @@ async def test_rpc_update_entry_sleep_period(
async_fire_time_changed(hass) async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert entry.data["sleep_period"] == 3600 assert entry.data[CONF_SLEEP_PERIOD] == 3600
async def test_rpc_sleeping_device_no_periodic_updates( async def test_rpc_sleeping_device_no_periodic_updates(

View File

@ -25,7 +25,13 @@ from homeassistant.components.shelly.const import (
BLEScannerMode, BLEScannerMode,
) )
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_ON, STATE_UNAVAILABLE from homeassistant.const import (
CONF_HOST,
CONF_MODEL,
CONF_PORT,
STATE_ON,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.device_registry import DeviceRegistry, format_mac from homeassistant.helpers.device_registry import DeviceRegistry, format_mac
@ -245,7 +251,7 @@ async def test_sleeping_block_device_online(
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert "online, resuming setup" in caplog.text assert "online, resuming setup" in caplog.text
assert entry.data["sleep_period"] == device_sleep assert entry.data[CONF_SLEEP_PERIOD] == device_sleep
@pytest.mark.parametrize(("entry_sleep", "device_sleep"), [(None, 0), (1000, 1000)]) @pytest.mark.parametrize(("entry_sleep", "device_sleep"), [(None, 0), (1000, 1000)])
@ -267,7 +273,7 @@ async def test_sleeping_rpc_device_online(
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert "online, resuming setup" in caplog.text assert "online, resuming setup" in caplog.text
assert entry.data["sleep_period"] == device_sleep assert entry.data[CONF_SLEEP_PERIOD] == device_sleep
async def test_sleeping_rpc_device_online_new_firmware( async def test_sleeping_rpc_device_online_new_firmware(
@ -286,7 +292,7 @@ async def test_sleeping_rpc_device_online_new_firmware(
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
assert "online, resuming setup" in caplog.text assert "online, resuming setup" in caplog.text
assert entry.data["sleep_period"] == 1500 assert entry.data[CONF_SLEEP_PERIOD] == 1500
async def test_sleeping_rpc_device_online_during_setup( async def test_sleeping_rpc_device_online_during_setup(
@ -474,7 +480,7 @@ async def test_entry_missing_port(hass: HomeAssistant) -> None:
data = { data = {
CONF_HOST: "192.168.1.37", CONF_HOST: "192.168.1.37",
CONF_SLEEP_PERIOD: 0, CONF_SLEEP_PERIOD: 0,
"model": MODEL_PLUS_2PM, CONF_MODEL: MODEL_PLUS_2PM,
CONF_GEN: 2, CONF_GEN: 2,
} }
entry = await init_integration(hass, 2, data=data, skip_setup=True) entry = await init_integration(hass, 2, data=data, skip_setup=True)
@ -497,7 +503,7 @@ async def test_rpc_entry_custom_port(hass: HomeAssistant) -> None:
data = { data = {
CONF_HOST: "192.168.1.37", CONF_HOST: "192.168.1.37",
CONF_SLEEP_PERIOD: 0, CONF_SLEEP_PERIOD: 0,
"model": MODEL_PLUS_2PM, CONF_MODEL: MODEL_PLUS_2PM,
CONF_GEN: 2, CONF_GEN: 2,
CONF_PORT: 8001, CONF_PORT: 8001,
} }