Skip to content

Commit

Permalink
Add development container and configs for test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus committed Feb 4, 2024
1 parent 111feb0 commit 055fd89
Show file tree
Hide file tree
Showing 20 changed files with 356 additions and 8 deletions.
42 changes: 42 additions & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "alengwenus/ha-sma-ev-charger",
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
"postCreateCommand": "scripts/setup",
"forwardPorts": [
8123
],
"portsAttributes": {
"8123": {
"label": "Home Assistant",
"onAutoForward": "notify"
}
},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"github.vscode-pull-request-github",
"ryanluker.vscode-coverage-gutters",
"ms-python.vscode-pylance"
],
"settings": {
"files.eol": "\n",
"editor.tabSize": 4,
"python.pythonPath": "/usr/bin/python3",
"python.analysis.autoSearchPaths": false,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true
}
}
},
"remoteUser": "vscode",
"features": {
"ghcr.io/devcontainers/features/rust:1": {}
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
44 changes: 44 additions & 0 deletions .github/workflows/install_dependencies/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: 'Install Dependencies'
description: 'Install Home Assistant and test dependencies'
inputs:
python-version:
description: 'Python version'
required: true
default: '3.10'
core-version:
description: 'Home Assistant core version'
required: false
default: 'dev'

runs:
using: "composite"
steps:
- name: Check out code from GitHub
uses: actions/checkout@v3
with:
repository: ${{ github.repository }}
ref: ${{ github.ref }}
persist-credentials: false
fetch-depth: 0
- name: Check out code from GitHub
uses: actions/checkout@v3
with:
repository: home-assistant/core
path: core
ref: ${{ inputs.core-version }}
- name: Set up Python ${{ inputs.python-version }}
id: python
uses: actions/[email protected]
with:
python-version: ${{ inputs.python-version }}
- name: Install dependencies
shell: bash
run: |
echo "::warning::### WARNING! Deprecation warnings muted with option '--use-pep517' please address this at some point in pytest.yaml. ###"
pip install -r core/requirements.txt --use-pep517
# because they decided to pull codecov the package from PyPI...
sed -i '/codecov/d' core/requirements_test.txt
pip install -r core/requirements_test.txt --use-pep517
pip install -e core/ --use-pep517
pip install pysmaev # this is in smaev's manifest.json
pip install $(python test_dependencies.py) --use-pep517
70 changes: 70 additions & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: pytest

on:
push:
branches: [feat/tests]
pull_request:

jobs:
pytest:
name: Run pytest
runs-on: ubuntu-20.04
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
include:
- python-version: "3.10"
core-version: "2022.11.5"
- python-version: "3.10"
core-version: "2022.12.9"
- python-version: "3.10"
core-version: "2023.1.7"
- python-version: "3.10"
core-version: "2023.2.5"
- python-version: "3.10"
core-version: "2023.3.6"
- python-version: "3.10"
core-version: "2023.4.6"
- python-version: "3.10"
core-version: "2023.5.2"
- python-version: "3.11"
core-version: "2023.6.1"
- python-version: "3.11"
core-version: "dev"
steps:
- name: Check out code from GitHub
uses: actions/checkout@v3

- name: Install Home Assistant
uses: ./.github/workflows/install_dependencies
with:
python-version: ${{ matrix.python-version }}
core-version: ${{ matrix.core-version }}

- name: Link custom_components/smaev
run: |
cd core
# Link smaev tests
cd tests/components/
ln -fs ../../../tests smaev
cd -
- name: Run pytest
timeout-minutes: 60
run: |
export PYTHONPATH=${PYTHONPATH}:${PWD}
cd core
python3 -X dev -m pytest \
-vvv \
-qq \
--timeout=9 \
--durations=10 \
--cov="custom_components.smaev" \
--cov-report=xml \
-o console_output_style=count \
-p no:sugar \
tests/components/smaev
env:
HA_CLONE: true
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ lib64
.project
.pydevproject

# Visual Studio Code
.vscode

# Testing
.tox
.coverage
Expand All @@ -49,3 +46,7 @@ desktop.ini
# other
sandbox
example.py

# Home Assistant configuration
config/*
!config/configuration.yaml
48 changes: 48 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# The contents of this file is based on https://github.com/home-assistant/core/blob/dev/pyproject.toml

target-version = "py310"

select = [
"B007", # Loop control variable {name} not used within loop body
"B014", # Exception handler with duplicate exception
"C", # complexity
"D", # docstrings
"E", # pycodestyle
"F", # pyflakes/autoflake
"ICN001", # import concentions; {name} should be imported as {asname}
"PGH004", # Use specific rule codes when using noqa
"PLC0414", # Useless import alias. Import alias does not rename original package.
"SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass
"SIM117", # Merge with-statements that use the same scope
"SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys()
"SIM201", # Use {left} != {right} instead of not {left} == {right}
"SIM212", # Use {a} if {a} else {b} instead of {b} if not {a} else {a}
"SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'.
"SIM401", # Use get from dict with default instead of an if block
"T20", # flake8-print
"TRY004", # Prefer TypeError exception for invalid type
"RUF006", # Store a reference to the return value of asyncio.create_task
"UP", # pyupgrade
"W", # pycodestyle
]

ignore = [
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
"D404", # First word of the docstring should not be This
"D406", # Section name should end with a newline
"D407", # Section name underlining
"D411", # Missing blank line before section
"E501", # line too long
"E731", # do not assign a lambda expression, use a def
]

[flake8-pytest-style]
fixture-parentheses = false

[pyupgrade]
keep-runtime-typing = true

[mccabe]
max-complexity = 25
11 changes: 11 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Home Assistant on port 8123",
"type": "shell",
"command": "scripts/develop",
"problemMatcher": []
}
]
}
8 changes: 8 additions & 0 deletions config/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://www.home-assistant.io/integrations/default_config/
default_config:

# https://www.home-assistant.io/integrations/logger/
logger:
default: info
logs:
custom_components.smaev: debug
1 change: 1 addition & 0 deletions custom_components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Custom Components."""
1 change: 0 additions & 1 deletion custom_components/smaev/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"config_flow": true,
"dependencies": [],
"documentation": "https://github.com/alengwenus/ha-sma-ev-charger",
"homekit": {},
"iot_class": "local_polling",
"issue_tracker": "https://github.com/alengwenus/ha-sma-ev-charger/issues",
"requirements": ["pysmaev==0.1.6"],
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "SMA EV Charger",
"render_readme": true,
"homeassistant": "2023.9"
"homeassistant": "2023.9.0"
}
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pre-commit==3.5.0
black==23.3.0
ruff==0.1.6
homeassistant==2024.1.5
# Install HA and test dependencies (pytest, coverage)
# To pin the dev container to a specific HA version, set this dependency
# to the adequate version (add `==<version>`) and rebuild the dev container.
# See https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/releases for version mappings.
pytest-homeassistant-custom-component==0.13.91
3 changes: 0 additions & 3 deletions requirements_test.txt

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/develop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

# Create config dir if not present
if [[ ! -d "${PWD}/config" ]]; then
mkdir -p "${PWD}/config"
hass --config "${PWD}/config" --script ensure_config
fi

# Set the path to custom_components
## This let's us have the structure we want <root>/custom_components/integration_blueprint
## while at the same time have Home Assistant configuration inside <root>/config
## without resulting to symlinks.
export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components"

# Start Home Assistant
hass --config "${PWD}/config" --debug
7 changes: 7 additions & 0 deletions scripts/lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

pre-commit run --all-files
8 changes: 8 additions & 0 deletions scripts/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

python3 -m pip install --requirement requirements.txt
pre-commit install-hooks
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool:pytest]
testpaths = tests
asyncio_mode = auto
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for Home Assistant SMA EV Charger."""
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Fixtures for testing."""
import os
import sys

import pytest

# Tests in the dev enviromentment use the pytest_homeassistant_custom_component instead of
# a cloned HA core repo for a simple and clean structure. To still test against a HA core
# clone (e.g. the dev branch for which no pytest_homeassistant_custom_component exists
# because HA does not publish dev snapshot packages), set the HA_CLONE env variable.
if "HA_CLONE" in os.environ:
# Rewire the testing package to the cloned test modules. See the test `Dockerfile`
# for setup details.
sys.modules["pytest_homeassistant_custom_component"] = __import__("tests")


@pytest.fixture(name="device_info")
def device_info():
"""Return device info."""
return {
"name": "SMA EV Charger 22",
"serial": "1234567890",
"model": "EVC22-3AC-10",
"manufacturer": "SMA",
"sw_version": "1.2.23.R",
}


@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
"""Enable use of custom_components."""
yield
Loading

0 comments on commit 055fd89

Please sign in to comment.