3.8 KiB
Copilot Instructions
This repository contains a custom component for Home Assistant, intended to be distributed via the HACS (Home Assistant Community Store).
Development Guidelines
- Follow the latest official practices from Home Assistant and HACS.
- Ensure compatibility with the latest Home Assistant release.
- Always use the current real-world date when working with times or schedules — do not use hardcoded or outdated values.
- Use async functions, non-blocking I/O, and config flows when applicable.
- Structure the component using standard files:
__init__.py,manifest.json,config_flow.py(if needed), and proper versioning. - Use Home Assistant built-in libraries and helpers whenever possible:
- For dates: use
homeassistant.util.dt(dt_util) - For configs: use
homeassistant.helpers.config_validation - For state handling: use
homeassistant.helpers.entity
- For dates: use
- Avoid wrapping built-in utilities (e.g., do not wrap
dt_util.parse_datetime) - Avoid using custom libraries unless absolutely necessary and justified
Coding Style
- Follow PEP8 and Home Assistant's coding conventions
- Use type hints for all function and method signatures
- Add docstrings to all public classes and public methods
- Use f-strings for formatting, not
%or.format() - Use relative paths and configurable options, not hardcoded values
- Provide valid and clean YAML examples when needed (e.g., for
configuration.yaml)
Code Structure and Ordering
Follow this standard order within Python modules:
-
Imports
- Python standard library imports
- Third-party libraries (
homeassistant.*) - Local imports (
from . import xyz) - Use
isortto enforce order
-
Module-level constants and globals
- Example:
DOMAIN,_LOGGER,CONF_*,DEFAULT_*
- Example:
-
Top-level functions
- Only define if they are not part of a class
-
Main classes
- Core classes first: entity classes, coordinators, config flows
- Method order within each class:
- Special methods (
__init__,__str__) first - Public methods (no
_prefix), in logical order of usage - Private methods (prefix
_), grouped below public ones
- Special methods (
-
Helper classes
- Place after main classes, or move to separate modules if complex
- Use
async deffor I/O or Home Assistant lifecycle methods - Split large files into multiple modules if needed
✅ Copilot tip: Use public methods first, private methods after. Avoid mixing. Keep file structure consistent.
Optional: Code Folding Regions
You may use #region and #endregion comments to group related logic. Only apply in large files and where folding improves clarity.
Linting and Code Quality
-
Use Ruff, which runs:
- Locally in the devcontainer (VS Code or Cursor)
- Remotely via GitHub Actions
-
Required Ruff rules:
F401,F841– No unused imports or variablesE402,E501– Imports at top, lines ≤88 charactersC901,PLR0912,PLR0915– Keep functions small and simplePLR0911,RET504– Avoid unnecessaryelseafterreturnB008– No mutable default argumentsT201– Use_LOGGER, notprint()SIM102– Useif x, notif x == True
-
Prefer a single return statement at the end of functions
-
Avoid early returns unless they improve clarity
-
Use Black for formatting and isort for sorting imports
-
Refer to
.ruff.tomlfor configuration details
✅ Copilot tip: Generate clean, single-pass functions with clear returns. Don’t leave unused code.
Tests
This project does not include automated tests.
⚠️ If generating tests, keep them minimal and avoid introducing test frameworks not already present.