mirror of
https://github.com/OMGeeky/homecontrol.esp-sensors.git
synced 2025-12-26 17:02:29 +01:00
3.9 KiB
3.9 KiB
ESP Sensors Project Guidelines
This document provides guidelines and instructions for developing and maintaining the ESP Sensors project.
Build and Configuration Instructions
Environment Setup
-
Python Version: This project uses Python 3.12. Ensure you have this version installed.
-
Virtual Environment: Always use a virtual environment for development:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate -
Dependencies: Install the required dependencies:
pip install -r requirements.txt
Project Structure
The project follows this structure:
esp-sensors/
├── src/
│ └── esp_sensors/ # Main package
│ ├── __init__.py
│ ├── sensor.py # Base sensor class
│ └── temperature.py # Temperature sensor implementation
├── tests/ # Test directory
├── .junie/ # Project documentation
├── pyproject.toml # Project configuration
└── requirements.txt # Dependencies
Testing Information
Running Tests
-
Basic Test Run:
python -m pytest -
Verbose Output:
python -m pytest -v -
With Coverage:
python -m pytest --cov=src.esp_sensors -
Generate Coverage Report:
python -m pytest --cov=src.esp_sensors --cov-report=htmlThis will create a
htmlcovdirectory with an HTML coverage report.
Adding New Tests
- Create test files in the
testsdirectory with the naming patterntest_*.py. - Test functions should be named with the prefix
test_. - Use pytest fixtures for common setup and teardown operations.
Example Test
Here's a simple example of a test for a temperature sensor:
import pytest
from src.esp_sensors.temperature import TemperatureSensor
def test_temperature_sensor_initialization():
"""Test that a temperature sensor can be initialized with valid parameters."""
sensor = TemperatureSensor("test_sensor", 5, 30, "C")
assert sensor.name == "test_sensor"
assert sensor.pin == 5
assert sensor.interval == 30
assert sensor.unit == "C"
Code Style and Development Guidelines
Code Formatting
This project uses Black for code formatting:
# Check if files need formatting
black --check .
# Format files
black .
Type Hints
Always use type hints in function signatures and variable declarations:
from typing import Dict, Any, Optional
def process_reading(value: float, metadata: Dict[str, Any]) -> Optional[float]:
# Function implementation
pass
Documentation
- Use docstrings for all modules, classes, and functions.
- Follow the Google docstring style.
- Include examples in docstrings where appropriate.
Example:
def read(self) -> float:
"""
Read the current sensor value.
Returns:
The sensor reading as a float
"""
# Implementation
Error Handling
- Use specific exception types rather than generic exceptions.
- Handle exceptions at the appropriate level.
- Log exceptions with context information.
Development Workflow
- Create a new branch for each feature or bug fix.
- Write tests before implementing features (Test-Driven Development).
- Ensure all tests pass before submitting changes.
- Format code with Black before committing.
- Update documentation as needed.
ESP-Specific Development Notes
When developing for actual ESP hardware:
- This project is designed to work with MicroPython on ESP32/ESP8266 devices.
- For hardware testing, you'll need to flash MicroPython to your device.
- Use tools like
ampyorrshellto upload code to the device. - Consider memory constraints when developing for ESP devices.
- For production, optimize code to reduce memory usage and power consumption.