mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-01-01 08:16:18 +00: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,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
@@ -44,73 +45,116 @@ class TestServer:
|
||||
|
||||
|
||||
class TestServerStartStop:
|
||||
def test_server_start_eosdash(self, tmpdir):
|
||||
"""Test the EOSdash server startup from EOS."""
|
||||
# Do not use any fixture as this will make pytest the owner of the EOSdash port.
|
||||
host = get_default_host()
|
||||
port = 8503
|
||||
eosdash_host = host
|
||||
eosdash_port = 8504
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_server_start_eosdash(self, config_eos, monkeypatch, tmp_path):
|
||||
"""Test the EOSdash server startup from EOS.
|
||||
|
||||
Do not use any fixture as this will make pytest the owner of the EOSdash port.
|
||||
|
||||
Tests that:
|
||||
1. EOSdash starts via the supervisor
|
||||
2. The /eosdash/health endpoint returns OK
|
||||
3. EOSdash reports correct status and version
|
||||
4. EOSdash can be terminated cleanly
|
||||
"""
|
||||
eos_dir = tmp_path
|
||||
monkeypatch.setenv("EOS_DIR", str(eos_dir))
|
||||
monkeypatch.setenv("EOS_CONFIG_DIR", str(eos_dir))
|
||||
|
||||
# Import with environment vars set to prevent creation of EOS.config.json in wrong dir.
|
||||
from akkudoktoreos.server.rest.starteosdash import run_eosdash_supervisor
|
||||
|
||||
config_eos.server.host = get_default_host()
|
||||
config_eos.server.port = 8503
|
||||
config_eos.server.eosdash_host = config_eos.server.host
|
||||
config_eos.server.eosdash_port = 8504
|
||||
timeout = 120
|
||||
|
||||
server = f"http://{host}:{port}"
|
||||
eosdash_server = f"http://{eosdash_host}:{eosdash_port}"
|
||||
eos_dir = str(tmpdir)
|
||||
eosdash_server = f"http://{config_eos.server.eosdash_host}:{config_eos.server.eosdash_port}"
|
||||
|
||||
# Cleanup any EOS and EOSdash process left.
|
||||
cleanup_eos_eosdash(host, port, eosdash_host, eosdash_port, timeout)
|
||||
|
||||
# Import after test setup to prevent creation of config file before test
|
||||
from akkudoktoreos.server.eos import start_eosdash
|
||||
|
||||
# Port may be blocked
|
||||
assert wait_for_port_free(eosdash_port, timeout=120, waiting_app_name="EOSdash")
|
||||
|
||||
process = start_eosdash(
|
||||
host=eosdash_host,
|
||||
port=eosdash_port,
|
||||
eos_host=host,
|
||||
eos_port=port,
|
||||
log_level="DEBUG",
|
||||
access_log=False,
|
||||
reload=False,
|
||||
eos_dir=eos_dir,
|
||||
eos_config_dir=eos_dir,
|
||||
cleanup_eos_eosdash(
|
||||
host=config_eos.server.host,
|
||||
port=config_eos.server.port,
|
||||
eosdash_host=config_eos.server.eosdash_host,
|
||||
eosdash_port=config_eos.server.eosdash_port,
|
||||
server_timeout=timeout,
|
||||
)
|
||||
|
||||
# Assure EOSdash is up
|
||||
# Port may be blocked
|
||||
assert wait_for_port_free(config_eos.server.eosdash_port, timeout=120, waiting_app_name="EOSdash")
|
||||
|
||||
|
||||
"""Start the EOSdash supervisor as a background task for testing."""
|
||||
task = asyncio.create_task(run_eosdash_supervisor())
|
||||
|
||||
# give the supervisor some time to begin starting EOSdash
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# ---------------------------------
|
||||
# Wait for health endpoint to come up
|
||||
# ---------------------------------
|
||||
startup = False
|
||||
error = ""
|
||||
|
||||
for retries in range(int(timeout / 3)):
|
||||
try:
|
||||
result = requests.get(f"{eosdash_server}/eosdash/health", timeout=2)
|
||||
if result.status_code == HTTPStatus.OK:
|
||||
resp = requests.get(f"{eosdash_server}/eosdash/health", timeout=2)
|
||||
if resp.status_code == HTTPStatus.OK:
|
||||
startup = True
|
||||
break
|
||||
error = f"{result.status_code}, {str(result.content)}"
|
||||
error = f"{resp.status_code}, {str(resp.content)}"
|
||||
except Exception as ex:
|
||||
error = str(ex)
|
||||
time.sleep(3)
|
||||
|
||||
assert startup, f"Connection to {eosdash_server}/eosdash/health failed: {error}"
|
||||
health = result.json()
|
||||
assert health["status"] == "alive"
|
||||
assert health["version"] == __version__
|
||||
await asyncio.sleep(3)
|
||||
|
||||
# Shutdown eosdash
|
||||
# Graceful shutdown of the background task
|
||||
# Do it before any assert
|
||||
task.cancel()
|
||||
try:
|
||||
result = requests.get(f"{eosdash_server}/eosdash/health", timeout=2)
|
||||
if result.status_code == HTTPStatus.OK:
|
||||
pid = result.json()["pid"]
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
time.sleep(1)
|
||||
result = requests.get(f"{eosdash_server}/eosdash/health", timeout=2)
|
||||
assert result.status_code != HTTPStatus.OK
|
||||
except:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# Cleanup any EOS and EOSdash process left.
|
||||
cleanup_eos_eosdash(host, port, eosdash_host, eosdash_port, timeout)
|
||||
assert startup, f"Connection to {eosdash_server}/eosdash/health failed: {error}"
|
||||
|
||||
health = resp.json()
|
||||
assert health.get("status") == "alive"
|
||||
assert health.get("version") == __version__
|
||||
|
||||
# ---------------------------------
|
||||
# Shutdown EOSdash (as provided)
|
||||
# ---------------------------------
|
||||
try:
|
||||
resp = requests.get(f"{eosdash_server}/eosdash/health", timeout=2)
|
||||
if resp.status_code == HTTPStatus.OK:
|
||||
pid = resp.json().get("pid")
|
||||
assert pid is not None, "EOSdash did not report a PID"
|
||||
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
time.sleep(1)
|
||||
|
||||
# After shutdown, the server should not respond OK anymore
|
||||
try:
|
||||
resp2 = requests.get(f"{eosdash_server}/eosdash/health", timeout=2)
|
||||
assert resp2.status_code != HTTPStatus.OK
|
||||
except Exception:
|
||||
pass # expected
|
||||
except Exception:
|
||||
pass # ignore shutdown errors for safety
|
||||
|
||||
# ---------------------------------
|
||||
# Cleanup any leftover processes
|
||||
# ---------------------------------
|
||||
cleanup_eos_eosdash(
|
||||
host=config_eos.server.host,
|
||||
port=config_eos.server.port,
|
||||
eosdash_host=config_eos.server.eosdash_host,
|
||||
eosdash_port=config_eos.server.eosdash_port,
|
||||
server_timeout=timeout,
|
||||
)
|
||||
|
||||
@pytest.mark.skipif(os.name == "nt", reason="Server restart not supported on Windows")
|
||||
def test_server_restart(self, server_setup_for_function, is_system_test):
|
||||
|
||||
Reference in New Issue
Block a user