forked from home-assistant/core
Refactoring and changed access token format
This commit is contained in:
@@ -14,7 +14,7 @@ type FederwiegeConfigEntry = ConfigEntry[Federwiege]
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool:
|
||||||
"""Set up this integration using UI."""
|
"""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
|
# Check if token still has access
|
||||||
if not await connection.refresh_token():
|
if not await connection.refresh_token():
|
||||||
@@ -26,23 +26,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -
|
|||||||
|
|
||||||
entry.runtime_data = federwiege
|
entry.runtime_data = federwiege
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
entry,
|
|
||||||
PLATFORMS,
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
entry,
|
entry.runtime_data.disconnect()
|
||||||
PLATFORMS,
|
|
||||||
)
|
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
federwiege = entry.runtime_data
|
|
||||||
federwiege.disconnect()
|
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@@ -33,7 +33,6 @@ class SmarlaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
if await conn.refresh_token():
|
if await conn.refresh_token():
|
||||||
info["serial_number"] = conn.token.serialNumber
|
info["serial_number"] = conn.token.serialNumber
|
||||||
info["token"] = conn.token.get_string()
|
|
||||||
else:
|
else:
|
||||||
errors["base"] = "invalid_auth"
|
errors["base"] = "invalid_auth"
|
||||||
|
|
||||||
@@ -46,15 +45,19 @@ class SmarlaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
if user_input is not None:
|
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:
|
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()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=info["serial_number"],
|
title=serial_number,
|
||||||
data={CONF_ACCESS_TOKEN: info["token"]},
|
data={CONF_ACCESS_TOKEN: token},
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
@@ -14,15 +14,9 @@ from .const import DEVICE_MODEL_NAME, DOMAIN, MANUFACTURER_NAME
|
|||||||
class SmarlaBaseEntity(Entity):
|
class SmarlaBaseEntity(Entity):
|
||||||
"""Common Base Entity class for defining Smarla device."""
|
"""Common Base Entity class for defining Smarla device."""
|
||||||
|
|
||||||
_property: Property
|
|
||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
_attr_has_entity_name = True
|
_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:
|
def __init__(self, federwiege: Federwiege, prop: Property) -> None:
|
||||||
"""Initialise the entity."""
|
"""Initialise the entity."""
|
||||||
self._property = prop
|
self._property = prop
|
||||||
@@ -34,6 +28,10 @@ class SmarlaBaseEntity(Entity):
|
|||||||
serial_number=federwiege.serial_number,
|
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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Run when this Entity has been added to HA."""
|
"""Run when this Entity has been added to HA."""
|
||||||
await self._property.add_listener(self.on_change)
|
await self._property.add_listener(self.on_change)
|
||||||
|
Reference in New Issue
Block a user