Merge the bridge and a home-screen interface into one app

The HEOS app is unpleasant to use for the four things that actually get
done in this house, so this is those four things on one screen: volume
for the Home 400 and the Living Room pair, joining either to the AVR,
splitting them off again, and picking the AVR's input.

The bridge's logic moves in mostly intact, split into a HEOS client, an
AVR client and a controller, with two grouping bugs fixed on the way:

  * Merging a room into the AVR sent only the pair's leader pid, which
    left the second Home 200 behind. Group targets now expand to every
    member pid, and unmerging re-forms the pair rather than leaving two
    lone speakers. The members are learned while the pair is visible and
    remembered in members.json so a restart can still rebuild it.

  * Volume for the pair used its gid, which stops existing the moment it
    joins the AVR's group. It now falls back to the member players.

Both sockets are kept open rather than reconnecting per command, which
is what made every button press feel slow; the AVR connection doubles as
a listener, so input changes made with the physical remote show up too.

The original query-string endpoints still answer, so anything already
pointed at the bridge keeps working.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-14 21:15:43 +02:00
co-authored by Claude Opus 5
parent 86fdf9a4d3
commit 375bcfdd76
20 changed files with 2428 additions and 490 deletions
+119
View File
@@ -0,0 +1,119 @@
"""A pretend HEOS network, for `python3 app.py --demo`.
Lets you work on the interface on a laptop, with no speakers on the
network -- and lets the panel be tested without waking the house up.
"""
import time
class _FakeAvr:
INPUTS = [
{"code": "MPLAY", "name": "Apple TV"},
{"code": "GAME", "name": "PlayStation"},
{"code": "SAT/CBL", "name": "TV Box"},
{"code": "BD", "name": "Blu-ray"},
{"code": "TUNER", "name": "Radio"},
{"code": "PHONO", "name": "Turntable"},
]
def __init__(self, allowed_codes=()):
self.allowed_codes = list(allowed_codes or [])
self.connected = True
self._code = "MPLAY"
def inputs(self, refresh=False):
sources = list(self.INPUTS)
if self.allowed_codes:
order = {code: i for i, code in enumerate(self.allowed_codes)}
sources = sorted((s for s in sources if s["code"] in order), key=lambda s: order[s["code"]])
return sources
def name_for(self, code):
return next((s["name"] for s in self.INPUTS if s["code"] == code), code)
def current_input(self):
return {"code": self._code, "name": self.name_for(self._code)}
def select_input(self, code):
time.sleep(0.15) # the real AVR is not instant either
self._code = code
return self.current_input()
class DemoController:
def __init__(self, cfg):
self.cfg = cfg
self.avr = _FakeAvr(cfg.AVR_INPUT_CODES)
self.heos = None
self._volume = {key: 22 + 7 * i for i, key in enumerate(cfg.TARGETS)}
self._joined = set()
# -- what the UI uses ---------------------------------------------
def state(self):
return {
"host": {"key": self.cfg.HOST_KEY, "label": self.cfg.TARGETS[self.cfg.HOST_KEY]["label"]},
"rooms": [
{"key": key, "label": self.cfg.TARGETS[key]["label"], "available": True,
"grouped": key in self._joined, "volume": self._volume[key], "error": None}
for key in self.cfg.ROOM_KEYS
],
"avr": {"connected": True, "inputs": self.avr.inputs(), "input": self.avr.current_input()},
"heos_ok": True,
"errors": [],
"demo": True,
}
def volume(self, key):
return self._volume[key]
def set_volume(self, key, level):
self._volume[key] = max(0, min(100, int(level)))
return self._volume[key]
def nudge_volume(self, key, delta):
return self.set_volume(key, self._volume[key] + int(delta))
def toggle_mute(self, key):
return None
def join(self, key):
self._joined.add(key)
return self.joined_keys()
def leave(self, key):
self._joined.discard(key)
return self.joined_keys()
def set_membership(self, joined):
self._joined = {k for k in self.cfg.ROOM_KEYS if k in joined}
return self.joined_keys()
def joined_keys(self):
return [k for k in self.cfg.ROOM_KEYS if k in self._joined]
# -- enough of the rest to keep the legacy routes answering --------
def scan(self):
return {
"players": [{"name": t["heos_name"], "pid": 1000 + i, "model": "Demo"}
for i, t in enumerate(self.cfg.TARGETS.values())],
"groups": [],
}
def group_targets(self, host_key, member_keys):
return self.set_membership(member_keys)
def ungroup(self, key):
return self.leave(key)
def play_state(self, key, state):
return None
def skip(self, key, direction):
return None
def heos_inputs(self, key):
return [{"name": s["name"], "input_id": f"inputs/{s['code'].lower()}"} for s in self.avr.inputs()]
def play_heos_input(self, key, input_id, source_key=None):
return None