2026-04-19 20:19:58 +02:00
|
|
|
from functools import lru_cache
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from pydantic import computed_field
|
|
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
|
|
|
app_name: str = "Home Automation Backend (Python)"
|
2026-04-20 17:36:05 +02:00
|
|
|
app_env: str = "production"
|
2026-04-19 20:19:58 +02:00
|
|
|
app_debug: bool = False
|
2026-04-20 17:36:05 +02:00
|
|
|
app_hostname: str = "localhost:8000"
|
2026-04-20 15:16:47 +02:00
|
|
|
app_database_url: str = "sqlite:///./data/app.db"
|
2026-04-19 20:19:58 +02:00
|
|
|
|
|
|
|
|
ticktick_client_id: str = ""
|
|
|
|
|
ticktick_client_secret: str = ""
|
|
|
|
|
ticktick_token: str = ""
|
|
|
|
|
|
|
|
|
|
home_assistant_base_url: str = ""
|
|
|
|
|
home_assistant_auth_token: str = ""
|
2026-04-20 10:11:02 +02:00
|
|
|
home_assistant_timeout_seconds: float = 1.0
|
2026-04-19 20:19:58 +02:00
|
|
|
home_assistant_action_task_project_id: str = ""
|
2026-04-29 12:11:10 +02:00
|
|
|
smtp_enabled: bool = False
|
|
|
|
|
smtp_host: str = ""
|
|
|
|
|
smtp_port: int = 587
|
|
|
|
|
smtp_username: str = ""
|
|
|
|
|
smtp_password: str = ""
|
2026-04-29 13:03:12 +02:00
|
|
|
smtp_from_name: str = ""
|
2026-04-29 12:11:10 +02:00
|
|
|
smtp_from_address: str = ""
|
|
|
|
|
smtp_to_address: str = ""
|
|
|
|
|
smtp_use_starttls: bool = True
|
2026-04-20 11:48:48 +02:00
|
|
|
poo_webhook_id: str = ""
|
|
|
|
|
poo_sensor_entity_name: str = "sensor.test_poo_status"
|
|
|
|
|
poo_sensor_friendly_name: str = "Poo Status"
|
2026-04-20 15:16:47 +02:00
|
|
|
auth_bootstrap_username: str = "admin"
|
|
|
|
|
auth_bootstrap_password: str = "admin"
|
|
|
|
|
auth_session_cookie_name: str = "home_automation_session"
|
|
|
|
|
auth_session_ttl_hours: int = 12
|
2026-04-20 17:36:05 +02:00
|
|
|
auth_cookie_secure_override: bool | None = True
|
2026-06-21 21:20:28 +02:00
|
|
|
auth_login_throttle_enabled: bool = True
|
|
|
|
|
auth_trust_forwarded_for: bool = False
|
2026-06-21 21:55:17 +02:00
|
|
|
auth_totp_issuer: str = "" # defaults to app_name when empty
|
2026-04-19 20:19:58 +02:00
|
|
|
|
2026-06-22 14:27:21 +02:00
|
|
|
# Modbus polling — global kill-switch (CONFIG_FIELDS registered in T08).
|
2026-06-22 20:28:12 +02:00
|
|
|
# Off by default: polling is opt-in so a fresh deploy writes no modbus_reading
|
|
|
|
|
# rows until the admin explicitly enables it (matches mqtt/discovery defaults).
|
|
|
|
|
modbus_polling_enabled: bool = False
|
2026-06-22 13:24:11 +02:00
|
|
|
|
2026-06-22 14:27:21 +02:00
|
|
|
# MQTT broker connection (T08 wires into CONFIG_FIELDS/UI; T10 builds the client).
|
|
|
|
|
mqtt_enabled: bool = False
|
|
|
|
|
mqtt_broker_host: str = ""
|
|
|
|
|
mqtt_broker_port: int = 1883
|
|
|
|
|
mqtt_username: str = ""
|
|
|
|
|
mqtt_password: str = ""
|
|
|
|
|
mqtt_tls_enabled: bool = False
|
|
|
|
|
|
|
|
|
|
# Home Assistant MQTT Discovery (T08 wires into CONFIG_FIELDS/UI; T11 does publishing).
|
|
|
|
|
ha_discovery_enabled: bool = False
|
|
|
|
|
ha_discovery_prefix: str = "homeassistant"
|
|
|
|
|
|
2026-04-19 20:19:58 +02:00
|
|
|
model_config = SettingsConfigDict(
|
|
|
|
|
env_file=".env",
|
|
|
|
|
env_file_encoding="utf-8",
|
|
|
|
|
case_sensitive=False,
|
2026-04-20 17:36:05 +02:00
|
|
|
extra="ignore",
|
2026-04-19 20:19:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def is_development(self) -> bool:
|
|
|
|
|
return self.app_env.lower() == "development"
|
|
|
|
|
|
2026-04-20 17:36:05 +02:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def app_base_url(self) -> str:
|
|
|
|
|
hostname = self.app_hostname.strip().rstrip("/")
|
|
|
|
|
if not hostname:
|
|
|
|
|
return ""
|
|
|
|
|
scheme = "http" if self.is_development else "https"
|
|
|
|
|
return f"{scheme}://{hostname}"
|
|
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def ticktick_redirect_uri(self) -> str:
|
|
|
|
|
if not self.app_base_url:
|
|
|
|
|
return ""
|
|
|
|
|
return f"{self.app_base_url}/ticktick/auth/code"
|
|
|
|
|
|
2026-04-19 21:39:23 +02:00
|
|
|
@staticmethod
|
|
|
|
|
def _sqlite_path_from_url(database_url: str) -> Path | None:
|
|
|
|
|
prefix = "sqlite:///"
|
|
|
|
|
if not database_url.startswith(prefix):
|
|
|
|
|
return None
|
|
|
|
|
raw_path = database_url[len(prefix) :]
|
|
|
|
|
return Path(raw_path)
|
|
|
|
|
|
2026-04-20 15:16:47 +02:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def app_sqlite_path(self) -> Path | None:
|
|
|
|
|
return self._sqlite_path_from_url(self.app_database_url)
|
|
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def auth_cookie_secure(self) -> bool:
|
|
|
|
|
if self.auth_cookie_secure_override is not None:
|
|
|
|
|
return self.auth_cookie_secure_override
|
|
|
|
|
return not self.is_development
|
|
|
|
|
|
2026-06-21 21:55:17 +02:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def effective_totp_issuer(self) -> str:
|
|
|
|
|
"""The issuer label shown in Authenticator apps. Falls back to app_name."""
|
|
|
|
|
return self.auth_totp_issuer.strip() or self.app_name
|
|
|
|
|
|
2026-04-19 20:19:58 +02:00
|
|
|
|
|
|
|
|
@lru_cache
|
|
|
|
|
def get_settings() -> Settings:
|
|
|
|
|
return Settings()
|