From a9dea1e6f5998e8f23e2c12ed2d1f2ceeccbc3b2 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Thu, 4 Jan 2024 00:21:14 +0100 Subject: [PATCH] Use gen from config entry data --- homeassistant/components/shelly/config_flow.py | 17 +++++++++-------- homeassistant/components/shelly/const.py | 2 ++ homeassistant/components/shelly/coordinator.py | 3 ++- homeassistant/components/shelly/utils.py | 5 +++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py index 68b0f1f8cccb18..29daf05016370e 100644 --- a/homeassistant/components/shelly/config_flow.py +++ b/homeassistant/components/shelly/config_flow.py @@ -25,6 +25,7 @@ from .const import ( CONF_BLE_SCANNER_MODE, + CONF_GEN, CONF_SLEEP_PERIOD, DOMAIN, LOGGER, @@ -84,7 +85,7 @@ async def validate_input( "title": rpc_device.name, CONF_SLEEP_PERIOD: sleep_period, "model": rpc_device.shelly.get("model"), - "gen": gen, + CONF_GEN: gen, } # Gen1 @@ -99,7 +100,7 @@ async def validate_input( "title": block_device.name, CONF_SLEEP_PERIOD: get_block_device_sleep_period(block_device.settings), "model": block_device.model, - "gen": gen, + CONF_GEN: gen, } @@ -153,7 +154,7 @@ async def async_step_user( **user_input, CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD], "model": device_info["model"], - "gen": device_info["gen"], + CONF_GEN: device_info[CONF_GEN], }, ) errors["base"] = "firmware_not_fully_provisioned" @@ -190,7 +191,7 @@ async def async_step_credentials( CONF_HOST: self.host, CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD], "model": device_info["model"], - "gen": device_info["gen"], + CONF_GEN: device_info[CONF_GEN], }, ) errors["base"] = "firmware_not_fully_provisioned" @@ -288,7 +289,7 @@ async def async_step_confirm_discovery( "host": self.host, CONF_SLEEP_PERIOD: self.device_info[CONF_SLEEP_PERIOD], "model": self.device_info["model"], - "gen": self.device_info["gen"], + CONF_GEN: self.device_info[CONF_GEN], }, ) self._set_confirm_only() @@ -321,7 +322,7 @@ async def async_step_reauth_confirm( except (DeviceConnectionError, InvalidAuthError, FirmwareUnsupported): return self.async_abort(reason="reauth_unsuccessful") - if self.entry.data.get("gen", 1) != 1: + if self.entry.data.get(CONF_GEN, 1) != 1: user_input[CONF_USERNAME] = "admin" try: await validate_input(self.hass, host, info, user_input) @@ -334,7 +335,7 @@ async def async_step_reauth_confirm( await self.hass.config_entries.async_reload(self.entry.entry_id) return self.async_abort(reason="reauth_successful") - if self.entry.data.get("gen", 1) in BLOCK_GENERATIONS: + if self.entry.data.get(CONF_GEN, 1) in BLOCK_GENERATIONS: schema = { vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str, @@ -363,7 +364,7 @@ def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlowHandler: def async_supports_options_flow(cls, config_entry: ConfigEntry) -> bool: """Return options flow support for this handler.""" return ( - config_entry.data.get("gen") in RPC_GENERATIONS + config_entry.data.get(CONF_GEN) 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/const.py b/homeassistant/components/shelly/const.py index ca1c450c9faaab..1e2c22691fb6c9 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -214,3 +214,5 @@ class BLEScannerMode(StrEnum): MODEL_MOTION_2, MODEL_VALVE, ) + +CONF_GEN = "gen" diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index a7659ecc392f9b..77fa0bd2efd166 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -33,6 +33,7 @@ ATTR_GENERATION, BATTERY_DEVICES_WITH_PERMANENT_CONNECTION, CONF_BLE_SCANNER_MODE, + CONF_GEN, CONF_SLEEP_PERIOD, DATA_CONFIG_ENTRY, DOMAIN, @@ -135,7 +136,7 @@ def async_setup(self) -> None: manufacturer="Shelly", model=aioshelly.const.MODEL_NAMES.get(self.model, self.model), sw_version=self.sw_version, - hw_version=f"gen{self.device.gen} ({self.model})", + hw_version=f"gen{self.entry.data[CONF_GEN]} ({self.model})", configuration_url=f"http://{self.entry.data[CONF_HOST]}", ) self.device_id = device_entry.id diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index a43d9cb0bcb07d..d40b22ca50a4fa 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -34,6 +34,7 @@ from .const import ( BASIC_INPUTS_EVENTS_TYPES, CONF_COAP_PORT, + CONF_GEN, DEFAULT_COAP_PORT, DEVICES_WITHOUT_FIRMWARE_CHANGELOG, DOMAIN, @@ -281,7 +282,7 @@ def get_info_auth(info: dict[str, Any]) -> bool: def get_info_gen(info: dict[str, Any]) -> int: """Return the device generation from shelly info.""" - return int(info.get("gen", 1)) + return int(info.get(CONF_GEN, 1)) def get_model_name(info: dict[str, Any]) -> str: @@ -325,7 +326,7 @@ def get_rpc_entity_name( def get_device_entry_gen(entry: ConfigEntry) -> int: """Return the device generation from config entry.""" - return entry.data.get("gen", 1) + return entry.data.get(CONF_GEN, 1) def get_rpc_key_instances(keys_dict: dict[str, Any], key: str) -> list[str]: