diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py index 29daf0501637..59ae6eed1965 100644 --- a/homeassistant/components/shelly/config_flow.py +++ b/homeassistant/components/shelly/config_flow.py @@ -36,6 +36,7 @@ from .coordinator import async_reconnect_soon from .utils import ( get_block_device_sleep_period, get_coap_context, + get_device_entry_gen, get_info_auth, get_info_gen, get_model_name, @@ -322,7 +323,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN): except (DeviceConnectionError, InvalidAuthError, FirmwareUnsupported): return self.async_abort(reason="reauth_unsuccessful") - if self.entry.data.get(CONF_GEN, 1) != 1: + if get_device_entry_gen(self.entry) != 1: user_input[CONF_USERNAME] = "admin" try: await validate_input(self.hass, host, info, user_input) @@ -335,7 +336,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN): await self.hass.config_entries.async_reload(self.entry.entry_id) return self.async_abort(reason="reauth_successful") - if self.entry.data.get(CONF_GEN, 1) in BLOCK_GENERATIONS: + if get_device_entry_gen(self.entry) in BLOCK_GENERATIONS: schema = { vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str, @@ -364,7 +365,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN): def async_supports_options_flow(cls, config_entry: ConfigEntry) -> bool: """Return options flow support for this handler.""" return ( - config_entry.data.get(CONF_GEN) in RPC_GENERATIONS + get_device_entry_gen(config_entry) in RPC_GENERATIONS and not config_entry.data.get(CONF_SLEEP_PERIOD) and config_entry.data.get("model") != MODEL_WALL_DISPLAY ) diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index 77fa0bd2efd1..7f88cce1134b 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -33,7 +33,6 @@ from .const import ( ATTR_GENERATION, BATTERY_DEVICES_WITH_PERMANENT_CONNECTION, CONF_BLE_SCANNER_MODE, - CONF_GEN, CONF_SLEEP_PERIOD, DATA_CONFIG_ENTRY, DOMAIN, @@ -58,7 +57,11 @@ from .const import ( UPDATE_PERIOD_MULTIPLIER, BLEScannerMode, ) -from .utils import get_rpc_device_wakeup_period, update_device_fw_info +from .utils import ( + get_device_entry_gen, + get_rpc_device_wakeup_period, + update_device_fw_info, +) _DeviceT = TypeVar("_DeviceT", bound="BlockDevice|RpcDevice") @@ -136,7 +139,7 @@ class ShellyCoordinatorBase(DataUpdateCoordinator[None], Generic[_DeviceT]): manufacturer="Shelly", model=aioshelly.const.MODEL_NAMES.get(self.model, self.model), sw_version=self.sw_version, - hw_version=f"gen{self.entry.data[CONF_GEN]} ({self.model})", + hw_version=f"gen{get_device_entry_gen(self.entry)} ({self.model})", configuration_url=f"http://{self.entry.data[CONF_HOST]}", ) self.device_id = device_entry.id diff --git a/tests/components/shelly/__init__.py b/tests/components/shelly/__init__.py index 0384e9255a38..26040e135577 100644 --- a/tests/components/shelly/__init__.py +++ b/tests/components/shelly/__init__.py @@ -12,6 +12,7 @@ from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components.shelly.const import ( + CONF_GEN, CONF_SLEEP_PERIOD, DOMAIN, REST_SENSORS_UPDATE_INTERVAL, @@ -30,7 +31,7 @@ MOCK_MAC = "123456789ABC" async def init_integration( hass: HomeAssistant, - gen: int, + gen: int | None, model=MODEL_25, sleep_period=0, options: dict[str, Any] | None = None, @@ -41,8 +42,9 @@ async def init_integration( CONF_HOST: "192.168.1.37", CONF_SLEEP_PERIOD: sleep_period, "model": model, - "gen": gen, } + if gen is not None: + data[CONF_GEN] = gen entry = MockConfigEntry( domain=DOMAIN, data=data, unique_id=MOCK_MAC, options=options diff --git a/tests/components/shelly/test_init.py b/tests/components/shelly/test_init.py index 8f6599b39e46..643fc775cc42 100644 --- a/tests/components/shelly/test_init.py +++ b/tests/components/shelly/test_init.py @@ -301,3 +301,11 @@ async def test_no_attempt_to_stop_scanner_with_sleepy_devices( mock_rpc_device.mock_update() await hass.async_block_till_done() assert not mock_stop_scanner.call_count + + +async def test_entry_missing_gen(hass: HomeAssistant, mock_block_device) -> None: + """Test successful Gen1 device init when gen is missing in entry data.""" + entry = await init_integration(hass, None) + + assert entry.state is ConfigEntryState.LOADED + assert hass.states.get("switch.test_name_channel_1").state is STATE_ON