Skip to content

Commit 652f149

Browse files
Merge pull request #77 from GeoDerp/add-on-centralized-docker
Centralized docker file for all use cases
2 parents 19c1926 + 1211739 commit 652f149

File tree

9 files changed

+256
-68
lines changed

9 files changed

+256
-68
lines changed

.github/workflows/build_test.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: "EMHASS-Add-on Build Test"
2+
3+
on:
4+
pull_request:
5+
workflow_dispatch: #on manuall trigger
6+
jobs:
7+
build:
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
architecture: [amd64,aarch64,armhf,armv7]
12+
name: Test action build
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout the repository
16+
uses: actions/checkout@v4
17+
- name: Test_Build_${{ matrix.architecture }}
18+
uses: home-assistant/builder@master
19+
with:
20+
args: |
21+
--test \
22+
--${{ matrix.architecture }} \
23+
--target emhass

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Be patient, the installation may take some time depending on your hardware.
1818

1919
When the installation has finished go to the `Configuration` tab to set the add-on parameters.
2020

21+
## Developing EMHASS/EMHASS-Add-on
22+
- EMHASS
23+
- For those who want to develop the EMHASS package itself. Have a look at the [Develop page](https://emhass.readthedocs.io/en/latest/develop.html). *(EMHASS docs)*
24+
- EMHASS-Add-on
25+
- For those who want to test the Home Assistant integration of EMHASS. Have a look at [Test.md](./Test.md).
26+
2127
## License
2228

2329
MIT License

Test.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Testing EMHASS-Add-on
2+
3+
To test EMHASS-Add-on integration, you will need a Home Assistant (with Supervisor) environment.
4+
5+
The common Home Assistant options are:
6+
7+
- Adding EMHASS-Add-on into a pre-existing Home Assistant environment.
8+
- Testing EMHASS-Add-on on a Home Assistant inside a vertual test environment _(Using VS-Code)_.
9+
10+
See the following steps for both options.
11+
12+
_The following examples are for testing the EMHASS-Add-on integration _(Docker wrapper of EMHASS for Home Assistant)_. To develop/test the EMHASS Packadge itself, check EMHASS [Develop page](https://emhass.readthedocs.io/en/latest/develop.html)_.
13+
14+
_See [Test EMHASS-Add-On build](#Test-EMHASS-Add-On-build) for an example of testing Home Assistants Docker image build._
15+
16+
## Develop on VS-Code DevContainer with Home Assistant test environment
17+
18+
Using VS-Code DevContainers, you can generate a Home Assistant test environment for the Add-on before release. We can pull a version of the EMHASS package (required with EMHASS-Add-on) from a Git repo/branch, or via pip. Alternately, we can specify different pre-built EMHASS-Add-On versions _(EMHASS-Add-On Docker images)_ from DockerHub.
19+
20+
See the following steps:
21+
22+
- DockerHub:
23+
- Edit the `version` line from from [`config.yml`](./emhass/config.yml) to pull different versions of EMHASS-Add-On via DockerHub
24+
- Git/pip:
25+
- Comment out `image: "davidusb/image-{arch}-emhass"` line from [`config.yml`](./emhass/config.yml). This will tell the addon to build from the local Dockerfile and not pull Image from DockerHub
26+
- Git
27+
- Change `build_version: addon` in [_build.yaml_](./emhass/build.yaml) to `build_version: addon-git`. _(This overrides the pip version of EMHASS specified via [requirements.txt](./emhass/requirements.txt))_
28+
- To specify the Git repo and branch, change lines accordingly in [*build.yaml*](/emhass/build.yaml).
29+
- repo: `#build_repo: https://github.com/davidusb-geek/emhass.git #addon-git mode`
30+
- branch: `#build_branch: master #addon-git mode`
31+
32+
- pip
33+
- Change `emhass` version in [requirements.txt](/emhass/requirements.txt) to pull EMHASS via pip version.
34+
- You may need to modify the other python packages to different versions to match
35+
- Finally
36+
- Run _(if not already)_ the VS-Code DevContainer _(Shortcut: `F1` > `Dev Containers: Rebuild and Reopen in Container`)_ - This requires DevContainers to be operational. See [visualstudio.com - Developing inside a Container](https://code.visualstudio.com/docs/devcontainers/containers) for more info
37+
- Start VS-Code Task to start Home Assistant (`ctrl+shift+p`>`Tasks: Run Task`> `Start Home Assistant`)
38+
- Login to the generated HA Portal: `localhost:7123`
39+
- Navigate to Home Assistant: `Add-ons` > `ADD-ON STORE`
40+
- Install/Run and Test Add-on
41+
- For more infomation see Home Assistant's [local addon testing](https://developers.home-assistant.io/docs/add-ons/testing).
42+
43+
_Note: If, on run, the emhass version looks off. Try: uninstalling Add-on, `check for updates` on Add-on Store page, and re-installing._
44+
_If you have chosen Git, also try removing the `emhass` python package from requirements.txt ._
45+
46+
## Adding EMHASS-Add-on into pre-existing Home Assistant environment
47+
48+
If you would like to test a version of EMHASS-Add-on inside a pre-existing Home Assistant (with Supervisor) environment, see the following steps:
49+
50+
- With your preferred method of choice, clone the emhass-add-on repository to the addons folder
51+
- One method is to use the [`Home Assistant Add-on: SSH server`](https://github.com/home-assistant/addons/blob/master/ssh/DOCS.md) addon to add:
52+
- Install addon and click `OPEN WEB UI`
53+
- See [SSH Addon README](https://github.com/home-assistant/addons/blob/master/ssh/DOCS.md#installation) for install steps
54+
- Type commands:
55+
```bash
56+
cd ~/addons/
57+
git clone https://github.com/davidusb-geek/emhass-add-on
58+
```
59+
- With your preferred method of choice, indicate which EMHASS package to build with (`image`), or specify what built DockerHub version of EMHASS-Add-on (`version`) to use, in section of [`config.yml`](./emhass/config.yml) file:
60+
- Comment out the **image** line `image: "davidusb/image-{arch}-emhass"` if you wish to pull a EMHASS version from pip or Git repo/branch
61+
- ssh example:
62+
```bash
63+
sed -i.bak '/image:/ s/./#&/' ~/addons/emhass-add-on/emhass/config.yml
64+
```
65+
- Change **version** if you would like to pull in an older version of EMHASS-Add-on _(pre built Docker image)_ from DockerHub (Default state)
66+
- ssh example:
67+
```bash
68+
emhassVersion=0.6.5
69+
sed -i.bak "s/version:.*/version: $emhassVersion/g" ~/addons/emhass-add-on/emhass/config.yml
70+
```
71+
- If you want EMHASS from **pip**:
72+
- To specify the EMHASS pip version, modify the `emhass` version number in [requirements.txt](/emhass/requirements.txt)
73+
- ssh example:
74+
```bash
75+
emhassVersion=0.7.7
76+
sed -i.bak "s/emhass==.*/emhass==$emhassVersion/g" ~/addons/emhass-add-on/emhass/requirements.txt
77+
```
78+
79+
- If you want EMHASS from **Git**:
80+
- Tell the Add-on to use Git, and specify what EMHASS repo and branch you would like to pull.
81+
82+
- To tell Docker to pull from Git, change the build argument from `addon` to `addon-git` in the [_build.yaml_](./emhass/build.yaml).
83+
- ssh example:
84+
```bash
85+
sed -i.bak "s/build_version:.*/build_version: addon-git/g" ~/addons/emhass-add-on/emhass/build.yaml
86+
```
87+
- To specify the EMHASS Git repository and branch values, _(optional)_ change lines in [*build.yaml*](/emhass/build.yaml):
88+
- `#build_repo: https://github.com/davidusb-geek/emhass.git #addon-git mode`
89+
- `#build_branch: master #addon-git mode` _
90+
91+
- ssh example:
92+
93+
```bash
94+
repo=https://github.com/daviasdasdsasddusb-geek/emhass.git
95+
branch=masaddasdasdter
96+
97+
sed -i.bak "s%build_repo:\s.*%build_repo: $repo%g" ~/addons/emhass-add-on/emhass/build.yaml
98+
sed -i.bak "s/build_branch:\s.*/build_repo: $branch/g" ~/addons/emhass-add-on/emhass/build.yaml
99+
```
100+
- Finally:
101+
- head to Home Assistant: `Add-ons` > `ADD-ON STORE`
102+
- you should see an `EMHASS` Add-on under `Local add-ons`
103+
- If you don't, try hamburger button _(3 dots)_ on top right > check updates > refresh page
104+
- Install and test Add-on
105+
- Use the Supervisor logs _(on the config/logs page)_ to see any logs with the Add-on.
106+
107+
</br>
108+
109+
_Note: If, on run, the emhass version looks off. Try: uninstalling Add-on, check for updates on Add-on Store page, and re-installing._
110+
_If you have chosen Git, also try removing the emhass python package from requirements.txt ._
111+
112+
## Test EMHASS-Add-On build
113+
114+
You can test the EMHASS-Add-On docker image build using the Home Assistant builder.
115+
116+
**emhass-add-on local repo**
117+
118+
Linux example:
119+
120+
```bash
121+
architecture=amd64 #your host machine architecture
122+
123+
docker run --rm --privileged -v ~/.docker:/root/.docker -v ${PWD}:/data ghcr.io/home-assistant/${architecture}-builder:latest --test --${architecture} --target /data/emhass
124+
```
125+
126+
_confirm terminal directory is in root `emhass-add-on` folder_
127+
128+
**emhass-add-on Git repo**
129+
130+
Linux example:
131+
132+
```bash
133+
architecture=amd64 #your host machine architecture
134+
135+
repo=https://github.com/davidusb-geek/emhass-add-on.git #repo example
136+
branch=main #branch example
137+
138+
docker run --rm --privileged -v ~/.docker:/root/.docker ghcr.io/home-assistant/${architecture}-builder:latest --test --${architecture} --target emhass -r ${repo} -b ${branch}
139+
```

emhass/Dockerfile

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,89 @@
1-
ARG BUILD_FROM
2-
FROM ${BUILD_FROM}
1+
## EMHASS-Add-On Docker
32

4-
# switch working directory
5-
WORKDIR /usr/src
3+
#build_version options are: addon (default), addon-git
4+
ARG build_version=addon
5+
ARG BUILD_ARCH
66

7-
# copy the requirements file into the image
8-
COPY ./requirements.txt /usr/src/requirements.txt
9-
# COPY ./emhass-0.5.0-py3-none-any.whl /usr/src/emhass-0.5.0-py3-none-any.whl
7+
8+
FROM ghcr.io/home-assistant/$BUILD_ARCH-base-debian:bookworm AS base
9+
10+
WORKDIR /app
11+
COPY requirements.txt /app/
1012

1113
# Setup
1214
RUN apt-get update \
1315
&& apt-get install -y --no-install-recommends \
14-
libffi-dev \
15-
python3 \
16-
python3-pip \
17-
python3-dev \
18-
git \
19-
build-essential \
20-
gcc \
21-
coinor-cbc \
22-
coinor-libcbc-dev \
23-
libglpk-dev \
24-
glpk-utils \
25-
libhdf5-dev \
26-
libhdf5-serial-dev \
27-
netcdf-bin \
28-
libnetcdf-dev \
29-
pkg-config \
30-
gfortran \
31-
libatlas-base-dev \
16+
libffi-dev \
17+
python3 \
18+
python3-pip \
19+
python3-dev \
20+
git \
21+
build-essential \
22+
gcc \
23+
coinor-cbc \
24+
coinor-libcbc-dev \
25+
libglpk-dev \
26+
glpk-utils \
27+
libhdf5-dev \
28+
libhdf5-serial-dev \
29+
netcdf-bin \
30+
libnetcdf-dev \
31+
pkg-config \
32+
gfortran \
33+
libatlas-base-dev \
3234
&& ln -s /usr/include/hdf5/serial /usr/include/hdf5/include \
3335
&& export HDF5_DIR=/usr/include/hdf5 \
3436
&& pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U setuptools wheel \
3537
&& pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -r requirements.txt \
36-
# && pip3 install emhass-0.5.0-py3-none-any.whl \
3738
&& apt-get purge -y --auto-remove \
38-
python3-pip \
39-
python3-dev \
40-
git \
41-
build-essential \
42-
libhdf5-dev \
43-
libhdf5-serial-dev \
44-
pkg-config \
45-
gfortran \
39+
gcc \
40+
build-essential \
41+
libhdf5-dev \
42+
libhdf5-serial-dev \
43+
pkg-config \
44+
gfortran \
4645
&& rm -rf /var/lib/apt/lists/*
4746

48-
WORKDIR /
4947

50-
# copy contents
51-
COPY config_emhass.yaml /usr/src
52-
COPY rootfs /
48+
#copy config file (on all builds)
49+
COPY config_emhass.yaml /app/
50+
51+
# Make sure data directory exists
52+
RUN mkdir -p /app/data/
53+
54+
#-------------------------
55+
#EMHASS-ADDON Default
56+
FROM base as addon
5357

54-
# Labels
5558
LABEL \
56-
io.hass.name="emhass" \
57-
io.hass.description="EMHASS: Energy Management for Home Assistant" \
58-
io.hass.version=${BUILD_VERSION} \
59-
io.hass.type="addon" \
60-
io.hass.arch="aarch64|amd64|armhf|armv7"
59+
io.hass.name="emhass" \
60+
io.hass.description="EMHASS: Energy Management for Home Assistant" \
61+
io.hass.version=${BUILD_VERSION} \
62+
io.hass.type="addon" \
63+
io.hass.arch="aarch64|amd64|armhf|armv7"
64+
65+
ENTRYPOINT python3 -m emhass.web_server --addon True --url http://supervisor/core/api --key $SUPERVISOR_TOKEN
66+
67+
#-----------
68+
#EMHASS-ADD-ON testing with git
69+
FROM addon as addon-git
70+
ARG build_repo=https://github.com/davidusb-geek/emhass.git
71+
ARG build_branch=master
72+
WORKDIR /tmp/
73+
#Repo
74+
RUN git clone $build_repo
75+
WORKDIR /tmp/emhass
76+
#Branch
77+
RUN git checkout $build_branch
78+
RUN mkdir -p /app/src/emhass/
79+
RUN mkdir -p /app/data/
80+
RUN cp -r /tmp/emhass/src/emhass/. /app/src/emhass/
81+
RUN cp /tmp/emhass/data/opt_res_latest.csv /app/data/
82+
RUN cp /tmp/emhass/setup.py /app/
83+
RUN cp /tmp/emhass/README.md /app/
84+
WORKDIR /app
85+
RUN python3 -m pip install --no-cache-dir --break-system-packages -U .
86+
ENTRYPOINT python3 -m emhass.web_server --addon True --url http://supervisor/core/api --key $SUPERVISOR_TOKEN
6187

88+
#check build arguments and build
89+
FROM ${build_version} AS final

emhass/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ Be patient, the installation may take some time depending on your hardware.
1818

1919
When the installation has finished go to the `Configuration` tab to set the add-on parameters.
2020

21+
## Developing EMHASS/EMHASS-Add-on
22+
23+
- EMHASS
24+
- For those who want to develop the EMHASS package itself. Have a look at the [Develop page](https://emhass.readthedocs.io/en/latest/develop.html). _(EMHASS docs)_
25+
- EMHASS-Add-on
26+
- For those who want to test the Home Assistant integration of EMHASS. Have a look at [Test.md](https://github.com/davidusb-geek/emhass-add-on/blob/main/Test.md).
27+
2128
## License
2229

2330
MIT License

emhass/build.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
build_from:
2-
amd64: ghcr.io/home-assistant/amd64-base-debian:bookworm
3-
armhf: ghcr.io/home-assistant/armhf-base-debian:bookworm
4-
armv7: ghcr.io/home-assistant/armv7-base-debian:bookworm
5-
aarch64: ghcr.io/home-assistant/aarch64-base-debian:bookworm
1+
args:
2+
build_version: addon
3+
build_repo: https://github.com/davidusb-geek/emhass.git #addon-git mode
4+
build_branch: master #addon-git mode
65
codenotary:
76
signer: notary@home-assistant.io
87
base_image: notary@home-assistant.io

emhass/config.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ arch:
88
- amd64
99
- armhf
1010
- armv7
11-
image: "davidusb/image-{arch}-emhass"
11+
image: "davidusb/image-{arch}-emhass" #set if use image from DockerHub
1212
ports:
1313
5000/tcp: 5000
1414
ports_description:
@@ -25,6 +25,10 @@ ingress_port: 5000
2525
ingress_stream: true
2626
panel_icon: mdi:home-lightning-bolt-outline
2727
panel_title: EMHASS
28+
environment:
29+
OPTIONS_PATH: '/data/options.json'
30+
CONFIG_PATH: '/app/config_emhass.yaml'
31+
DATA_PATH: '/app/data/'
2832
options:
2933
logging_level: INFO
3034
costfun: profit

emhass/rootfs/etc/services.d/emhass/finish

Lines changed: 0 additions & 12 deletions
This file was deleted.

emhass/rootfs/etc/services.d/emhass/run

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)