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)" app_env: str = "development" app_debug: bool = False app_host: str = "0.0.0.0" app_port: int = 8000 location_database_url: str = "sqlite:///./data/locationRecorder.db" poo_database_url: str = "sqlite:///./data/pooRecorder.db" ticktick_client_id: str = "" ticktick_client_secret: str = "" ticktick_redirect_uri: str = "" ticktick_token: str = "" home_assistant_base_url: str = "" home_assistant_auth_token: str = "" home_assistant_timeout_seconds: float = 1.0 home_assistant_action_task_project_id: str = "" poo_webhook_id: str = "" poo_sensor_entity_name: str = "sensor.test_poo_status" poo_sensor_friendly_name: str = "Poo Status" model_config = SettingsConfigDict( env_file=".env", env_file_encoding="utf-8", case_sensitive=False, ) @computed_field @property def is_development(self) -> bool: return self.app_env.lower() == "development" @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) @computed_field @property def location_sqlite_path(self) -> Path | None: return self._sqlite_path_from_url(self.location_database_url) @computed_field @property def poo_sqlite_path(self) -> Path | None: return self._sqlite_path_from_url(self.poo_database_url) @lru_cache def get_settings() -> Settings: return Settings()