Skip to content

Commit

Permalink
feat: add init script to initialize a working devenv with a specifi…
Browse files Browse the repository at this point in the history
…c Home Assistant version (#125)
  • Loading branch information
palazzem authored Feb 1, 2024
1 parent c48287d commit 7df2fda
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 3,686 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
pip install --upgrade pip
pip install tox
- name: Install Home Assistant testing platform
run: |
./scripts/download_fixtures.sh $(curl --silent "https://api.github.com/repos/home-assistant/core/releases/latest" | grep -Po "(?<=\"tag_name\": \").*(?=\")")
- name: Test with tox environments
run: tox

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Home Assistant configuration
# Home Assistant
config/
tests/hass/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,21 @@ To create a virtual environment and install the project and its dependencies, ex
terminal:
```bash
# Create and activate a new virtual environment
python3 -m venv venv
# Initialize the environment with the latest version of Home Assistant
E_HASS_VERSION=$(curl --silent "https://api.github.com/repos/home-assistant/core/releases/latest" | grep -Po "(?<=\"tag_name\": \").*(?=\")")
./scripts/init $E_HASS_VERSION
source venv/bin/activate
# Upgrade pip and install all projects and their dependencies
pip install --upgrade pip
pip install -e '.[all]'
# Install pre-commit hooks
pre-commit install
```

Instead, if you want to develop and test this integration with a different Home Assistant version, just pass the
version to the init script:
```bash
# Initialize the environment Home Assistant 2024.1.1
./scripts/init 2024.1.1
source venv/bin/activate

# Install pre-commit hooks
pre-commit install
Expand Down
45 changes: 45 additions & 0 deletions scripts/download_fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
#
# This script downloads a specified version of Home Assistant,
# extracts its test suite, processes the files, and moves them into a
# local 'tests/hass' directory for further use.
#
# Usage: ./scripts/download_fixtures.sh <Home Assistant Version>
# Example: ./scripts/download_fixtures.sh 2021.3.4
set -e

# Parameters
VERSION=$1

# Abort if no version is specified
if [ -z "$VERSION" ]; then
echo "Usage: ./scripts/download_fixtures.sh <Home Assistant Version>"
exit 1
fi

# Variables
DOWNLOAD_FOLDER=$(mktemp -d)
HASS_TESTS_FOLDER=$DOWNLOAD_FOLDER/core-$VERSION/tests/

# Remove previous folder if exists
if [ -d "tests/hass" ]; then
echo "Removing previous tests/hass/ folder"
rm -rf tests/hass
fi

# Download HASS version
echo "Downloading Home Assistant $VERSION in $DOWNLOAD_FOLDER"
curl -L https://github.com/home-assistant/core/archive/refs/tags/$VERSION.tar.gz -o $DOWNLOAD_FOLDER/$VERSION.tar.gz

# Extract HASS fixtures and tests helpers, excluding all components and actual tests
echo "Extracting tests/ folder from $VERSION.tar.gz"
tar -C $DOWNLOAD_FOLDER --exclude='*/components/*' --exclude='*/pylint/*' -xf $DOWNLOAD_FOLDER/$VERSION.tar.gz core-$VERSION/tests
find $HASS_TESTS_FOLDER -type f -name "test_*.py" -delete

# Recursively find and update imports
find $HASS_TESTS_FOLDER -type f -exec sed -i 's/from tests\./from tests.hass./g' {} +
mv $HASS_TESTS_FOLDER/conftest.py $HASS_TESTS_FOLDER/fixtures.py

# Copy Home Assistant fixtures
mv $HASS_TESTS_FOLDER ./tests/hass
echo "Home Assistant $VERSION tests are now in tests/hass/"
29 changes: 29 additions & 0 deletions scripts/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
set -e

# Parameters
VERSION=$1

# Abort if no version is specified
if [ -z "$VERSION" ]; then
echo "Usage: ./scripts/init.sh <Home Assistant Version>"
exit 1
fi

# Abort if `venv` folder already exists
if [ -d "venv" ]; then
echo "venv/ folder already exists. Deactivate your venv and remove venv/ folder."
exit 1
fi

# Create and activate a new virtual environment
python3 -m venv venv
source venv/bin/activate

# Upgrade pip and install all projects and their dependencies
pip install --upgrade pip
pip install -e '.[all]'

# Override Home Assistant version
pip install homeassistant==$VERSION
./scripts/download_fixtures.sh $VERSION
1 change: 0 additions & 1 deletion tests/hass/__init__.py

This file was deleted.

Loading

0 comments on commit 7df2fda

Please sign in to comment.