wohnbot/state_manager.py

53 lines
No EOL
1.7 KiB
Python

import json
from pathlib import Path
import logging
import os
import dotenv
logger = logging.getLogger(__name__)
dotenv.load_dotenv() # Load environment variables from .env file
class StateManager:
def __init__(self, state_file: Path):
self.state_file = state_file
self.logged_in = False # Initialize logged_in attribute
# Load credentials from environment variables
self.email = os.getenv("INBERLIN_EMAIL")
self.password = os.getenv("INBERLIN_PASSWORD")
if not self.email or not self.password:
logger.warning("Email or password not set in environment variables.")
def load_state(self) -> dict:
"""Load persistent state"""
if self.state_file.exists():
with open(self.state_file, "r") as f:
return json.load(f)
return {"autopilot": False}
def save_state(self, state: dict):
"""Save persistent state"""
with open(self.state_file, "w") as f:
json.dump(state, f, indent=2)
def set_autopilot(self, enabled: bool):
"""Enable or disable autopilot mode"""
state = self.load_state()
state["autopilot"] = enabled
self.save_state(state)
logger.info(f"Autopilot {'enabled' if enabled else 'disabled'}")
def is_autopilot_enabled(self) -> bool:
"""Check if autopilot mode is enabled"""
return self.load_state().get("autopilot", False)
def set_logged_in(self, status: bool):
"""Set the logged_in status"""
self.logged_in = status
logger.info(f"Logged in status set to: {status}")
def is_logged_in(self) -> bool:
"""Check the logged_in status"""
return self.logged_in