Skip to content

Commit

Permalink
Merge pull request #6 from guimatheus92/users/guimatheus92/onboard-do…
Browse files Browse the repository at this point in the history
…cker

Onboard docker
  • Loading branch information
guimatheus92 authored Jul 26, 2024
2 parents 38f4b25 + e272339 commit cdd75b2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
*.pyc
.env
.git
48 changes: 48 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Contributing Guidelines

All types of contributions are encouraged and valued. Please make sure to read
relevant sections in this file and in README before making your contribution. It
will make it a lot easier for us maintainers and smooth out the experience for
all involved. This project looks forward to your contributions :tada:

## How to contribute

#### Development


1. **Clone the Repository**:

```bash
git clone https://github.com/guimatheus92/node-red-homeassistant-three-way.git
cd node-red-homeassistant-three-way
```

2. Run the command below to install dependecies libraries:

```python
pip install -r requirements.txt
```

3. Update the `config.yaml` file with your Home Assistant URL and access token:

```yaml
home_assistant:
home_assistant_url: 'http://your-home-assistant-url:8123'
access_token: 'your-access-token'
```

4. Run app using the command below:

```sh
flask run
```

## Submitting Pull Request

* Ensure any install or build dependencies are working and the code is
generating the right output before submitting a PR
* Update the README.md with details of changes to the interface, this includes
new fields, features, etc.
* Promptly address any CI failures. If your pull request fails to build or pass
tests, please push another commit to fix it
* Resolve any merge conflicts that occur
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use the official Python image from the Docker Hub
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV FLASK_ENV=production

# Run run.py when the container launches
CMD ["python", "run.py"]
84 changes: 46 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,62 @@ This project is a web application designed to help users manage their Home Assis
- Node-RED installed in Home Assistant
- (optional) Node-RED node: [@bartbutenaers/node-red-autolayout-sidebar](https://flows.nodered.org/node/@bartbutenaers/node-red-autolayout-sidebar "@bartbutenaers/node-red-autolayout-sidebar")

`pip install -r requirements.txt` to install the libraries.
### Directory Structure

### Configuration
```plaintext
node-red-homeassistant-three-way/
├── .dockerignore
├── .gitignore
├── config.py
├── config.yaml
├── CONTRIBUTING.md
├── devices.csv
├── Dockerfile
├── mappings.csv
├── README.md
├── requirements.txt
├── run.py
├── tests
│ └── unit_tests
│ └── test_functions.py
├── scripts
│ ├── check_mapping.py
│ ├── get_ha_devices_entities.py
│ └── main.py
└── app
├── routes.py
├── __init__.py
├── templates
│ ├── devices.html
│ ├── index.html
│ ├── mappings.html
│ └── settings.html
└── static
├── favicon.ico
├── scripts.js
└── styles.css
```

#### Development
## How to run

1. Update the `config.yaml` file with your Home Assistant URL and access token:
1. **Clone the Repository**:

```yaml
home_assistant:
home_assistant_url: 'http://your-home-assistant-url:8123'
access_token: 'your-access-token'
```bash
git clone https://github.com/guimatheus92/node-red-homeassistant-three-way.git
cd node-red-homeassistant-three-way
```

2. **Build the Docker Image**:

2. Run app using the command below:
Build the Docker image using the following command:

```sh
flask run
```
```bash
docker-compose up --build
```

### Directory Structure
3. **Access the Flask Application**:

```plaintext
node-red-homeassistant-three-way/
├── app/
│ ├── __init__.py
│ ├── routes.py
│ ├── templates/
│ │ ├── index.html
│ │ ├── devices.html
│ │ ├── mappings.html
│ │ └── settings.html
│ ├── static/
│ │ ├── styles.css
│ │ └── scripts.js
├── scripts/
│ ├── get_ha_devices_entities.py
│ ├── check_mapping.py
│ ├── main.py
├── config.py
├── run.py
├── requirements.txt
├── config.yaml
├── mappings.csv
└── devices.csv
```
Open your web browser and navigate to `http://localhost:5000` to access the application.

## How to use

Expand Down
7 changes: 4 additions & 3 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ def mappings():

@app.route('/settings', methods=['GET', 'POST'])
def settings():
config_file_path = os.path.join(os.path.dirname(__file__), '..', 'config.yaml')
config_file_path = '/app/config.yaml'

if request.method == 'POST':
with open(config_file_path, 'r', encoding='utf8') as file:
config = yaml.safe_load(file)

# Update only the specific keys in the configuration
config['home_assistant']['home_assistant_url'] = request.form['home_assistant_url']
config['home_assistant']['access_token'] = request.form['access_token']

with open(config_file_path, 'w', encoding='utf8') as file:
yaml.dump(config, file)
yaml.safe_dump(config, file)

flash('Settings updated successfully', 'success')
return redirect(url_for('settings'))
Expand Down Expand Up @@ -219,4 +220,4 @@ def clear_devices():
except Exception as e:
app.logger.error(f"Error clearing devices: {e}")
app.logger.error(traceback.format_exc())
return jsonify({"success": False, "message": "An error occurred while clearing devices"}), 500
return jsonify({"success": False, "message": "An error occurred while clearing devices"}), 500
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
web:
container_name: node-red-homeassistant-three-way
image: guimatheus92/node-red-homeassistant-three-way
build:
context: .
volumes:
- .:/app
ports:
- "5000:5000"
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
app = create_app('config.ProductionConfig')

if __name__ == "__main__":
app.run()
app.run(host='0.0.0.0', port=5000)

0 comments on commit cdd75b2

Please sign in to comment.