fix PageManager

This commit is contained in:
2026-01-30 12:30:38 +03:00
parent dc7fd19022
commit 958279b1b7
5 changed files with 77 additions and 431 deletions

View File

@@ -84,7 +84,14 @@ class PageManager:
],
))
# Page 4: Confirm (dynamic)
# Page 4: UPS Status (if available)
self._pages.append(PageDefinition(
page_type=PageType.INFO,
title="UPS Status",
generator=self._generate_ups_page,
))
# Page 5: Confirm (dynamic)
self._pages.append(PageDefinition(
page_type=PageType.CONFIRM,
title="Confirm",
@@ -110,15 +117,42 @@ class PageManager:
"""
self._data_providers[name] = provider
def set_action_handler(self, action_id: ActionID, handler: Callable[[], bool]) -> None:
def set_action_handler(self, action_id: ActionID | str, handler: Callable[[], bool]) -> None:
"""Register an action handler.
Args:
action_id: Action identifier
action_id: Action identifier (ActionID enum or string)
handler: Callable that executes action, returns True on success
"""
self._action_handlers[action_id] = handler
def add_page(self, page: Any) -> None:
"""Add a custom page to the page manager.
Args:
page: Page object with get_content(), is_enabled(), name, title attributes
"""
# Create a PageDefinition wrapper for custom page
def generator(mgr: "PageManager") -> Page:
content = page.get_content()
return Page(
page_type=PageType(content.page_type.value),
title=content.title,
lines=content.lines,
actions=content.actions,
selected=content.selected,
)
# Insert before the confirm page (last page)
page_def = PageDefinition(
page_type=PageType.INFO,
title=page.title,
generator=generator,
)
# Insert at position -1 (before confirm page)
self._pages.insert(len(self._pages) - 1, page_def)
_logger.info(f"Added custom page: {page.name}")
def get_data(self, name: str, default: Any = None) -> Any:
"""Get data from a provider.
@@ -435,6 +469,40 @@ class PageManager:
]
return Page(PageType.MENU, "Actions", actions=actions)
def _generate_ups_page(self, mgr: "PageManager") -> Page:
"""Generate UPS status page."""
ups_data = mgr.get_data("ups")
if ups_data is None:
lines = ["UPS not available", "", "Check I2C connection"]
return Page(PageType.INFO, "UPS Status", lines)
# Battery bar visualization
capacity = ups_data.capacity if hasattr(ups_data, 'capacity') else 0
filled = int(capacity / 16.67) # 6 segments
empty = 6 - filled
bar = "[" + "=" * filled + " " * empty + "]"
lines = [
f"Bat: {capacity:.1f}% {bar}",
f"Voltage: {ups_data.voltage:.2f}V",
]
if hasattr(ups_data, 'power_loss') and ups_data.power_loss:
lines.append("Power: BATTERY MODE")
if capacity < 15:
lines.append("!! CRITICAL !!")
elif capacity < 25:
lines.append("! LOW BATTERY !")
else:
status = "Charging" if getattr(ups_data, 'is_charging', False) else "Full"
lines.append(f"Status: {status}")
input_v = getattr(ups_data, 'input_voltage', 0)
if input_v > 0:
lines.append(f"Input: {input_v:.2f}V")
return Page(PageType.INFO, "UPS Status", lines)
def _generate_confirm_page(self, mgr: "PageManager") -> Page:
"""Generate confirmation page."""
if mgr._pending_action == ActionID.REBOOT_SYSTEM: