Add Shift+Enter secondary item action with radio handler
This commit is contained in:
@@ -28,6 +28,7 @@ class ItemModule(Protocol):
|
||||
PROPERTY_METADATA: dict[str, dict[str, object]]
|
||||
validate_update: Callable[[WorldItem, dict], dict]
|
||||
use_item: Callable[[WorldItem, str, Callable[[dict], str]], ItemUseResult]
|
||||
secondary_use_item: Callable[[WorldItem, str, Callable[[dict], str]], ItemUseResult] | None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
||||
@@ -6,7 +6,7 @@ from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
|
||||
def build_item_module(definition: Any, *, validate_update: Any, use_item: Any) -> Any:
|
||||
def build_item_module(definition: Any, *, validate_update: Any, use_item: Any, secondary_use_item: Any = None) -> Any:
|
||||
"""Compose a plugin module-like object from split definition/validator/actions files."""
|
||||
|
||||
exports: dict[str, Any] = {
|
||||
@@ -16,4 +16,6 @@ def build_item_module(definition: Any, *, validate_update: Any, use_item: Any) -
|
||||
}
|
||||
exports["validate_update"] = validate_update
|
||||
exports["use_item"] = use_item
|
||||
if secondary_use_item is not None:
|
||||
exports["secondary_use_item"] = secondary_use_item
|
||||
return SimpleNamespace(**exports)
|
||||
|
||||
@@ -19,3 +19,25 @@ def use_item(item: WorldItem, nickname: str, _clock_formatter: Callable[[dict],
|
||||
others_message=f"{nickname} turns {state_text} {item.title}.",
|
||||
updated_params={**item.params, "enabled": next_enabled},
|
||||
)
|
||||
|
||||
|
||||
def secondary_use_item(item: WorldItem, _nickname: str, _clock_formatter: Callable[[dict], str]) -> ItemUseResult:
|
||||
"""Speak now-playing metadata for this radio."""
|
||||
|
||||
if item.params.get("enabled") is False:
|
||||
return ItemUseResult(
|
||||
self_message=f"{item.title} is off.",
|
||||
others_message=f"{item.title} is off.",
|
||||
)
|
||||
|
||||
station_name = str(item.params.get("stationName", "")).strip()
|
||||
now_playing = str(item.params.get("nowPlaying", "")).strip()
|
||||
if now_playing and station_name:
|
||||
message = f"Playing {now_playing} from {station_name}."
|
||||
elif now_playing:
|
||||
message = f"Playing {now_playing}."
|
||||
elif station_name:
|
||||
message = f"Playing from {station_name}."
|
||||
else:
|
||||
message = "No now playing data."
|
||||
return ItemUseResult(self_message=message, others_message=message)
|
||||
|
||||
@@ -8,5 +8,10 @@ from . import actions, definition, validator
|
||||
ITEM_TYPE_PLUGIN = {
|
||||
"type": "radio_station",
|
||||
"order": 40,
|
||||
"module": build_item_module(definition, validate_update=validator.validate_update, use_item=actions.use_item),
|
||||
"module": build_item_module(
|
||||
definition,
|
||||
validate_update=validator.validate_update,
|
||||
use_item=actions.use_item,
|
||||
secondary_use_item=actions.secondary_use_item,
|
||||
),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user