Add volume handling to Preferences and persistence method

Added volume attribute to Preferences and implemented persist_volume method to save normalized media volume.
This commit is contained in:
Omar Amin authored and GitHub committed 2026-02-21 16:25:16 +02:00
1 parent c91511c93e
commit ac69618d29
1 file changed
+17
+17
View File
@@ -57,6 +57,7 @@ class AvailableWakeWord:
@dataclass @dataclass
class Preferences: class Preferences:
active_wake_words: List[str] = field(default_factory=list) active_wake_words: List[str] = field(default_factory=list)
volume: Optional[float] = None
thinking_sound: int = 0 # 0 = disabled, 1 = enabled thinking_sound: int = 0 # 0 = disabled, 1 = enabled
@@ -92,6 +93,7 @@ class ServerState:
thinking_sound_enabled: bool = False thinking_sound_enabled: bool = False
muted: bool = False muted: bool = False
connected: bool = False connected: bool = False
volume: float = 1.0
def save_preferences(self) -> None: def save_preferences(self) -> None:
"""Save preferences as JSON.""" """Save preferences as JSON."""
@@ -101,3 +103,18 @@ class ServerState:
json.dump( json.dump(
asdict(self.preferences), preferences_file, ensure_ascii=False, indent=4 asdict(self.preferences), preferences_file, ensure_ascii=False, indent=4
) )
def persist_volume(self, volume: float) -> None:
"""Persist the normalized media volume (0.0 - 1.0)."""
clamped_volume = max(0.0, min(1.0, volume))
if (
abs(self.volume - clamped_volume) < 0.0001
and self.preferences.volume is not None
and abs(self.preferences.volume - clamped_volume) < 0.0001
):
return
self.volume = clamped_volume
self.preferences.volume = clamped_volume
self.save_preferences()