diff --git a/.github/workflows/publish_docker.yaml b/.github/workflows/publish_docker.yaml index 4a58a5ac..dbdab77d 100644 --- a/.github/workflows/publish_docker.yaml +++ b/.github/workflows/publish_docker.yaml @@ -38,5 +38,6 @@ jobs: context: . platforms: linux/amd64,linux/arm64 push: true + build-args: build_version=standalone tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/Add-onEmulateDocker b/Add-onEmulateDocker deleted file mode 100644 index 44f794de..00000000 --- a/Add-onEmulateDocker +++ /dev/null @@ -1,70 +0,0 @@ -#Test Add-on via Docker Emulation -## Docker run example: -## docker build -t emhass/testing -f Add-onEmulateDocker . -## docker run -it -p 5000:5000 --name emhass-test -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" docker.io/emhass/testing --url YOURHAURLHERE --key YOURHAKEYHERE -FROM ghcr.io/home-assistant/$TARGETARCH-base-debian:bookworm - -WORKDIR /usr/src - -# copy the requirements file into the image -COPY ./requirements_addon.txt /usr/src/requirements.txt - -# Setup -RUN apt-get update \ - && apt-get install -y --no-install-recommends \ - libffi-dev \ - python3 \ - python3-pip \ - python3-dev \ - git \ - build-essential \ - gcc \ - coinor-cbc \ - coinor-libcbc-dev \ - libglpk-dev \ - glpk-utils \ - libhdf5-dev \ - libhdf5-serial-dev \ - netcdf-bin \ - libnetcdf-dev \ - pkg-config \ - gfortran \ - libatlas-base-dev \ - && ln -s /usr/include/hdf5/serial /usr/include/hdf5/include \ - && export HDF5_DIR=/usr/include/hdf5 \ - && pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U setuptools wheel \ - && pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -r requirements.txt \ - && apt-get purge -y --auto-remove \ - git \ - build-essential \ - libhdf5-dev \ - libhdf5-serial-dev \ - pkg-config \ - gfortran \ - && rm -rf /var/lib/apt/lists/* - -#install local emhass -#RUN git clone https://github.com/davidusb-geek/emhass.git -#pip install emhass (original) -#COPY ./. /app/ -#WORKDIR /app/ -#RUN python3 setup.py install -#create share dir -#RUN mkdir /share/ - - -WORKDIR / - -# copy contents -COPY config_emhass.yaml /usr/src -COPY options.json /data/ - -# Labels -LABEL \ - io.hass.name="emhass" \ - io.hass.description="EMHASS: Energy Management for Home Assistant" \ - io.hass.version=${BUILD_VERSION} \ - io.hass.type="addon" \ - io.hass.arch="aarch64|amd64|armhf|armv7" - -ENTRYPOINT [ "python3", "-m", "emhass.web_server", "--addon", "True" , "--no_response", "true"] diff --git a/Dockerfile b/Dockerfile index 88e202e9..00b90e4d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,68 +1,149 @@ -FROM python:3.11-slim-buster -# FROM ghcr.io/home-assistant/amd64-base-debian:bookworm # Uncomment to test add-on -# FROM ghcr.io/home-assistant/armhf-base-debian:bookworm +## EMHASS Docker +## Docker run ADD-ON testing example: +## docker build -t emhass/docker --build-arg build_version=addon-local . +## docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE +## +## Docker run Standalone example: +## docker build -t emhass/docker --build-arg build_version=standalone . +## docker run -it -p 5000:5000 --name emhass-container -v $(pwd)/config_emhass.yaml:/app/config_emhass.yaml -v $(pwd)/secrets_emhass.yaml:/app/secrets_emhass.yaml emhass/docker -# switch working directory -WORKDIR /app +#build_version options are: addon, addon-pip, addon-git, addon-local, standalone (default) +ARG build_version=standalone + +FROM ghcr.io/home-assistant/$TARGETARCH-base-debian:bookworm AS base -# copy the requirements file into the image -COPY requirements.txt requirements.txt -COPY requirements_webserver.txt requirements_webserver.txt -COPY setup.py setup.py -COPY README.md README.md +WORKDIR /app +COPY requirements.txt /app/ # Setup RUN apt-get update \ && apt-get install -y --no-install-recommends \ - # libffi-dev \ - # python3 \ - # python3-pip \ - # python3-dev \ - # git \ - # build-essential \ - gcc \ - coinor-cbc \ - coinor-libcbc-dev \ - libhdf5-dev \ - libhdf5-serial-dev \ - netcdf-bin \ - libnetcdf-dev \ - # pkg-config \ - # gfortran \ - # libatlas-base-dev \ + libffi-dev \ + python3 \ + python3-pip \ + python3-dev \ + git \ + build-essential \ + gcc \ + coinor-cbc \ + coinor-libcbc-dev \ + libglpk-dev \ + glpk-utils \ + libhdf5-dev \ + libhdf5-serial-dev \ + netcdf-bin \ + libnetcdf-dev \ + pkg-config \ + gfortran \ + libatlas-base-dev \ && ln -s /usr/include/hdf5/serial /usr/include/hdf5/include \ && export HDF5_DIR=/usr/include/hdf5 \ - # && pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U setuptools wheel \ - && pip3 install --no-cache-dir --break-system-packages -r requirements_webserver.txt \ + && pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U setuptools wheel \ + && pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -r requirements.txt \ && apt-get purge -y --auto-remove \ - gcc \ - libhdf5-dev \ - libhdf5-serial-dev \ - netcdf-bin \ - libnetcdf-dev \ + gcc \ + build-essential \ + libhdf5-dev \ + libhdf5-serial-dev \ + pkg-config \ + gfortran \ && rm -rf /var/lib/apt/lists/* -# copy contents -COPY src/emhass/__init__.py /app/src/emhass/__init__.py -COPY src/emhass/command_line.py /app/src/emhass/command_line.py -COPY src/emhass/forecast.py /app/src/emhass/forecast.py -COPY src/emhass/machine_learning_forecaster.py /app/src/emhass/machine_learning_forecaster.py -COPY src/emhass/optimization.py /app/src/emhass/optimization.py -COPY src/emhass/retrieve_hass.py /app/src/emhass/retrieve_hass.py -COPY src/emhass/utils.py /app/src/emhass/utils.py -COPY src/emhass/web_server.py /app/src/emhass/web_server.py -COPY src/emhass/templates/index.html /app/src/emhass/templates/index.html -COPY src/emhass/templates/template.html /app/src/emhass/templates/template.html -COPY src/emhass/static/style.css /app/src/emhass/static/style.css -COPY src/emhass/static/img/emhass_logo_short.svg /app/src/emhass/static/img/emhass_logo_short.svg -COPY src/emhass/static/img/emhass_icon.png /app/src/emhass/static/img/emhass_icon.png -COPY data/opt_res_latest.csv /app/data/opt_res_latest.csv - -RUN python3 setup.py install - -# set env variables + +#copy config file (on all builds) +COPY config_emhass.yaml /app/ + +# Make sure data directory exists +RUN mkdir -p /app/data/ + +#------------------------- +#EMHASS-ADDON Default (this has no emhass packadge) +FROM base as addon + +LABEL \ + io.hass.name="emhass" \ + io.hass.description="EMHASS: Energy Management for Home Assistant" \ + io.hass.version=${BUILD_VERSION} \ + io.hass.type="addon" \ + io.hass.arch="aarch64|amd64|armhf|armv7" + +ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True", "--url", "http://supervisor/core/api"] + +#----------- +#EMHASS-ADD-ON Testing with pip emhass (closest testing reference) +FROM addon as addon-pip +#set build arg for pip version +ARG build_pip_version="" +RUN pip3 install --no-cache-dir --break-system-packages --upgrade --upgrade-strategy=only-if-needed -U emhass${build_pip_version} + +COPY options.json /app/ + +ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True", "--no_response", "True"] + +#----------- +#EMHASS-ADD-ON Testing from local files +FROM addon as addon-local +COPY src/emhass/ /app/src/emhass/ +COPY src/emhass/templates/ /app/src/emhass/templates/ +COPY src/emhass/static/ /app/src/emhass/static/ +COPY src/emhass/static/img/ /app/src/emhass/static/img/ +COPY data/opt_res_latest.csv /app/data/ +#add options.json, this otherwise would be generated via HA +COPY options.json /app/ +COPY README.md /app/ +COPY setup.py /app/ +#compile EMHASS locally +RUN python3 -m pip install --no-cache-dir --break-system-packages -U . +ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True" , "--no_response", "True"] + + +#----------- +#EMHASS-ADD-ON testing with git +FROM addon as addon-git +ARG build_repo=https://github.com/davidusb-geek/emhass.git +ARG build_branch=master +WORKDIR /tmp/ +#Repo +RUN git clone $build_repo +WORKDIR /tmp/emhass +#Branch +RUN git checkout $build_branch +RUN mkdir -p /app/src/emhass/ +RUN cp -r /tmp/emhass/src/emhass/. /app/src/emhass/ +RUN cp /tmp/emhass/data/opt_res_latest.csv /app/data/ +RUN cp /tmp/emhass/setup.py /app/ +RUN cp /tmp/emhass/README.md /app/ +#add options.json, this otherwise would be generated via HA +RUN cp /tmp/emhass/options.json /app/ +WORKDIR /app +RUN python3 -m pip install --no-cache-dir --break-system-packages -U . +ENTRYPOINT [ "python3", "-m", "emhass.web_server","--addon", "True" , "--no_response", "True"] + +#------------------------- +#EMHASS STANDALONE +FROM base as standalone + +RUN pip3 install --extra-index-url=https://www.piwheels.org/simple --no-cache-dir --break-system-packages -U flask waitress plotly + +COPY src/emhass/ /app/src/emhass/ +COPY src/emhass/templates/ /app/src/emhass/templates/ +COPY src/emhass/static/ /app/src/emhass/static/ +COPY src/emhass/static/img/ /app/src/emhass/static/img/ +COPY data/opt_res_latest.csv /app/data/ +COPY README.md /app/ +COPY setup.py /app/ +#secrets file will need to be copied manually at docker run + +# # set env variables ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 -# configure the container to run in an executed manner -CMD [ "python3", "src/emhass/web_server.py" ] +#build EMHASS +# RUN python3 setup.py install +RUN python3 -m pip install --no-cache-dir --break-system-packages -U . +ENTRYPOINT [ "python3", "-m", "emhass.web_server"] +#------------------------- + + +#check build arguments and build +FROM ${build_version} AS final \ No newline at end of file diff --git a/README.md b/README.md index 834bb4e2..ce2cd3dd 100644 --- a/README.md +++ b/README.md @@ -425,7 +425,7 @@ Check the dedicated section in the documentation here: [https://emhass.readthedo ## Development -Pull request are very much accepted on this project. For development you can find some instructions here [Development](./docs/develop.md) +Pull request are very much accepted on this project. For development you can find some instructions here [Development](https://emhass.readthedocs.io/en/latest/develop.html) ## Troubleshooting diff --git a/deploy_docker.mk b/deploy_docker.mk index 58b4923c..fbc76086 100644 --- a/deploy_docker.mk +++ b/deploy_docker.mk @@ -9,4 +9,4 @@ clean_deploy: deploy docker save -o emhass_${image_ref}.tar ${project}/${repo}:${image_ref} deploy: - docker build -t ${project}/${repo}:${image_ref} -f Dockerfile . + docker build -t ${project}/${repo}:${image_ref} --build-arg build_version=standalone -f Dockerfile . diff --git a/docs/develop.md b/docs/develop.md index 53b3d749..027dbe7a 100644 --- a/docs/develop.md +++ b/docs/develop.md @@ -1,46 +1,267 @@ -Create a developer environment: +# EMHASS Development + +There are multiple different approaches to developing EMHASS. +The choice depends on EMHASS mode (standalone/add-on) and preference (Python venv/DevContainer/Docker). +Below are some development workflow examples: +_Note: It is preferred to run both addon mode, standalone mode and unitest once before submitting and pull request._ + +## Step 1 - Fork + +_With your preferred Git tool of choice:_ +Fork the EMHASS github repository into your own account, then clone the forked repository into your local development platform. (ie. PC or Codespace) + +## Step 2 - Develop + +To develop and test code choose one of the following methods: + +### Method 1 - Python Virtual Environment + +We can use python virtual environments to build, develop and test/unitest the code. +This method works well with standalone mode. + +_confirm terminal is in the root `emhass` directory before starting_ + +**Create a developer environment:** + +```bash +python -m venv .venv ``` -virtualenv -p /usr/bin/python3 emhass-dev + +**Activate the environment:** + +- linux: + + ```bash + source .venv/bin/activate + ``` + +- windows: + + ```cmd + .venv\Scripts\activate.bat + ``` + +An IDE like VSCode should automatically catch that a new virtual env was created. + +**Install the _emhass_ package in editable mode:** + +```bash +python -m pip install -e . ``` -To develop using Anaconda use (pick the correct Python and Pip versions): + +**Set paths with environment variables:** + +- Linux + ```bash + export OPTIONS_PATH="${PWD}/options.json" && export USE_OPTIONS="True" ##optional to test options.json + export CONFIG_PATH="${PWD}/config_emhass.yaml" + export SECRETS_PATH="${PWD}/secrets_emhass.yaml" + export DATA_PATH="${PWD}/data/" + ``` +- windows + ```cmd + set "OPTIONS_PATH=%cd%/options.json" & :: optional to test options.json + set "USE_OPTIONS=True" & :: optional to test options.json + set "CONFIG_PATH=%cd%/config_emhass.yaml" + set "SECRETS_PATH=%cd%/secrets_emhass.yaml" + set "DATA_PATH=%cd%/data/" + ``` + +_Make sure `secrets_emhass.yaml` has been made and set. Copy `secrets_emhass(example).yaml` for an example._ + +**Run EMHASS** + ``` -conda create --name emhass-dev python=3.8 pip=21.0.1 +python3 src/emhass/web_server.py ``` -Then activate environment and install the required packages using: + +**Run unitests** + ``` -pip install -r requirements.txt +python3 -m unitest -v -RP -s ./tests -p 'test_*.py' ``` -Add `emhass` to the Python path using the path to `src`, for example: + +_unitest will need to be installed prior_ + +### Method 2: VS-Code Debug and Run via DevContainer + +In VS-Code, you can run a Docker DevContainer to set up a virtual environment. There you can edit and test EMHASS. + +The recommended steps to run are: + +- Open forked root (`emhass`) folder inside of VS-Code +- VS-Code will ask if you want to run in a dev-container, say yes _([Dev Container](https://code.visualstudio.com/docs/devcontainers/containers) must be set up first)_. *(Shortcut: `F1` > `Dev Containers: Rebuild and Reopen in Container`)* +- Edit some code... +- Compile emhass by pressing `control+shift+p` > `Tasks: Run Task` > `EMHASS Install`. + This has been set up in the [tasks.json](https://github.com/davidusb-geek/emhass/blob/master/.vscode/tasks.json) file. - Before _run & debug_, re-run `EMHASS Install` task every time a change has been made to emhass. +- Launch and debug the application via selecting the [`Run and Debug`](https://code.visualstudio.com/docs/editor/debugging) tab /`Ctrl+Shift+D` > `EMHASS run Addon`. This has been set up in the [Launch.json](https://github.com/davidusb-geek/emhass/blob/master/.vscode/launch.json) . + + - You will need to modify the `EMHASS_URL` _(http://HAIPHERE:8123/)_ and `EMHASS_KEY` _(PLACEKEYHERE)_ inside of Launch.json that matches your HA environment before running. + - If you want to change your parameters, you can edit options.json file before launch. + - you can also choose to run `EMHASS run` instead of `EMHASS run Addon`. This acts more like standalone mode and uses the secrets_emhass.yaml files and removes the use of options.json. _(user sets parameters in config_emhass.yaml instead)_ + +- You can run all the unitests by heading to the [`Testing`](https://code.visualstudio.com/docs/python/testing) tab on the left hand side. + This is recommended before creating a pull request. + +### Method 3 - Docker Virtual Environment + +With Docker, you can test EMHASS in both standalone and add-on mode via modifying the build argument: `build_version` with values: `standalone`, `addon-pip`, `addon-git`, `addon-local`. +Since emhass-add-on is using the same docker base, this method is good to test the add-on functionality of your code. _(addon-local)_ + +Depending on your choice of running standalone or addon, `docker run` will require different passed variables/arguments to function. See following examples: + +_Note: Make sure your terminal is in the root `emhass` directory before running the docker build._ + +#### Docker run add-on via with local files: + +**addon-local** copies the local emhass files (from your device) to compile and run in addon mode. + +```bash +docker build -t emhass/docker --build-arg build_version=addon-local . + +docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE ``` -/home/user/emhass/src + +**Note:** + +- `addon` mode can have secret parameters passed in at run via variables `-e`, arguments (`--key`,`--url`) or via `secrets_emhass.yaml` with a volume mount +- on file change, you will need to re-build and re-run the Docker image/container in order for the change to take effect. (excluding volume mounted configs) +- if you are planning to modify the configs: options.json, secrets_emhass.yaml or config_emhass.yaml, you can volume mount them with `-v`: + + ```bash + docker build -t emhass/docker --build-arg build_version=addon-local . + + docker run -it -p 5000:5000 --name emhass-container -v $(pwd)/options.json:/app/options.json -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE + ``` + + This allows the editing of config files without re-building the Docker Image. On config change, restart the container to take effect: + + ```bash + docker stop emhass-container + + docker start emhass-container + ``` + +#### Docker run Standalone with local files: + +**standalone** copies the local emhass files (from your device) to compile and run in standalone mode. + +```bash +docker build -t emhass/docker --build-arg build_version=standalone . + +docker run -it -p 5000:5000 --name emhass-container -v $(pwd)/config_emhass.yaml:/app/config_emhass.yaml -v $(pwd)/secrets_emhass.yaml:/app/secrets_emhass.yaml emhass/docker ``` -If working on linux we can add these lines to the `~/.bashrc` file: + +_Standalone mode requires `secrets_emhass.yaml` to be set and passed in on operate. Copy `secrets_emhass(example).yaml` for an example._ + +#### Docker run add-on with Git or pip: + +If you would like to test with the current production/master versions of emhass, you can do so via pip or Git. With Git, you can also specify other repos/branches outside of `davidusb-geek/emhass:master`. + +**addon-pip** will be the closest environment to the production emhass-add-on. +However, both come with the disadvantage of not easily being able to edit the emhass package itself. + +**Docker run add-on git** + +```bash +docker build -t emhass/docker --build-arg build_version=addon-git . + +docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" -v $(pwd)/options.json:/app/options.json emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE ``` -# Python modules -export PYTHONPATH="${PYTHONPATH}:/home/user/emhass/src" + +To test a repo and branch outside of `davidusb-geek/emhass:master`: +_(Utilizing build args `build_repo` and `build_branch`)_ +_Linux:_ +```bash +repo=https://github.com/davidusb-geek/emhass.git +branch=master + +docker build -t emhass/docker --build-arg build_version=addon-git --build-arg build_repo=$repo --build-arg build_branch=$branch . + +docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" -v $(pwd)/options.json:/app/options.json emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE ``` -Don't foget to source the `~/.bashrc` file: + +**Docker run add-on pip:** + +```bash +docker build -t emhass/docker --build-arg build_version=addon-pip . + +docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" -v $(pwd)/options.json:/app/options.json emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE ``` -source ~/.bashrc +To build with specific pip version, set with build arg: `build_pip_version`: +```bash +docker build -t emhass/docker --build-arg build_version=addon-pip --build-arg build_pip_version='==0.7.7' . + +docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" -v $(pwd)/options.json:/app/options.json emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE ``` -Update the build package: +
+ +_You can add or remove file volume mounts with the `-v` tag, this should override the file in the container (ex. options.json)_ + +#### EMHASS older then **0.7.9** +For older versions of EMHASS, you may wish to specify the _config_, _data_ and _options_ paths to avoid errors: +```bash +docker run ... -e OPTIONS_PATH='/app/options.json' -e CONFIG_PATH='/app/config_emhass.yaml' -e DATA_PATH='/app/data/' ... ``` -python3 -m pip install --upgrade build +For example pip: +```bash +docker build -t emhass/docker --build-arg build_version=addon-pip . + +docker run -it -p 5000:5000 --name emhass-container -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" -e CONFIG_PATH='/app/config_emhass.yaml' -e DATA_PATH='/app/data/' -e OPTIONS_PATH='/app/options.json' -v $(pwd)/options.json:/app/options.json emhass/docker --url YOURHAURLHERE --key YOURHAKEYHERE ``` -And generate distribution archives with: + + +#### Delete built Docker image + +We can delete the Docker image and container via: + +```bash +docker rm -f emhass-container #force delete Docker container + +docker rmi emhass/docker #delete Docker image ``` -python3 -m build + +#### Other Docker Options + +**Rapid Testing** +As editing and testing EMHASS via docker may be repetitive (rebuilding image and deleting containers), you may want to simplify the removal, build and run process. + +**For rapid Docker testing, try a command chain:** +_Linux:_ +```bash +docker build -t emhass/docker --build-arg build_version=addon-local . && docker run --rm -it -p 5000:5000 -v $(pwd)/secrets_emhass.yaml:/app/secrets_emhass.yaml --name emhass-container emhass/docker ``` -Or with: + +_The example command chain rebuilds Docker image, and runs new container with newly built image. `--rm` has been added to the `docker run` to delete the container once ended to avoid manual deletion every time._ +_This use case may not require any volume mounts (unless you use secrets_emhass.yaml) as the Docker build process will pull the latest versions of the configs as it builds._ + + +**Environment Variables** +Running addon mode, you can also pass location, key and url secret parameters via environment variables. + +```bash +docker build -t emhass/docker --build-arg build_version=addon-local . + +docker run -it -p 5000:5000 --name emhass-container -e URL="YOURHAURLHERE" -e KEY="YOURHAKEYHERE" -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" -e TIME_ZONE="Europe/Paris" emhass/docker ``` -python3 setup.py build bdist_wheel -``` -Create a new tag version: -``` -git tag vX.X.X -git push origin --tags -``` -Upload to pypi: -``` -twine upload dist/* + +This allows the user to set variables prior to build +Linux: + +```bash +export EMHASS_URL="YOURHAURLHERE" +export EMHASS_KEY="YOURHAKEYHERE" +export TIME_ZONE="Europe/Paris" +export LAT="45.83" +export LON="6.86" +export ALT="4807.8" + +docker build -t emhass/docker --build-arg build_version=addon-local . + +docker run -it -p 5000:5000 --name emhass-container -e EMHASS_KEY -e EMHASS_URL -e TIME_ZONE -e LAT -e LON -e ALT emhass/docker ``` + +## Step 3 - Pull request + +Once developed, commit your code, and push to your fork. +Then submit a pull request with your fork to the [davidusb-geek/emhass@master](https://github.com/davidusb-geek/emhass) repository. diff --git a/docs/index.md b/docs/index.md index 2d5913ff..12ec181a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -23,6 +23,7 @@ mlforecaster.md study_case.md config.md emhass.md +develop.md ``` # Indices and tables diff --git a/src/emhass/web_server.py b/src/emhass/web_server.py index 58e441f7..5db7ae50 100644 --- a/src/emhass/web_server.py +++ b/src/emhass/web_server.py @@ -205,12 +205,14 @@ def action_call(action_name): use_options = os.getenv('USE_OPTIONS', default=False) # Define the paths if args.addon==1: - OPTIONS_PATH = os.getenv('OPTIONS_PATH', default="/data/options.json") + OPTIONS_PATH = os.getenv('OPTIONS_PATH', default="/app/options.json") options_json = Path(OPTIONS_PATH) - CONFIG_PATH = os.getenv("CONFIG_PATH", default="/usr/src/config_emhass.yaml") + CONFIG_PATH = os.getenv("CONFIG_PATH", default="/app/config_emhass.yaml") #Obtain url and key from ENV or ARG hass_url = os.getenv("EMHASS_URL", default=args.url) - key = os.getenv("EMHASS_KEY", default=args.key) + key = os.getenv("SUPERVISOR_TOKEN", default=args.key) + if hass_url != "http://supervisor/core/api": + key = os.getenv("EMHASS_KEY", key) #If url or key is None, Set as empty string to reduce NoneType errors bellow if key is None: key = "" if hass_url is None: hass_url = "" @@ -220,7 +222,7 @@ def action_call(action_name): options = json.load(data) else: app.logger.error("options.json does not exists") - DATA_PATH = os.getenv("DATA_PATH", default="/share/") + DATA_PATH = os.getenv("DATA_PATH", default="/app/data/") else: if use_options: OPTIONS_PATH = os.getenv('OPTIONS_PATH', default="/app/options.json")