mirror of
https://github.com/cassandra/home-information.git
synced 2026-04-17 21:19:45 -04:00
* Working on improving AI agent docs. * Update dev/docs and claude agent configs. * Updated claude commands.
3.0 KiB
3.0 KiB
Backend Guidelines
Core Concepts & Modules
App Module Structure
Key Base Classes
hi.utils.singleton.Singleton- All manager classeshi.apps.common.enums.LabeledEnum- All enumshi.apps.common.model_fields.LabeledEnumField- Enum storage in models
Design Patterns
Entity-Centric Architecture
All controllable/observable items modeled as entities with states:
Entitymodel withIntegrationKeyMixinfor external system mappingEntityStatefor current state tracking- Integration key format:
"integration_name:integration_id"
Singleton Manager Pattern
Core functionality uses singleton pattern with thread-safe initialization:
- Implement
__init_singleton__()not__init__() - Use
cls.instance()for access - Include
ensure_initialized()andensure_initialized_async()methods
Dual Sync/Async Support
Manager classes provide both sync (Django views) and async (integrations) methods:
- Sync:
get_data(),do_control() - Async:
get_data_async(),do_control_async()
Database Patterns
- Strategic indexing: Use
db_index=Trueand composite indexes - Entity queries: Always use
select_related()for foreign keys - Enum storage: CharField with
LabeledEnumField, not Django choices
Django View Philosophy
Simple Views
Keep view classes lightweight - delegate complex logic to helper classes:
- Business logic → Helper classes
- Database queries → Manager classes
- Data structure construction → Dedicated builder classes
View Patterns
- Use mixins for common functionality (
EntityMixin,LocationViewMixin) - Custom base classes for specialized behaviors (
HiModalView) - Always use Django URL names, never hardcoded URLs
Settings & Configuration
App Settings Auto-Discovery
Create settings.py in any app with SettingEnum subclass:
- Automatically appears on config page
- Requires
./manage.py migrateafter adding new settings - Debug internal settings at
/config/internal
Adding Config UI Sections
- Add entry to
ConfigPageTypeenum - Create view extending
ConfigPageView - Override required methods
- Create template extending
config/pages/config_base.html
Performance Patterns
Caching
- TTL caching:
cachetools.TTLCachewith thread locks - Memory efficiency:
collections.dequefor circular buffers
Threading & AsyncIO
- Background threads: Daemon threads with graceful shutdown
- Django+AsyncIO: Use
asgiref.sync.sync_to_asyncfor ORM access
Development Tools
Debug Settings
Modify src/hi/settings/development.py:
SUPPRESS_SELECT_REQUEST_ENPOINTS_LOGGING- Hide polling requestsSUPPRESS_MONITORS- Disable background tasksBASE_URL_FOR_EMAIL_LINKS- Email link testing