Skip to content

Commit

Permalink
Merge pull request #9 from geeks-r-us/dev
Browse files Browse the repository at this point in the history
Version 0.0.2
  • Loading branch information
geeks-r-us authored Aug 29, 2024
2 parents ee34ea2 + 88b342d commit 3c976a7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
46 changes: 38 additions & 8 deletions custom_components/maxstorage_ultimate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def get_mac_address(self, ip_address):
except subprocess.CalledProcessError as ex:
_LOGGER.error("Error getting MAC address: %s", ex)
return None
except FileNotFoundError as ex:
_LOGGER.error("Error getting MAC address: %s", ex)
return None

async def ensure_authenticated(self):
"""Ensure that the session is authenticated."""
Expand All @@ -102,14 +105,37 @@ async def _read_device_info(self, response: aiohttp.ClientResponse):
content = await response.text()
soup = BeautifulSoup(content, "html.parser")

elements = soup.find_all("p", style="white-space: normal;padding: 5px")
for element in elements:
entries = element.find_all("b")
for entry in entries:
if entry.next_sibling is not None:
self.device_info[
entry.text.strip().replace(":", "")
] = entry.next_sibling.strip()
# Find all potential keys (bold text could be considered a key)
keys = soup.find_all(["b", "div"])
current_key = None

for element in keys:
text = element.get_text().strip().replace(":", "")

# If the current element is a key
if text and (
text
in [
"Anlagenname",
"MasterController-Nummer",
"Firmware-Version",
"Hardware-Version",
"Ident",
]
):
current_key = text
# Directly following sibling logic for Version 3.4.0
if element.next_sibling and not element.next_sibling.name:
self.device_info[current_key] = element.next_sibling.strip()
# Following div logic for Version 3.4.3
elif element.find_next_sibling():
sibling = element.find_next_sibling()
if sibling and not sibling.find(["b"]):
self.device_info[current_key] = sibling.get_text(strip=True)

if not self.device_info:
_LOGGER.error("Failed to parse device info from response: %s", content)
raise DataParserError("Failed to parse device info")

def get_device_info(self):
"""Return the device info."""
Expand Down Expand Up @@ -148,6 +174,10 @@ async def close(self):
await self.session.close()


class DataParserError(Exception):
"""Exception raised when data parsing fails."""


class AuthenticationFailedError(Exception):
"""Exception raised when authentication fails."""

Expand Down
52 changes: 51 additions & 1 deletion custom_components/maxstorage_ultimate/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_NAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
Expand Down Expand Up @@ -211,3 +211,53 @@ def _show_setup_form_confirm(
description_placeholders={"name": self._name},
errors=errors or {},
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> MaxStorageOptionsFlowHandler:
"""Get the options flow for this handler."""
return MaxStorageOptionsFlowHandler(config_entry)


class MaxStorageOptionsFlowHandler(OptionsFlow):
"""Handle MaxStorage options."""

def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize MaxStorage options flow."""
self.config_entry = config_entry

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

options = self.config_entry.options
data_schema = vol.Schema(
{
vol.Required(
CONF_STORAGE_HOST,
default=options.get(
CONF_STORAGE_HOST, self.config_entry.data[CONF_STORAGE_HOST]
),
): str,
vol.Required(
CONF_STORAGE_USER,
default=options.get(
CONF_STORAGE_USER, self.config_entry.data[CONF_STORAGE_USER]
),
): str,
vol.Required(
CONF_STORAGE_PASSWORD,
default=options.get(
CONF_STORAGE_PASSWORD,
self.config_entry.data[CONF_STORAGE_PASSWORD],
),
): str,
}
)

return self.async_show_form(step_id="init", data_schema=data_schema)

0 comments on commit 3c976a7

Please sign in to comment.