Refactoring and changed access token format

This commit is contained in:
Robin Lintermann 2025-05-06 09:11:30 +00:00
parent b456af473b
commit 73d37972df
3 changed files with 16 additions and 24 deletions

View File

@ -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

View File

@ -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(

View File

@ -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)