Align CONF_ in Shelly integration (#140202)

* Allign CONST_ in Shelly integration

* apply review comment
This commit is contained in:
Simone Chemelli 2025-03-09 18:04:00 +01:00 committed by GitHub
parent befcd63221
commit 8a51644d1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 25 deletions

View File

@ -16,7 +16,13 @@ from aioshelly.rpc_device import RpcDevice, bluetooth_mac_from_primary_mac
import voluptuous as vol import voluptuous as vol
from homeassistant.components.bluetooth import async_remove_scanner 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.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import ( 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 # Following code block will force the right value for affected devices
if ( if (
sleep_period == BLOCK_WRONG_SLEEP_PERIOD 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( LOGGER.warning(
"Updating stored sleep period for %s: from %s to %s", "Updating stored sleep period for %s: from %s to %s",

View File

@ -31,6 +31,7 @@ from homeassistant.config_entries import (
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_MAC, CONF_MAC,
CONF_MODEL,
CONF_PASSWORD, CONF_PASSWORD,
CONF_PORT, CONF_PORT,
CONF_USERNAME, CONF_USERNAME,
@ -116,7 +117,9 @@ async def validate_input(
return { return {
"title": rpc_device.name, "title": rpc_device.name,
CONF_SLEEP_PERIOD: sleep_period, 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, CONF_GEN: gen,
} }
@ -136,7 +139,7 @@ async def validate_input(
return { return {
"title": block_device.name, "title": block_device.name,
CONF_SLEEP_PERIOD: sleep_period, CONF_SLEEP_PERIOD: sleep_period,
"model": block_device.model, CONF_MODEL: block_device.model,
CONF_GEN: gen, CONF_GEN: gen,
} }
@ -191,14 +194,14 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
LOGGER.exception("Unexpected exception") LOGGER.exception("Unexpected exception")
errors["base"] = "unknown" errors["base"] = "unknown"
else: else:
if device_info["model"]: if device_info[CONF_MODEL]:
return self.async_create_entry( return self.async_create_entry(
title=device_info["title"], title=device_info["title"],
data={ data={
CONF_HOST: user_input[CONF_HOST], CONF_HOST: user_input[CONF_HOST],
CONF_PORT: user_input[CONF_PORT], CONF_PORT: user_input[CONF_PORT],
CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD], CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD],
"model": device_info["model"], CONF_MODEL: device_info[CONF_MODEL],
CONF_GEN: device_info[CONF_GEN], CONF_GEN: device_info[CONF_GEN],
}, },
) )
@ -230,7 +233,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
LOGGER.exception("Unexpected exception") LOGGER.exception("Unexpected exception")
errors["base"] = "unknown" errors["base"] = "unknown"
else: else:
if device_info["model"]: if device_info[CONF_MODEL]:
return self.async_create_entry( return self.async_create_entry(
title=device_info["title"], title=device_info["title"],
data={ data={
@ -238,7 +241,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
CONF_HOST: self.host, CONF_HOST: self.host,
CONF_PORT: self.port, CONF_PORT: self.port,
CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD], CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD],
"model": device_info["model"], CONF_MODEL: device_info[CONF_MODEL],
CONF_GEN: device_info[CONF_GEN], CONF_GEN: device_info[CONF_GEN],
}, },
) )
@ -336,7 +339,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle discovery confirm.""" """Handle discovery confirm."""
errors: dict[str, str] = {} errors: dict[str, str] = {}
if not self.device_info["model"]: if not self.device_info[CONF_MODEL]:
errors["base"] = "firmware_not_fully_provisioned" errors["base"] = "firmware_not_fully_provisioned"
model = "Shelly" model = "Shelly"
else: else:
@ -345,9 +348,9 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_create_entry( return self.async_create_entry(
title=self.device_info["title"], title=self.device_info["title"],
data={ data={
"host": self.host, CONF_HOST: self.host,
CONF_SLEEP_PERIOD: self.device_info[CONF_SLEEP_PERIOD], 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], CONF_GEN: self.device_info[CONF_GEN],
}, },
) )
@ -356,8 +359,8 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_show_form( return self.async_show_form(
step_id="confirm_discovery", step_id="confirm_discovery",
description_placeholders={ description_placeholders={
"model": model, CONF_MODEL: model,
"host": self.host, CONF_HOST: self.host,
}, },
errors=errors, errors=errors,
) )
@ -466,7 +469,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
return ( return (
get_device_entry_gen(config_entry) in RPC_GENERATIONS get_device_entry_gen(config_entry) in RPC_GENERATIONS
and not config_entry.data.get(CONF_SLEEP_PERIOD) 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
) )

View File

@ -26,6 +26,7 @@ from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_ID, ATTR_DEVICE_ID,
CONF_HOST, CONF_HOST,
CONF_MODEL,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
Platform, Platform,
) )
@ -139,7 +140,7 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice](
@cached_property @cached_property
def model(self) -> str: def model(self) -> str:
"""Model of the device.""" """Model of the device."""
return cast(str, self.config_entry.data["model"]) return cast(str, self.config_entry.data[CONF_MODEL])
@cached_property @cached_property
def mac(self) -> str: def mac(self) -> str:

View File

@ -6,7 +6,13 @@ from typing import Any
from homeassistant.components.bluetooth import async_scanner_by_source from homeassistant.components.bluetooth import async_scanner_by_source
from homeassistant.components.diagnostics import async_redact_data 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 homeassistant.core import HomeAssistant
from .coordinator import ShellyConfigEntry from .coordinator import ShellyConfigEntry
@ -30,9 +36,9 @@ async def async_get_config_entry_diagnostics(
block_coordinator = shelly_entry_data.block block_coordinator = shelly_entry_data.block
assert block_coordinator assert block_coordinator
device_info = { device_info = {
"name": block_coordinator.name, ATTR_NAME: block_coordinator.name,
"model": block_coordinator.model, ATTR_MODEL: block_coordinator.model,
"sw_version": block_coordinator.sw_version, ATTR_SW_VERSION: block_coordinator.sw_version,
} }
if block_coordinator.device.initialized: if block_coordinator.device.initialized:
device_settings = { device_settings = {
@ -65,9 +71,9 @@ async def async_get_config_entry_diagnostics(
rpc_coordinator = shelly_entry_data.rpc rpc_coordinator = shelly_entry_data.rpc
assert rpc_coordinator assert rpc_coordinator
device_info = { device_info = {
"name": rpc_coordinator.name, ATTR_NAME: rpc_coordinator.name,
"model": rpc_coordinator.model, ATTR_MODEL: rpc_coordinator.model,
"sw_version": rpc_coordinator.sw_version, ATTR_SW_VERSION: rpc_coordinator.sw_version,
} }
if rpc_coordinator.device.initialized: if rpc_coordinator.device.initialized:
device_settings = { device_settings = {

View File

@ -28,7 +28,12 @@ from yarl import URL
from homeassistant.components import network from homeassistant.components import network
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
from homeassistant.config_entries import ConfigEntry 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.core import Event, HomeAssistant, callback
from homeassistant.helpers import ( from homeassistant.helpers import (
device_registry as dr, 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: def get_model_name(info: dict[str, Any]) -> str:
"""Return the device model name.""" """Return the device model name."""
if get_info_gen(info) in RPC_GENERATIONS: 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"])) 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_key="unsupported_firmware",
translation_placeholders={ translation_placeholders={
"device_name": entry.title, "device_name": entry.title,
"ip_address": entry.data["host"], "ip_address": entry.data[CONF_HOST],
}, },
) )