Skip to content

Commit

Permalink
Add login and rewrite access to extended data for solarlog (#126024)
Browse files Browse the repository at this point in the history
* Initial commit

* Add/update tests

* Minor adjustment

* Update data_schema

* Adjust get password

* Set const for has_password, remove deletion of extended_data

* Update diagnostics snapshot

* Correct typo

* Add test for migration from mv 2 to 3

* Adjust migration test
  • Loading branch information
dontinelli authored Sep 20, 2024
1 parent 1845802 commit 41ffa8d
Show file tree
Hide file tree
Showing 9 changed files with 434 additions and 77 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/solarlog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from .const import CONF_HAS_PWD
from .coordinator import SolarLogCoordinator

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -57,12 +58,13 @@ async def async_migrate_entry(
entity.entity_id, new_unique_id=new_uid
)

if config_entry.minor_version < 3:
# migrate config_entry
new = {**config_entry.data}
new["extended_data"] = False
new[CONF_HAS_PWD] = False

hass.config_entries.async_update_entry(
config_entry, data=new, minor_version=2, version=1
config_entry, data=new, minor_version=3, version=1
)

_LOGGER.debug(
Expand Down
128 changes: 115 additions & 13 deletions homeassistant/components/solarlog/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
"""Config flow for solarlog integration."""

from collections.abc import Mapping
import logging
from typing import TYPE_CHECKING, Any
from urllib.parse import ParseResult, urlparse

from solarlog_cli.solarlog_connector import SolarLogConnector
from solarlog_cli.solarlog_exceptions import SolarLogConnectionError, SolarLogError
from solarlog_cli.solarlog_exceptions import (
SolarLogAuthenticationError,
SolarLogConnectionError,
SolarLogError,
)
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD
from homeassistant.util import slugify

from .const import DEFAULT_HOST, DEFAULT_NAME, DOMAIN
from . import SolarlogConfigEntry
from .const import CONF_HAS_PWD, DEFAULT_HOST, DEFAULT_NAME, DOMAIN

_LOGGER = logging.getLogger(__name__)


class SolarLogConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for solarlog."""

_entry: SolarlogConfigEntry | None = None
VERSION = 1
MINOR_VERSION = 2
MINOR_VERSION = 3

def __init__(self) -> None:
"""Initialize the config flow."""
self._errors: dict = {}
self._user_input: dict = {}

def _parse_url(self, host: str) -> str:
"""Return parsed host url."""
Expand All @@ -51,6 +59,23 @@ async def _test_connection(self, host: str) -> bool:

return True

async def _test_extended_data(self, host: str, pwd: str = "") -> bool:
"""Check if we get extended data from Solar-Log device."""
response: bool = False
solarlog = SolarLogConnector(host, password=pwd)
try:
response = await solarlog.test_extended_data_available()
except SolarLogAuthenticationError:
self._errors = {CONF_HOST: "password_error"}
response = False
except SolarLogError:
self._errors = {CONF_HOST: "unknown"}
response = False
finally:
await solarlog.client.close()

return response

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
Expand All @@ -64,6 +89,10 @@ async def async_step_user(
user_input[CONF_NAME] = slugify(user_input[CONF_NAME])

if await self._test_connection(user_input[CONF_HOST]):
if user_input[CONF_HAS_PWD]:
self._user_input = user_input
return await self.async_step_password()

return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
Expand All @@ -76,7 +105,33 @@ async def async_step_user(
{
vol.Required(CONF_NAME, default=user_input[CONF_NAME]): str,
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): str,
vol.Required("extended_data", default=False): bool,
vol.Required(CONF_HAS_PWD, default=False): bool,
}
),
errors=self._errors,
)

async def async_step_password(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Step when user sets password ."""
self._errors = {}
if user_input is not None:
if await self._test_extended_data(
self._user_input[CONF_HOST], user_input[CONF_PASSWORD]
):
self._user_input |= user_input
return self.async_create_entry(
title=self._user_input[CONF_NAME], data=self._user_input
)
else:
user_input = {CONF_PASSWORD: ""}

return self.async_show_form(
step_id="password",
data_schema=vol.Schema(
{
vol.Required(CONF_PASSWORD): str,
}
),
errors=self._errors,
Expand All @@ -93,19 +148,66 @@ async def async_step_reconfigure(
assert entry is not None

if user_input is not None:
return self.async_update_reload_and_abort(
entry,
reason="reconfigure_successful",
data={**entry.data, **user_input},
)
if not user_input[CONF_HAS_PWD] or user_input.get(CONF_PASSWORD, "") == "":
user_input[CONF_PASSWORD] = ""
user_input[CONF_HAS_PWD] = False
return self.async_update_reload_and_abort(
entry,
reason="reconfigure_successful",
data={**entry.data, **user_input},
)

if await self._test_extended_data(
entry.data[CONF_HOST], user_input.get(CONF_PASSWORD, "")
):
# if password has been provided, only save if extended data is available
return self.async_update_reload_and_abort(
entry,
reason="reconfigure_successful",
data={**entry.data, **user_input},
)

return self.async_show_form(
step_id="reconfigure",
data_schema=vol.Schema(
{
vol.Required(
"extended_data", default=entry.data["extended_data"]
): bool,
vol.Optional(CONF_HAS_PWD, default=entry.data[CONF_HAS_PWD]): bool,
vol.Optional(CONF_PASSWORD): str,
}
),
)

async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle flow upon an API authentication error."""
self._entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reauthorization flow."""

assert self._entry is not None

if user_input and await self._test_extended_data(
self._entry.data[CONF_HOST], user_input.get(CONF_PASSWORD, "")
):
return self.async_update_reload_and_abort(
self._entry, data={**self._entry.data, **user_input}
)

data_schema = vol.Schema(
{
vol.Optional(
CONF_HAS_PWD, default=self._entry.data[CONF_HAS_PWD]
): bool,
vol.Optional(CONF_PASSWORD): str,
}
)
return self.async_show_form(
step_id="reauth_confirm",
data_schema=data_schema,
errors=self._errors,
)
2 changes: 2 additions & 0 deletions homeassistant/components/solarlog/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
# Default config for solarlog.
DEFAULT_HOST = "http://solar-log"
DEFAULT_NAME = "solarlog"

CONF_HAS_PWD = "has_password"
42 changes: 35 additions & 7 deletions homeassistant/components/solarlog/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

from solarlog_cli.solarlog_connector import SolarLogConnector
from solarlog_cli.solarlog_exceptions import (
SolarLogAuthenticationError,
SolarLogConnectionError,
SolarLogUpdateError,
)
from solarlog_cli.solarlog_models import SolarlogData

from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

_LOGGER = logging.getLogger(__name__)
Expand All @@ -35,6 +36,7 @@ def __init__(self, hass: HomeAssistant, entry: SolarlogConfigEntry) -> None:
)

host_entry = entry.data[CONF_HOST]
password = entry.data.get("password", "")

url = urlparse(host_entry, "http")
netloc = url.netloc or url.path
Expand All @@ -45,12 +47,18 @@ def __init__(self, hass: HomeAssistant, entry: SolarlogConfigEntry) -> None:
self.host = url.geturl()

self.solarlog = SolarLogConnector(
self.host, entry.data["extended_data"], hass.config.time_zone
self.host,
tz=hass.config.time_zone,
password=password,
)

async def _async_setup(self) -> None:
"""Do initialization logic."""
if self.solarlog.extended_data:
_LOGGER.debug("Start async_setup")
logged_in = False
if self.solarlog.password != "":
logged_in = await self.renew_authentication()
if logged_in or await self.solarlog.test_extended_data_available():
device_list = await self.solarlog.update_device_list()
self.solarlog.set_enabled_devices({key: True for key in device_list})

Expand All @@ -63,11 +71,31 @@ async def _async_update_data(self) -> SolarlogData:
if self.solarlog.extended_data:
await self.solarlog.update_device_list()
data.inverter_data = await self.solarlog.update_inverter_data()
except SolarLogConnectionError as err:
raise ConfigEntryNotReady(err) from err
except SolarLogUpdateError as err:
raise UpdateFailed(err) from err
except SolarLogConnectionError as ex:
raise ConfigEntryNotReady(ex) from ex
except SolarLogAuthenticationError as ex:
if await self.renew_authentication():
# login was successful, update availability of extended data, retry data update
await self.solarlog.test_extended_data_available()
raise ConfigEntryNotReady from ex
raise ConfigEntryAuthFailed from ex
except SolarLogUpdateError as ex:
raise UpdateFailed(ex) from ex

_LOGGER.debug("Data successfully updated")

return data

async def renew_authentication(self) -> bool:
"""Renew access token for SolarLog API."""
logged_in = False
try:
logged_in = await self.solarlog.login()
except SolarLogAuthenticationError as ex:
raise ConfigEntryAuthFailed from ex
except (SolarLogConnectionError, SolarLogUpdateError) as ex:
raise ConfigEntryNotReady from ex

_LOGGER.debug("Credentials successfully updated? %s", logged_in)

return logged_in
25 changes: 22 additions & 3 deletions homeassistant/components/solarlog/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,45 @@
"data": {
"host": "[%key:common::config_flow::data::host%]",
"name": "The prefix to be used for your Solar-Log sensors",
"extended_data": "Get additional data from Solar-Log. Extended data is only accessible, if no password is set for the Solar-Log. Use at your own risk!"
"has_password": "I have the password for the Solar-Log user account."
},
"data_description": {
"host": "The hostname or IP address of your Solar-Log device."
"host": "The hostname or IP address of your Solar-Log device.",
"has_password": "The password is required, if the open JSON-API is deactivated or if you would like to access additional data provided by your Solar-Log device."
}
},
"password": {
"title": "Define your Solar-Log connection",
"data": {
"password": "[%key:common::config_flow::data::password%]"
},
"data_description": {
"password": "The password for the general user of your Solar-Log device."
}
},
"reauth_confirm": {
"description": "Update your credentials for Solar-Log device",
"data": {
"has_password": "[%key:component::solarlog::config::step::user::data::has_password%]",
"password": "[%key:common::config_flow::data::password%]"
}
},
"reconfigure": {
"title": "Configure SolarLog",
"data": {
"extended_data": "[%key:component::solarlog::config::step::user::data::extended_data%]"
"has_password": "[%key:component::solarlog::config::step::user::data::has_password%]"
}
}
},
"error": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"password_error": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
}
},
Expand Down
17 changes: 12 additions & 5 deletions tests/components/solarlog/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import pytest
from solarlog_cli.solarlog_models import InverterData, SolarlogData

from homeassistant.components.solarlog.const import DOMAIN as SOLARLOG_DOMAIN
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.components.solarlog.const import (
CONF_HAS_PWD,
DOMAIN as SOLARLOG_DOMAIN,
)
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD

from .const import HOST, NAME

Expand Down Expand Up @@ -36,9 +39,10 @@ def mock_config_entry() -> MockConfigEntry:
data={
CONF_HOST: HOST,
CONF_NAME: NAME,
"extended_data": True,
CONF_HAS_PWD: True,
CONF_PASSWORD: "pwd",
},
minor_version=2,
minor_version=3,
entry_id="ce5f5431554d101905d31797e1232da8",
)

Expand All @@ -55,11 +59,14 @@ def mock_solarlog_connector():
mock_solarlog_api = AsyncMock()
mock_solarlog_api.set_enabled_devices = MagicMock()
mock_solarlog_api.test_connection.return_value = True
mock_solarlog_api.test_extended_data_available.return_value = True
mock_solarlog_api.extended_data.return_value = True
mock_solarlog_api.update_data.return_value = data
mock_solarlog_api.update_device_list.return_value = INVERTER_DATA
mock_solarlog_api.update_device_list.return_value = DEVICE_LIST
mock_solarlog_api.update_inverter_data.return_value = INVERTER_DATA
mock_solarlog_api.device_name = {0: "Inverter 1", 1: "Inverter 2"}.get
mock_solarlog_api.device_enabled = {0: True, 1: False}.get
mock_solarlog_api.password.return_value = "pwd"

with (
patch(
Expand Down
Loading

0 comments on commit 41ffa8d

Please sign in to comment.