davidusb-geek / emhass

emhass: Energy Management for Home Assistant, is a Python module designed to optimize your home energy interfacing with Home Assistant.
MIT License
260 stars 51 forks source link

Optional: two additional testing environment options #182

Closed GeoDerp closed 4 months ago

GeoDerp commented 5 months ago

This is an optional pull request uses VS-Code Dev container or Dockerfile (as an sandboxed environment) for debugging and testing the application. Both of which where designed with focus on running with Add-on mode. And both need an additional HA environment to operate (no supervisor support for the moment)

Method 1: VS-Code Debug and Run

Recommended steps to run are:

Method 2: Dockerfile run

Alternatively: This PR also adds a dockerfile that tries to emulate the Add-on environment as best as possible. This method is slower, however does not need VS-code to run (just Docker environment) Run it on the emhass root dir with: (It is recommended to run this on the host device, rather than an dev container)

docker build -t emhass/testing -f Add-onEmulateDocker .
docker run -it -p 5000:5000 --name emhass-test docker.io/emhass/testing --url YOURHAURLHERE --key YOURHAKEYHERE

Delete docker container and image with:

docker rm  emhass-test
docker rmi emhass/testing

For Rapid testing try a command chain like this: Bash Example

docker rm -f emhass-test ; docker build -t emhass/testing -f Add-onEmulateDocker . && docker run -it -p 5001:5000 --name emhass-test docker.io/emhass/testing --url YOURHAURLHERE --key YOURHAKEYHERE

With example above use secrets_emhass.yaml or options.yaml file to set time_zone,Lat,Lon & Alt

Docker Alternative

We can also pass location, key and url parameters via environment variables

docker build -t emhass/testing -f Add-onEmulateDocker .
docker run -it -p 5000:5000 --name emhass-test -e URL="YOURHAURLHERE" -e KEY="YOURHAKEYHERE" -e LAT="45.83" -e LON="6.86" -e ALT="4807.8" docker.io/emhass/testing

This allows the user to set variables prior to build Linux Example: (in you host terminal)

export EMHASS_URL="YOURHAURLHERE"
export EMHASS_KEY="YOURHAKEYHERE"
export TIME_ZONE="Europe/Paris"
export LAT="45.83"
export LON="45.83"
export ALT="4807.8"

docker build -t emhass/testing -f Add-onEmulateDocker .
docker run -it -p 5001:5000 --name emhass-test  -e EMHASS_KEY -e EMHASS_URL -e TIME_ZONE  -e LAT -e LON -e ALT  docker.io/emhass/testing

Note:

GeoDerp commented 5 months ago

Note: these methods are still developmental. Somethings act slightly differently then they should. However, the hope is that this will be an assistance for developers that choose to use it. And maybe someone can refine one of these (or both) processes from here.

davidusb-geek commented 5 months ago

This looks nice for add-on testing. To try to emulate even better the add-on environment we could use this image for docker: ghcr.io/home-assistant/amd64-base-debian:bookworm

GeoDerp commented 5 months ago

This looks nice for add-on testing. To try to emulate even better the add-on environment we could use this image for docker: ghcr.io/home-assistant/amd64-base-debian:bookworm

Good Idea 👍 7171b3564cda7f052097d7cb5c1da72b5a50ca2e

davidusb-geek commented 5 months ago

Hi. Could consider your changes to the web_server.py. Were they necessary to implement these new testing methods. I like these new methods but your modifications to the web_server.py invalidates the use of the use_options helper that I'm using for debug (look at the current open review)

GeoDerp commented 5 months ago

Yeah sorry about that. I Should be able to add it back in without a problem. Will check now however

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (195e774) 89.84% compared to head (9562dcb) 89.90%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #182 +/- ## ========================================== + Coverage 89.84% 89.90% +0.06% ========================================== Files 6 6 Lines 1645 1645 ========================================== + Hits 1478 1479 +1 + Misses 167 166 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

GeoDerp commented 5 months ago

@davidusb-geek Added support to use Environment Variables. I understand that this adds a bit of code. Let me know if you want me to revert any aspects (ex: no ENV support for url and key)

GeoDerp commented 5 months ago

I was also thinking about adding security with the secrets with the use of docker secrets. But since the docker container in question is for development only I thought it was outside of scope.

davidusb-geek commented 4 months ago

@davidusb-geek Added support to use Environment Variables. I understand that this adds a bit of code. Let me know if you want me to revert any aspects (ex: no ENV support for url and key)

From what I see in the code the default values are what it was before so it should be ok. I need to take some time to test this further. This week-end...

davidusb-geek commented 4 months ago

Merging... Added this page to the wiki as a help following this PR: https://github.com/davidusb-geek/emhass/wiki/Contributing