Files
home-automation/app/config.py
T

106 lines
3.1 KiB
Python
Raw Normal View History

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
location_database_url: str = "sqlite:///./data/locationRecorder.db"
poo_database_url: str = "sqlite:///./data/pooRecorder.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 = ""
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
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-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"
@staticmethod
def _sqlite_path_from_url(database_url: str) -> Path | None:
2026-04-19 20:19:58 +02:00
prefix = "sqlite:///"
if not database_url.startswith(prefix):
2026-04-19 20:19:58 +02:00
return None
raw_path = database_url[len(prefix) :]
2026-04-19 20:19:58 +02:00
return Path(raw_path)
@computed_field
@property
def location_sqlite_path(self) -> Path | None:
return self._sqlite_path_from_url(self.location_database_url)
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 poo_sqlite_path(self) -> Path | None:
return self._sqlite_path_from_url(self.poo_database_url)
2026-04-20 15:16:47 +02:00
@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-04-19 20:19:58 +02:00
@lru_cache
def get_settings() -> Settings:
return Settings()