Add runtime script management with Home Assistant integration

Features:
- Runtime script CRUD operations (create, update, delete)
- Thread-safe ConfigManager for YAML updates
- WebSocket notifications for script changes
- Web UI script management interface with full CRUD
- Home Assistant auto-reload on script changes
- Client-side position interpolation for smooth playback
- Include command field in script list API response

Technical improvements:
- Added broadcast_scripts_changed() to WebSocket manager
- Enhanced HA integration to handle scripts_changed messages
- Implemented smooth position updates in Web UI (100ms interval)
- Thread-safe configuration updates with file locking

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-06 03:53:23 +03:00
parent 71a0a6e6d1
commit d7c5994e56
8 changed files with 1013 additions and 26 deletions

View File

@@ -54,41 +54,100 @@ except ImportError:
# Volume control imports
PYCAW_AVAILABLE = False
_volume_control = None
_configured_device_name: str | None = None
try:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL, CoInitialize, CoUninitialize
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
def _init_volume_control():
"""Initialize volume control interface."""
global _volume_control
if _volume_control is not None:
return _volume_control
import warnings
# Suppress pycaw warnings about missing device properties
warnings.filterwarnings("ignore", category=UserWarning, module="pycaw")
def _get_all_audio_devices() -> list[dict[str, str]]:
"""Get list of all audio output devices."""
devices = []
try:
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
# Use pycaw's GetAllDevices which handles property retrieval
all_devices = AudioUtilities.GetAllDevices()
for device in all_devices:
# Only include render (output) devices with valid names
# Render devices have IDs starting with {0.0.0
if device.FriendlyName and device.id and device.id.startswith("{0.0.0"):
devices.append({
"id": device.id,
"name": device.FriendlyName,
})
except Exception as e:
logger.error(f"Error enumerating audio devices: {e}")
return devices
def _find_device_by_name(device_name: str):
"""Find an audio device by its friendly name (partial match).
Returns the AudioDevice wrapper for the matched device.
"""
try:
# Get all devices and find matching one
all_devices = AudioUtilities.GetAllDevices()
for device in all_devices:
if device.FriendlyName and device_name.lower() in device.FriendlyName.lower():
logger.info(f"Found audio device: {device.FriendlyName}")
return device
except Exception as e:
logger.error(f"Error finding device by name: {e}")
return None
def _init_volume_control(device_name: str | None = None):
"""Initialize volume control interface.
Args:
device_name: Name of the audio device to control (partial match).
If None, uses the default audio device.
"""
global _volume_control, _configured_device_name
if _volume_control is not None and device_name == _configured_device_name:
return _volume_control
_configured_device_name = device_name
try:
if device_name:
# Find specific device by name
device = _find_device_by_name(device_name)
if device is None:
logger.warning(f"Audio device '{device_name}' not found, using default")
device = AudioUtilities.GetSpeakers()
else:
# Use default device
device = AudioUtilities.GetSpeakers()
if hasattr(device, 'Activate'):
interface = device.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
elif hasattr(device, '_dev'):
interface = device._dev.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
else:
logger.warning("Could not activate audio device")
return None
_volume_control = cast(interface, POINTER(IAudioEndpointVolume))
return _volume_control
except AttributeError:
# Try accessing the underlying device
try:
devices = AudioUtilities.GetSpeakers()
if hasattr(devices, '_dev'):
interface = devices._dev.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
_volume_control = cast(interface, POINTER(IAudioEndpointVolume))
return _volume_control
except Exception as e:
logger.debug(f"Volume control init failed: {e}")
except Exception as e:
logger.debug(f"Volume control init error: {e}")
logger.error(f"Volume control init error: {e}")
return None
PYCAW_AVAILABLE = True
except ImportError as e:
logger.warning(f"pycaw not available: {e}")
def _init_volume_control():
def _get_all_audio_devices() -> list[dict[str, str]]:
return []
def _find_device_by_name(device_name: str):
return None
def _init_volume_control(device_name: str | None = None):
return None
WINDOWS_AVAILABLE = WINSDK_AVAILABLE
@@ -427,25 +486,38 @@ def _sync_seek(position: float) -> bool:
class WindowsMediaController(MediaController):
"""Media controller for Windows using WinRT and pycaw."""
def __init__(self):
def __init__(self, audio_device: str | None = None):
"""Initialize the Windows media controller.
Args:
audio_device: Name of the audio device to control (partial match).
If None, uses the default audio device.
"""
if not WINDOWS_AVAILABLE:
raise RuntimeError(
"Windows media control requires winsdk, pycaw, and comtypes packages"
)
self._volume_interface = None
self._volume_init_attempted = False
self._audio_device = audio_device
def _get_volume_interface(self):
"""Get the audio endpoint volume interface."""
if not self._volume_init_attempted:
self._volume_init_attempted = True
self._volume_interface = _init_volume_control()
self._volume_interface = _init_volume_control(self._audio_device)
if self._volume_interface:
logger.info("Volume control initialized successfully")
device_info = f" (device: {self._audio_device})" if self._audio_device else " (default device)"
logger.info(f"Volume control initialized successfully{device_info}")
else:
logger.warning("Volume control not available")
return self._volume_interface
@staticmethod
def get_audio_devices() -> list[dict[str, str]]:
"""Get list of available audio output devices."""
return _get_all_audio_devices()
async def get_status(self) -> MediaStatus:
"""Get current media playback status."""
status = MediaStatus()