From 8a51644d1db59bc39642043a2c11c4ad42394715 Mon Sep 17 00:00:00 2001
From: Simone Chemelli <simone.chemelli@gmail.com>
Date: Sun, 9 Mar 2025 18:04:00 +0100
Subject: [PATCH] Align CONF_ in Shelly integration (#140202)

* Allign CONST_ in Shelly integration

* apply review comment
---
 homeassistant/components/shelly/__init__.py   | 10 +++++--
 .../components/shelly/config_flow.py          | 27 ++++++++++---------
 .../components/shelly/coordinator.py          |  3 ++-
 .../components/shelly/diagnostics.py          | 20 +++++++++-----
 homeassistant/components/shelly/utils.py      | 11 +++++---
 5 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py
index 55b75b3face..7440013940c 100644
--- a/homeassistant/components/shelly/__init__.py
+++ b/homeassistant/components/shelly/__init__.py
@@ -16,7 +16,13 @@ from aioshelly.rpc_device import RpcDevice, bluetooth_mac_from_primary_mac
 import voluptuous as vol
 
 from homeassistant.components.bluetooth import async_remove_scanner
-from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
+from homeassistant.const import (
+    CONF_HOST,
+    CONF_MODEL,
+    CONF_PASSWORD,
+    CONF_USERNAME,
+    Platform,
+)
 from homeassistant.core import HomeAssistant
 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
 from homeassistant.helpers import (
@@ -159,7 +165,7 @@ async def _async_setup_block_entry(
     # Following code block will force the right value for affected devices
     if (
         sleep_period == BLOCK_WRONG_SLEEP_PERIOD
-        and entry.data["model"] in MODELS_WITH_WRONG_SLEEP_PERIOD
+        and entry.data[CONF_MODEL] in MODELS_WITH_WRONG_SLEEP_PERIOD
     ):
         LOGGER.warning(
             "Updating stored sleep period for %s: from %s to %s",
diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py
index 5c5e187a0f4..8e47235c981 100644
--- a/homeassistant/components/shelly/config_flow.py
+++ b/homeassistant/components/shelly/config_flow.py
@@ -31,6 +31,7 @@ from homeassistant.config_entries import (
 from homeassistant.const import (
     CONF_HOST,
     CONF_MAC,
+    CONF_MODEL,
     CONF_PASSWORD,
     CONF_PORT,
     CONF_USERNAME,
@@ -116,7 +117,9 @@ async def validate_input(
         return {
             "title": rpc_device.name,
             CONF_SLEEP_PERIOD: sleep_period,
-            "model": rpc_device.xmod_info.get("p") or rpc_device.shelly.get("model"),
+            CONF_MODEL: (
+                rpc_device.xmod_info.get("p") or rpc_device.shelly.get(CONF_MODEL)
+            ),
             CONF_GEN: gen,
         }
 
@@ -136,7 +139,7 @@ async def validate_input(
     return {
         "title": block_device.name,
         CONF_SLEEP_PERIOD: sleep_period,
-        "model": block_device.model,
+        CONF_MODEL: block_device.model,
         CONF_GEN: gen,
     }
 
@@ -191,14 +194,14 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
                     LOGGER.exception("Unexpected exception")
                     errors["base"] = "unknown"
                 else:
-                    if device_info["model"]:
+                    if device_info[CONF_MODEL]:
                         return self.async_create_entry(
                             title=device_info["title"],
                             data={
                                 CONF_HOST: user_input[CONF_HOST],
                                 CONF_PORT: user_input[CONF_PORT],
                                 CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD],
-                                "model": device_info["model"],
+                                CONF_MODEL: device_info[CONF_MODEL],
                                 CONF_GEN: device_info[CONF_GEN],
                             },
                         )
@@ -230,7 +233,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
                 LOGGER.exception("Unexpected exception")
                 errors["base"] = "unknown"
             else:
-                if device_info["model"]:
+                if device_info[CONF_MODEL]:
                     return self.async_create_entry(
                         title=device_info["title"],
                         data={
@@ -238,7 +241,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
                             CONF_HOST: self.host,
                             CONF_PORT: self.port,
                             CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD],
-                            "model": device_info["model"],
+                            CONF_MODEL: device_info[CONF_MODEL],
                             CONF_GEN: device_info[CONF_GEN],
                         },
                     )
@@ -336,7 +339,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
         """Handle discovery confirm."""
         errors: dict[str, str] = {}
 
-        if not self.device_info["model"]:
+        if not self.device_info[CONF_MODEL]:
             errors["base"] = "firmware_not_fully_provisioned"
             model = "Shelly"
         else:
@@ -345,9 +348,9 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
                 return self.async_create_entry(
                     title=self.device_info["title"],
                     data={
-                        "host": self.host,
+                        CONF_HOST: self.host,
                         CONF_SLEEP_PERIOD: self.device_info[CONF_SLEEP_PERIOD],
-                        "model": self.device_info["model"],
+                        CONF_MODEL: self.device_info[CONF_MODEL],
                         CONF_GEN: self.device_info[CONF_GEN],
                     },
                 )
@@ -356,8 +359,8 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
         return self.async_show_form(
             step_id="confirm_discovery",
             description_placeholders={
-                "model": model,
-                "host": self.host,
+                CONF_MODEL: model,
+                CONF_HOST: self.host,
             },
             errors=errors,
         )
@@ -466,7 +469,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
         return (
             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
+            and config_entry.data.get(CONF_MODEL) != MODEL_WALL_DISPLAY
         )
 
 
diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py
index bebf8efbdd7..95812c12e10 100644
--- a/homeassistant/components/shelly/coordinator.py
+++ b/homeassistant/components/shelly/coordinator.py
@@ -26,6 +26,7 @@ from homeassistant.config_entries import ConfigEntry, ConfigEntryState
 from homeassistant.const import (
     ATTR_DEVICE_ID,
     CONF_HOST,
+    CONF_MODEL,
     EVENT_HOMEASSISTANT_STOP,
     Platform,
 )
@@ -139,7 +140,7 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice](
     @cached_property
     def model(self) -> str:
         """Model of the device."""
-        return cast(str, self.config_entry.data["model"])
+        return cast(str, self.config_entry.data[CONF_MODEL])
 
     @cached_property
     def mac(self) -> str:
diff --git a/homeassistant/components/shelly/diagnostics.py b/homeassistant/components/shelly/diagnostics.py
index d56a2884e17..cac2bb2f16b 100644
--- a/homeassistant/components/shelly/diagnostics.py
+++ b/homeassistant/components/shelly/diagnostics.py
@@ -6,7 +6,13 @@ from typing import Any
 
 from homeassistant.components.bluetooth import async_scanner_by_source
 from homeassistant.components.diagnostics import async_redact_data
-from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
+from homeassistant.const import (
+    ATTR_MODEL,
+    ATTR_NAME,
+    ATTR_SW_VERSION,
+    CONF_PASSWORD,
+    CONF_USERNAME,
+)
 from homeassistant.core import HomeAssistant
 
 from .coordinator import ShellyConfigEntry
@@ -30,9 +36,9 @@ async def async_get_config_entry_diagnostics(
         block_coordinator = shelly_entry_data.block
         assert block_coordinator
         device_info = {
-            "name": block_coordinator.name,
-            "model": block_coordinator.model,
-            "sw_version": block_coordinator.sw_version,
+            ATTR_NAME: block_coordinator.name,
+            ATTR_MODEL: block_coordinator.model,
+            ATTR_SW_VERSION: block_coordinator.sw_version,
         }
         if block_coordinator.device.initialized:
             device_settings = {
@@ -65,9 +71,9 @@ async def async_get_config_entry_diagnostics(
         rpc_coordinator = shelly_entry_data.rpc
         assert rpc_coordinator
         device_info = {
-            "name": rpc_coordinator.name,
-            "model": rpc_coordinator.model,
-            "sw_version": rpc_coordinator.sw_version,
+            ATTR_NAME: rpc_coordinator.name,
+            ATTR_MODEL: rpc_coordinator.model,
+            ATTR_SW_VERSION: rpc_coordinator.sw_version,
         }
         if rpc_coordinator.device.initialized:
             device_settings = {
diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py
index b478e416c50..626cb287f64 100644
--- a/homeassistant/components/shelly/utils.py
+++ b/homeassistant/components/shelly/utils.py
@@ -28,7 +28,12 @@ from yarl import URL
 from homeassistant.components import network
 from homeassistant.components.http import HomeAssistantView
 from homeassistant.config_entries import ConfigEntry
-from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP
+from homeassistant.const import (
+    CONF_HOST,
+    CONF_MODEL,
+    CONF_PORT,
+    EVENT_HOMEASSISTANT_STOP,
+)
 from homeassistant.core import Event, HomeAssistant, callback
 from homeassistant.helpers import (
     device_registry as dr,
@@ -322,7 +327,7 @@ def get_info_gen(info: dict[str, Any]) -> int:
 def get_model_name(info: dict[str, Any]) -> str:
     """Return the device model name."""
     if get_info_gen(info) in RPC_GENERATIONS:
-        return cast(str, MODEL_NAMES.get(info["model"], info["model"]))
+        return cast(str, MODEL_NAMES.get(info[CONF_MODEL], info[CONF_MODEL]))
 
     return cast(str, MODEL_NAMES.get(info["type"], info["type"]))
 
@@ -514,7 +519,7 @@ def async_create_issue_unsupported_firmware(
         translation_key="unsupported_firmware",
         translation_placeholders={
             "device_name": entry.title,
-            "ip_address": entry.data["host"],
+            "ip_address": entry.data[CONF_HOST],
         },
     )
 
-- 
GitLab