Skip to main content
Home Assistant maintains high code quality standards through automated linting, formatting, and type checking.

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 in pyproject.toml:

Pylint

Pylint provides deeper code analysis with custom Home Assistant plugins.

Running Pylint

Custom Plugins

Home Assistant includes custom pylint plugins in pylint/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 in pyproject.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.
Configuration in .pre-commit-config.yaml:

Hassfest

Validates integration metadata, manifests, and configurations.
Hashfest validates:
  • Integration manifests (manifest.json)
  • Service definitions (services.yaml)
  • String translations (strings.json)
  • Icons (icons.json)
  • Requirements and dependencies

Pre-commit Hooks

Home Assistant uses prek (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:
Ruff’s isort integration handles this automatically:

Docstrings

Use Google-style docstrings:
Configuration:

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 with async_:

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
Quality level is indicated in manifest.json:
When looking for code examples, refer to Platinum or Gold-level integrations.

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

Resources