From 73d37972df55a557c11f40334d77df21f68edf2c Mon Sep 17 00:00:00 2001 From: Robin Lintermann Date: Tue, 6 May 2025 09:11:30 +0000 Subject: [PATCH] Refactoring and changed access token format --- homeassistant/components/smarla/__init__.py | 17 ++++------------- homeassistant/components/smarla/config_flow.py | 13 ++++++++----- homeassistant/components/smarla/entity.py | 10 ++++------ 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/smarla/__init__.py b/homeassistant/components/smarla/__init__.py index 14a728946f3..c8738639a85 100644 --- a/homeassistant/components/smarla/__init__.py +++ b/homeassistant/components/smarla/__init__.py @@ -14,7 +14,7 @@ type FederwiegeConfigEntry = ConfigEntry[Federwiege] async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool: """Set up this integration using UI.""" - connection = Connection(HOST, token_str=entry.data.get(CONF_ACCESS_TOKEN, None)) + connection = Connection(HOST, token_b64=entry.data.get(CONF_ACCESS_TOKEN, None)) # Check if token still has access if not await connection.refresh_token(): @@ -26,23 +26,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) - entry.runtime_data = federwiege - await hass.config_entries.async_forward_entry_setups( - entry, - PLATFORMS, - ) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True async def async_unload_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms( - entry, - PLATFORMS, - ) - - if unload_ok: - federwiege = entry.runtime_data - federwiege.disconnect() + if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): + entry.runtime_data.disconnect() return unload_ok diff --git a/homeassistant/components/smarla/config_flow.py b/homeassistant/components/smarla/config_flow.py index 64f354b8354..9fce505f2a6 100644 --- a/homeassistant/components/smarla/config_flow.py +++ b/homeassistant/components/smarla/config_flow.py @@ -33,7 +33,6 @@ class SmarlaConfigFlow(ConfigFlow, domain=DOMAIN): if await conn.refresh_token(): info["serial_number"] = conn.token.serialNumber - info["token"] = conn.token.get_string() else: errors["base"] = "invalid_auth" @@ -46,15 +45,19 @@ class SmarlaConfigFlow(ConfigFlow, domain=DOMAIN): errors: dict[str, str] = {} if user_input is not None: - errors, info = await self._handle_token(token=user_input[CONF_ACCESS_TOKEN]) + token = user_input[CONF_ACCESS_TOKEN] + + errors, info = await self._handle_token(token=token) if not errors: - await self.async_set_unique_id(info["serial_number"]) + serial_number = info["serial_number"] + + await self.async_set_unique_id(serial_number) self._abort_if_unique_id_configured() return self.async_create_entry( - title=info["serial_number"], - data={CONF_ACCESS_TOKEN: info["token"]}, + title=serial_number, + data={CONF_ACCESS_TOKEN: token}, ) return self.async_show_form( diff --git a/homeassistant/components/smarla/entity.py b/homeassistant/components/smarla/entity.py index a93bad87e13..a0ca052219c 100644 --- a/homeassistant/components/smarla/entity.py +++ b/homeassistant/components/smarla/entity.py @@ -14,15 +14,9 @@ from .const import DEVICE_MODEL_NAME, DOMAIN, MANUFACTURER_NAME class SmarlaBaseEntity(Entity): """Common Base Entity class for defining Smarla device.""" - _property: Property - _attr_should_poll = False _attr_has_entity_name = True - async def on_change(self, value: Any): - """Notify ha when state changes.""" - self.async_write_ha_state() - def __init__(self, federwiege: Federwiege, prop: Property) -> None: """Initialise the entity.""" self._property = prop @@ -34,6 +28,10 @@ class SmarlaBaseEntity(Entity): serial_number=federwiege.serial_number, ) + async def on_change(self, value: Any): + """Notify ha when state changes.""" + self.async_write_ha_state() + async def async_added_to_hass(self) -> None: """Run when this Entity has been added to HA.""" await self._property.add_listener(self.on_change)