Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Shelly RPC device gen from config entry data #107019

Merged
merged 1 commit into from Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions homeassistant/components/shelly/config_flow.py
Expand Up @@ -25,6 +25,7 @@

from .const import (
CONF_BLE_SCANNER_MODE,
CONF_GEN,
CONF_SLEEP_PERIOD,
DOMAIN,
LOGGER,
Expand Down Expand Up @@ -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
Expand All @@ -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,
}


Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/shelly/const.py
Expand Up @@ -214,3 +214,5 @@ class BLEScannerMode(StrEnum):
MODEL_MOTION_2,
MODEL_VALVE,
)

CONF_GEN = "gen"
3 changes: 2 additions & 1 deletion homeassistant/components/shelly/coordinator.py
Expand Up @@ -33,6 +33,7 @@
ATTR_GENERATION,
BATTERY_DEVICES_WITH_PERMANENT_CONNECTION,
CONF_BLE_SCANNER_MODE,
CONF_GEN,
CONF_SLEEP_PERIOD,
DATA_CONFIG_ENTRY,
DOMAIN,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/shelly/utils.py
Expand Up @@ -34,6 +34,7 @@
from .const import (
BASIC_INPUTS_EVENTS_TYPES,
CONF_COAP_PORT,
CONF_GEN,
DEFAULT_COAP_PORT,
DEVICES_WITHOUT_FIRMWARE_CHANGELOG,
DOMAIN,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down