Code Quality Tools
Ruff
Ruff is a fast Python linter and formatter that enforces code style and catches common issues.Running Ruff
Configuration
Ruff is configured inpyproject.toml:
Pylint
Pylint provides deeper code analysis with custom Home Assistant plugins.Running Pylint
Custom Plugins
Home Assistant includes custom pylint plugins inpylint/plugins/:
- hass_async_load_fixtures - Validates fixture loading patterns
- hass_decorator - Checks decorator usage
- hass_enforce_class_module - Ensures classes are in correct modules
- hass_enforce_sorted_platforms - Enforces platform sorting
- hass_enforce_type_hints - Requires type hints
- hass_imports - Validates import patterns
- hass_logger - Checks logger usage
Configuration
Pylint is configured inpyproject.toml:
Mypy
Mypy performs static type checking to catch type-related bugs.Running Mypy
Type Hints
All new code must include type hints:Strict Typing
Certain modules enforce strict typing. These are tracked in the.strict-typing file.
Codespell
Catches spelling mistakes in code, comments, and documentation..pre-commit-config.yaml:
Hassfest
Validates integration metadata, manifests, and configurations.- Integration manifests (
manifest.json) - Service definitions (
services.yaml) - String translations (
strings.json) - Icons (
icons.json) - Requirements and dependencies
Pre-commit Hooks
Home Assistant usesprek (a pre-commit hook manager) to run quality checks automatically.
Install Hooks
Hook Configuration
Hooks are defined in.pre-commit-config.yaml:
Running Hooks Manually
Code Style Guidelines
Imports
Imports should be organized and sorted:Docstrings
Use Google-style docstrings:Line Length
Maximum line length is not strictly enforced (E501 is disabled), but keep lines reasonable for readability. Ruff’s formatter will handle most cases.Naming Conventions
- Variables and functions:
snake_case - Classes:
PascalCase - Constants:
UPPER_CASE - Private methods:
_leading_underscore
Async Code
Prefix async functions withasync_:
Common Issues and Fixes
Import Sorting
If imports are out of order:Type Hint Issues
Add missing type hints:Pylint: Too Many Arguments
Refactor functions with too many parameters:Complexity Issues
If code is too complex (McCabe complexity > 25), refactor:Integration Quality Scale
Home Assistant uses a quality scale for integrations:- Platinum - Highest quality, exemplary code
- Gold - High quality, good example to follow
- Silver - Good quality
- Bronze - Basic quality requirements met
manifest.json:
Automated Checks
All checks run automatically in CI/CD:On Pre-commit
- Ruff formatting and linting
- Codespell
- Mypy type checking (for changed files)
- Pylint (for changed files)
- Hassfest validation
On Pull Request
- All pre-commit checks
- Full test suite
- Coverage report
- Additional validation checks
Best Practices
Write Clean Code
- Keep it simple - Avoid unnecessary complexity
- Be consistent - Follow existing patterns
- Use type hints - Makes code self-documenting
- Add docstrings - Explain what and why, not how
- Handle errors - Don’t let exceptions crash Home Assistant
Performance
- Avoid blocking I/O - Use async for I/O operations
- Cache when appropriate - Don’t repeat expensive operations
- Clean up resources - Implement proper shutdown handlers
Security
- Validate input - Never trust user input
- Use constants - Avoid hardcoded secrets
- Handle credentials safely - Use config entries for sensitive data