mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-13 09:13:30 +01:00
feat: add Home Assistant and NodeRED adapters (#764)
Adapters for Home Assistant and NodeRED integration are added. Akkudoktor-EOS can now be run as Home Assistant add-on and standalone. As Home Assistant add-on EOS uses ingress to fully integrate the EOSdash dashboard in Home Assistant. The fix includes several bug fixes that are not directly related to the adapter implementation but are necessary to keep EOS running properly and to test and document the changes. * fix: development version scheme The development versioning scheme is adaptet to fit to docker and home assistant expectations. The new scheme is x.y.z and x.y.z.dev<hash>. Hash is only digits as expected by home assistant. Development version is appended by .dev as expected by docker. * fix: use mean value in interval on resampling for array When downsampling data use the mean value of all values within the new sampling interval. * fix: default battery ev soc and appliance wh Make the genetic simulation return default values for the battery SoC, electric vehicle SoC and appliance load if these assets are not used. * fix: import json string Strip outer quotes from JSON strings on import to be compliant to json.loads() expectation. * fix: default interval definition for import data Default interval must be defined in lowercase human definition to be accepted by pendulum. * fix: clearoutside schema change * feat: add adapters for integrations Adapters for Home Assistant and NodeRED integration are added. Akkudoktor-EOS can now be run as Home Assistant add-on and standalone. As Home Assistant add-on EOS uses ingress to fully integrate the EOSdash dashboard in Home Assistant. * feat: allow eos to be started with root permissions and drop priviledges Home assistant starts all add-ons with root permissions. Eos now drops root permissions if an applicable user is defined by paramter --run_as_user. The docker image defines the user eos to be used. * feat: make eos supervise and monitor EOSdash Eos now not only starts EOSdash but also monitors EOSdash during runtime and restarts EOSdash on fault. EOSdash logging is captured by EOS and forwarded to the EOS log to provide better visibility. * feat: add duration to string conversion Make to_duration to also return the duration as string on request. * chore: Use info logging to report missing optimization parameters In parameter preparation for automatic optimization an error was logged for missing paramters. Log is now down using the info level. * chore: make EOSdash use the EOS data directory for file import/ export EOSdash use the EOS data directory for file import/ export by default. This allows to use the configuration import/ export function also within docker images. * chore: improve EOSdash config tab display Improve display of JSON code and add more forms for config value update. * chore: make docker image file system layout similar to home assistant Only use /data directory for persistent data. This is handled as a docker volume. The /data volume is mapped to ~/.local/share/net.akkudoktor.eos if using docker compose. * chore: add home assistant add-on development environment Add VSCode devcontainer and task definition for home assistant add-on development. * chore: improve documentation
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
from typing import Any, Dict, List, Optional, Sequence, TypeVar, Union
|
||||
from collections.abc import Sequence
|
||||
from typing import Any, Dict, List, Optional, TypeVar, Union
|
||||
|
||||
import requests
|
||||
from loguru import logger
|
||||
@@ -7,6 +8,7 @@ from monsterui.franken import (
|
||||
H3,
|
||||
H4,
|
||||
Card,
|
||||
CardTitle,
|
||||
Details,
|
||||
Div,
|
||||
DividerLine,
|
||||
@@ -15,6 +17,7 @@ from monsterui.franken import (
|
||||
Form,
|
||||
Grid,
|
||||
Input,
|
||||
LabelCheckboxX,
|
||||
P,
|
||||
Summary,
|
||||
UkIcon,
|
||||
@@ -25,7 +28,15 @@ from pydantic_core import PydanticUndefined
|
||||
from akkudoktoreos.config.config import ConfigEOS
|
||||
from akkudoktoreos.core.pydantic import PydanticBaseModel
|
||||
from akkudoktoreos.prediction.pvforecast import PVForecastPlaneSetting
|
||||
from akkudoktoreos.server.dash.components import ConfigCard
|
||||
from akkudoktoreos.server.dash.components import (
|
||||
ConfigCard,
|
||||
JsonView,
|
||||
TextView,
|
||||
make_config_update_list_form,
|
||||
make_config_update_map_form,
|
||||
make_config_update_value_form,
|
||||
)
|
||||
from akkudoktoreos.server.dash.context import request_url_for
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
@@ -33,6 +44,14 @@ T = TypeVar("T")
|
||||
# Dictionary of config names and associated dictionary with keys "value", "result", "error", "open".
|
||||
config_update_latest: dict[str, dict[str, Optional[Union[str, bool]]]] = {}
|
||||
|
||||
# Current state of config displayed
|
||||
config_visible: dict[str, dict] = {
|
||||
"config-visible-read-only": {
|
||||
"label": "Configuration (read-only)",
|
||||
"visible": False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def get_nested_value(
|
||||
dictionary: Union[Dict[str, Any], List[Any]],
|
||||
@@ -178,9 +197,9 @@ def resolve_nested_types(field_type: Any, parent_types: list[str]) -> list[tuple
|
||||
return resolved_types
|
||||
|
||||
|
||||
def configuration(
|
||||
def create_config_details(
|
||||
model: type[PydanticBaseModel], values: dict, values_prefix: list[str] = []
|
||||
) -> list[dict]:
|
||||
) -> dict[str, dict]:
|
||||
"""Generate configuration details based on provided values and model metadata.
|
||||
|
||||
Args:
|
||||
@@ -189,9 +208,9 @@ def configuration(
|
||||
values_prefix (list[str]): A list of parent type names that prefixes the model values in the values.
|
||||
|
||||
Returns:
|
||||
list[dict]: A sorted list of configuration details, each represented as a dictionary.
|
||||
dict[dict]: A dictionary of configuration details, each represented as a dictionary.
|
||||
"""
|
||||
configs = []
|
||||
config_details: dict[str, dict] = {}
|
||||
inner_types: set[type[PydanticBaseModel]] = set()
|
||||
|
||||
for field_name, field_info in list(model.model_fields.items()) + list(
|
||||
@@ -244,7 +263,7 @@ def configuration(
|
||||
.replace("NoneType", "None")
|
||||
.replace("<class 'float'>", "float")
|
||||
)
|
||||
configs.append(config)
|
||||
config_details[str(config["name"])] = config
|
||||
found_basic = True
|
||||
else:
|
||||
new_parent_types = parent_types + nested_parent_types
|
||||
@@ -258,18 +277,18 @@ def configuration(
|
||||
)
|
||||
|
||||
extract_nested_models(field_info, [field_name])
|
||||
return sorted(configs, key=lambda x: x["name"])
|
||||
return config_details
|
||||
|
||||
|
||||
def get_configuration(eos_host: str, eos_port: Union[str, int]) -> list[dict]:
|
||||
"""Fetch and process configuration data from the specified EOS server.
|
||||
def get_config(eos_host: str, eos_port: Union[str, int]) -> dict[str, Any]:
|
||||
"""Fetch configuration data from the specified EOS server.
|
||||
|
||||
Args:
|
||||
eos_host (str): The hostname of the EOS server.
|
||||
eos_port (Union[str, int]): The port of the EOS server.
|
||||
|
||||
Returns:
|
||||
List[dict]: A list of processed configuration entries.
|
||||
dict[str, Any]: A dict of configuration data.
|
||||
"""
|
||||
server = f"http://{eos_host}:{eos_port}"
|
||||
|
||||
@@ -284,7 +303,7 @@ def get_configuration(eos_host: str, eos_port: Union[str, int]) -> list[dict]:
|
||||
warning_msg = f"Can not retrieve configuration from {server}: {e}, {detail}"
|
||||
logger.warning(warning_msg)
|
||||
|
||||
return configuration(ConfigEOS, config)
|
||||
return config
|
||||
|
||||
|
||||
def ConfigPlanesCard(
|
||||
@@ -341,7 +360,7 @@ def ConfigPlanesCard(
|
||||
# Create cards for all planes
|
||||
rows = []
|
||||
for i in range(0, max_planes):
|
||||
plane_config = configuration(
|
||||
plane_config = create_config_details(
|
||||
PVForecastPlaneSetting(),
|
||||
eos_planes_config,
|
||||
values_prefix=["pvforecast", "planes", str(i)],
|
||||
@@ -352,10 +371,12 @@ def ConfigPlanesCard(
|
||||
plane_value = json.dumps(eos_planes[i])
|
||||
else:
|
||||
plane_value = json.dumps(None)
|
||||
for config in plane_config:
|
||||
for config_key in sorted(plane_config.keys()):
|
||||
config = plane_config[config_key]
|
||||
update_error = config_update_latest.get(config["name"], {}).get("error") # type: ignore
|
||||
update_value = config_update_latest.get(config["name"], {}).get("value") # type: ignore
|
||||
update_open = config_update_latest.get(config["name"], {}).get("open") # type: ignore
|
||||
update_form_factory = None
|
||||
if update_open:
|
||||
planes_update_open = True
|
||||
plane_update_open = True
|
||||
@@ -368,6 +389,12 @@ def ConfigPlanesCard(
|
||||
error_msg = "update_error or update_value or update_open of wrong type."
|
||||
logger.error(error_msg)
|
||||
raise TypeError(error_msg)
|
||||
if config["name"].endswith("pvtechchoice"):
|
||||
update_form_factory = make_config_update_value_form(
|
||||
["crystSi", "CIS", "CdTe", "Unknown"]
|
||||
)
|
||||
elif config["name"].endswith("mountingplace"):
|
||||
update_form_factory = make_config_update_value_form(["free", "building"])
|
||||
plane_rows.append(
|
||||
ConfigCard(
|
||||
config["name"],
|
||||
@@ -380,6 +407,7 @@ def ConfigPlanesCard(
|
||||
update_error,
|
||||
update_value,
|
||||
update_open,
|
||||
update_form_factory,
|
||||
)
|
||||
)
|
||||
rows.append(
|
||||
@@ -396,7 +424,7 @@ def ConfigPlanesCard(
|
||||
P(read_only),
|
||||
),
|
||||
),
|
||||
P(plane_value),
|
||||
JsonView(json.loads(plane_value)),
|
||||
),
|
||||
cls="list-none",
|
||||
),
|
||||
@@ -421,12 +449,12 @@ def ConfigPlanesCard(
|
||||
P(read_only),
|
||||
),
|
||||
),
|
||||
P(value),
|
||||
JsonView(json.loads(value)),
|
||||
),
|
||||
cls="list-none",
|
||||
),
|
||||
Grid(
|
||||
P(description),
|
||||
TextView(description),
|
||||
P(config_type),
|
||||
),
|
||||
# Default
|
||||
@@ -441,9 +469,10 @@ def ConfigPlanesCard(
|
||||
DivRAligned(P("update")),
|
||||
Grid(
|
||||
Form(
|
||||
Input(value="update", type="hidden", id="action"),
|
||||
Input(value=config_name, type="hidden", id="key"),
|
||||
Input(value=planes_update_value, type="text", id="value"),
|
||||
hx_put="/eosdash/configuration",
|
||||
hx_put=request_url_for("/eosdash/configuration"),
|
||||
hx_target="#page-content",
|
||||
hx_swap="innerHTML",
|
||||
),
|
||||
@@ -454,7 +483,7 @@ def ConfigPlanesCard(
|
||||
# Last error
|
||||
Grid(
|
||||
DivRAligned(P("update error")),
|
||||
P(planes_update_error),
|
||||
TextView(planes_update_error),
|
||||
)
|
||||
if planes_update_error
|
||||
else None,
|
||||
@@ -468,33 +497,150 @@ def ConfigPlanesCard(
|
||||
|
||||
|
||||
def Configuration(
|
||||
eos_host: str, eos_port: Union[str, int], configuration: Optional[list[dict]] = None
|
||||
eos_host: str,
|
||||
eos_port: Union[str, int],
|
||||
data: Optional[dict] = None,
|
||||
) -> Div:
|
||||
"""Create a visual representation of the configuration.
|
||||
|
||||
Args:
|
||||
eos_host (str): The hostname of the EOS server.
|
||||
eos_port (Union[str, int]): The port of the EOS server.
|
||||
configuration (Optional[list[dict]]): Optional configuration. If not provided it will be
|
||||
retrievd from EOS.
|
||||
data (Optional[dict], optional): Incoming data to trigger config actions. Defaults to None.
|
||||
|
||||
Returns:
|
||||
rows: Rows of configuration details.
|
||||
"""
|
||||
if not configuration:
|
||||
configuration = get_configuration(eos_host, eos_port)
|
||||
global config_visible
|
||||
dark = False
|
||||
|
||||
if data and data.get("action", None):
|
||||
if data.get("dark", None) == "true":
|
||||
dark = True
|
||||
if data["action"] == "visible":
|
||||
renderer = data.get("renderer", None)
|
||||
if renderer:
|
||||
config_visible[renderer]["visible"] = bool(data.get(f"{renderer}-visible", False))
|
||||
elif data["action"] == "update":
|
||||
# This data contains a new value for key
|
||||
key = data["key"]
|
||||
value_json_str: str = data.get("value", "")
|
||||
try:
|
||||
value = json.loads(value_json_str)
|
||||
except:
|
||||
if value_json_str in ("None", "none", "Null", "null"):
|
||||
value = None
|
||||
else:
|
||||
value = value_json_str
|
||||
|
||||
error = None
|
||||
config = None
|
||||
try:
|
||||
server = f"http://{eos_host}:{eos_port}"
|
||||
path = key.replace(".", "/")
|
||||
response = requests.put(f"{server}/v1/config/{path}", json=value, timeout=10)
|
||||
response.raise_for_status()
|
||||
config = response.json()
|
||||
except requests.exceptions.HTTPError as err:
|
||||
try:
|
||||
# Try to get 'detail' from the JSON response
|
||||
detail = response.json().get(
|
||||
"detail", f"No error details for value '{value}' '{response.text}'"
|
||||
)
|
||||
except ValueError:
|
||||
# Response is not JSON
|
||||
detail = f"No error details for value '{value}' '{response.text}'"
|
||||
error = f"Can not set {key} on {server}: {err}, {detail}"
|
||||
# Mark all updates as closed
|
||||
for k in config_update_latest:
|
||||
config_update_latest[k]["open"] = False
|
||||
# Remember this update as latest one
|
||||
config_update_latest[key] = {
|
||||
"error": error,
|
||||
"result": config,
|
||||
"value": value_json_str,
|
||||
"open": True,
|
||||
}
|
||||
|
||||
# (Re-)read configuration details to be shure we display actual data
|
||||
config = get_config(eos_host, eos_port)
|
||||
|
||||
# Process configuration data
|
||||
config_details = create_config_details(ConfigEOS, config)
|
||||
|
||||
ConfigMenu = Card(
|
||||
# CheckboxGroup to toggle config data visibility
|
||||
Grid(
|
||||
*[
|
||||
LabelCheckboxX(
|
||||
label=config_visible[renderer]["label"],
|
||||
id=f"{renderer}-visible",
|
||||
name=f"{renderer}-visible",
|
||||
value="true",
|
||||
checked=config_visible[renderer]["visible"],
|
||||
hx_post=request_url_for("/eosdash/configuration"),
|
||||
hx_target="#page-content",
|
||||
hx_swap="innerHTML",
|
||||
hx_vals='js:{ "action": "visible", "renderer": '
|
||||
+ '"'
|
||||
+ f"{renderer}"
|
||||
+ '", '
|
||||
+ '"dark": window.matchMedia("(prefers-color-scheme: dark)").matches '
|
||||
+ "}",
|
||||
# lbl_cls=f"text-{solution_color[renderer]}",
|
||||
)
|
||||
for renderer in list(config_visible.keys())
|
||||
],
|
||||
cols=4,
|
||||
),
|
||||
header=CardTitle("Choose What's Shown"),
|
||||
)
|
||||
|
||||
rows = []
|
||||
last_category = ""
|
||||
# find some special configuration values
|
||||
max_planes = 0
|
||||
for config in configuration:
|
||||
if config["name"] == "pvforecast.max_planes":
|
||||
try:
|
||||
max_planes = int(config["value"])
|
||||
except:
|
||||
max_planes = 0
|
||||
try:
|
||||
max_planes = int(config_details["pvforecast.max_planes"]["value"])
|
||||
except:
|
||||
max_planes = 0
|
||||
logger.debug(f"max_planes: {max_planes}")
|
||||
|
||||
try:
|
||||
homeassistant_entity_ids = json.loads(
|
||||
config_details["adapter.homeassistant.homeassistant_entity_ids"]["value"]
|
||||
)
|
||||
except:
|
||||
homeassistant_entity_ids = []
|
||||
logger.debug(f"homeassistant_entity_ids: {homeassistant_entity_ids}")
|
||||
|
||||
eos_solution_entity_ids = []
|
||||
try:
|
||||
eos_solution_entity_ids = json.loads(
|
||||
config_details["adapter.homeassistant.eos_solution_entity_ids"]["value"]
|
||||
)
|
||||
except:
|
||||
eos_solution_entity_ids = []
|
||||
logger.debug(f"eos_solution_entity_ids {eos_solution_entity_ids}")
|
||||
|
||||
eos_device_instruction_entity_ids = []
|
||||
try:
|
||||
eos_device_instruction_entity_ids = json.loads(
|
||||
config_details["adapter.homeassistant.eos_device_instruction_entity_ids"]["value"]
|
||||
)
|
||||
except:
|
||||
eos_device_instruction_entity_ids = []
|
||||
logger.debug(f"eos_device_instruction_entity_ids {eos_device_instruction_entity_ids}")
|
||||
|
||||
devices_measurement_keys = []
|
||||
try:
|
||||
devices_measurement_keys = json.loads(config_details["devices.measurement_keys"]["value"])
|
||||
except:
|
||||
devices_measurement_keys = []
|
||||
logger.debug(f"devices_measurement_keys {devices_measurement_keys}")
|
||||
|
||||
# build visual representation
|
||||
for config in configuration:
|
||||
for config_key in sorted(config_details.keys()):
|
||||
config = config_details[config_key]
|
||||
category = config["name"].split(".")[0]
|
||||
if category != last_category:
|
||||
rows.append(H3(category))
|
||||
@@ -512,6 +658,12 @@ def Configuration(
|
||||
error_msg = "update_error or update_value or update_open of wrong type."
|
||||
logger.error(error_msg)
|
||||
raise TypeError(error_msg)
|
||||
if (
|
||||
not config_visible["config-visible-read-only"]["visible"]
|
||||
and config["read-only"] != "rw"
|
||||
):
|
||||
# Do not display read only values
|
||||
continue
|
||||
if (
|
||||
config["type"]
|
||||
== "Optional[list[akkudoktoreos.prediction.pvforecast.PVForecastPlaneSetting]]"
|
||||
@@ -532,7 +684,47 @@ def Configuration(
|
||||
update_open,
|
||||
)
|
||||
)
|
||||
else:
|
||||
elif not config["deprecated"]:
|
||||
update_form_factory = None
|
||||
if config["name"].endswith(".provider"):
|
||||
# Special configuration for prediction provider setting
|
||||
try:
|
||||
provider_ids = json.loads(config_details[config["name"] + "s"]["value"])
|
||||
except:
|
||||
provider_ids = []
|
||||
if config["type"].startswith("Optional[list"):
|
||||
update_form_factory = make_config_update_list_form(provider_ids)
|
||||
else:
|
||||
provider_ids.append("None")
|
||||
update_form_factory = make_config_update_value_form(provider_ids)
|
||||
elif config["name"].startswith("adapter.homeassistant.config_entity_ids"):
|
||||
# Home Assistant adapter config entities
|
||||
update_form_factory = make_config_update_map_form(None, homeassistant_entity_ids)
|
||||
elif config["name"].startswith("adapter.homeassistant.load_emr_entity_ids"):
|
||||
# Home Assistant adapter load energy meter readings entities
|
||||
update_form_factory = make_config_update_list_form(homeassistant_entity_ids)
|
||||
elif config["name"].startswith("adapter.homeassistant.pv_production_emr_entity_ids"):
|
||||
# Home Assistant adapter pv energy meter readings entities
|
||||
update_form_factory = make_config_update_list_form(homeassistant_entity_ids)
|
||||
elif config["name"].startswith("adapter.homeassistant.device_measurement_entity_ids"):
|
||||
# Home Assistant adapter device measurement entities
|
||||
update_form_factory = make_config_update_map_form(
|
||||
devices_measurement_keys, homeassistant_entity_ids
|
||||
)
|
||||
elif config["name"].startswith("adapter.homeassistant.device_instruction_entity_ids"):
|
||||
# Home Assistant adapter device instruction entities
|
||||
update_form_factory = make_config_update_list_form(
|
||||
eos_device_instruction_entity_ids
|
||||
)
|
||||
elif config["name"].startswith("adapter.homeassistant.solution_entity_ids"):
|
||||
# Home Assistant adapter optimization solution entities
|
||||
update_form_factory = make_config_update_list_form(eos_solution_entity_ids)
|
||||
elif config["name"].startswith("ems.mode"):
|
||||
# Energy managemnt mode
|
||||
update_form_factory = make_config_update_value_form(
|
||||
["OPTIMIZATION", "PREDICTION", "None"]
|
||||
)
|
||||
|
||||
rows.append(
|
||||
ConfigCard(
|
||||
config["name"],
|
||||
@@ -545,61 +737,8 @@ def Configuration(
|
||||
update_error,
|
||||
update_value,
|
||||
update_open,
|
||||
update_form_factory,
|
||||
)
|
||||
)
|
||||
return Div(*rows, cls="space-y-4")
|
||||
|
||||
|
||||
def ConfigKeyUpdate(eos_host: str, eos_port: Union[str, int], key: str, value: str) -> P:
|
||||
"""Update configuration key and create a visual representation of the configuration.
|
||||
|
||||
Args:
|
||||
eos_host (str): The hostname of the EOS server.
|
||||
eos_port (Union[str, int]): The port of the EOS server.
|
||||
key (str): configuration key in dot notation
|
||||
value (str): configuration value as json string
|
||||
|
||||
Returns:
|
||||
rows: Rows of configuration details.
|
||||
"""
|
||||
server = f"http://{eos_host}:{eos_port}"
|
||||
path = key.replace(".", "/")
|
||||
try:
|
||||
data = json.loads(value)
|
||||
except:
|
||||
if value in ("None", "none", "Null", "null"):
|
||||
data = None
|
||||
else:
|
||||
data = value
|
||||
|
||||
error = None
|
||||
config = None
|
||||
try:
|
||||
response = requests.put(f"{server}/v1/config/{path}", json=data, timeout=10)
|
||||
response.raise_for_status()
|
||||
config = response.json()
|
||||
except requests.exceptions.HTTPError as err:
|
||||
try:
|
||||
# Try to get 'detail' from the JSON response
|
||||
detail = response.json().get(
|
||||
"detail", f"No error details for data '{data}' '{response.text}'"
|
||||
)
|
||||
except ValueError:
|
||||
# Response is not JSON
|
||||
detail = f"No error details for data '{data}' '{response.text}'"
|
||||
error = f"Can not set {key} on {server}: {err}, {detail}"
|
||||
# Mark all updates as closed
|
||||
for k in config_update_latest:
|
||||
config_update_latest[k]["open"] = False
|
||||
# Remember this update as latest one
|
||||
config_update_latest[key] = {
|
||||
"error": error,
|
||||
"result": config,
|
||||
"value": value,
|
||||
"open": True,
|
||||
}
|
||||
if error or config is None:
|
||||
# Reread configuration to be shure we display actual data
|
||||
return Configuration(eos_host, eos_port)
|
||||
# Use configuration already provided
|
||||
return Configuration(eos_host, eos_port, configuration(ConfigEOS, config))
|
||||
return Div(ConfigMenu, *rows, cls="space-y-3")
|
||||
|
||||
Reference in New Issue
Block a user