Loxone app: a playing zone shows as idle on the main page after the app starts (zone state sent before the app authenticates)
The problem
On the Loxone app's main page, the tile of a zone that is playing shows it as not playing: not highlighted, no volume buttons. Opening the zone shows it playing. The tile comes right only when something in the zone's state changes (the next song's title, a volume step), and goes wrong again the next time the app starts.
Observed behaviour
On dev (0fd45d23), with a Miniserver and the Loxone app on a phone, on a zone that follows a networked player playing a radio of its own (an external state controller):
- App quit and started again: the tile shows "fetching status", then the station's name with the tile off, and stays off until the zone's state changes.
- Every
audio_eventfor the zone saysmode: playthroughout; nothing is lost. - While a radio plays, the zone's state does not change from one second to the next:
ZoneStateStore.patchsets a radio'stimeanddurationto 0 (unlessradioControllable), so the only events are the 60 s rebroadcasts, all identical.
Cause
Read in the Loxone app (macOS build 17.1.1, AudioZoneV2ControlStateContainer):
- The tile's playing state and its volume buttons need the player's health to be
online(canPlayMusic). The health isinitializinguntil the app's own connection to the audio server has authenticated. - They are worked out in
prepareStates, which runs when the Miniserver sends a state of the zone or of the audio server, or when anaudio_eventarrives that differs from the previous one: the app hashes the whole event and returns early on an identical one. A change of health only redraws the tile (_updateStates(true)skipsprepareStates). - sonn sends every zone's state in
handleWebSocket, right after the identification, before the app has sentsecure/authenticate. The app takes that first state while the health is stillinitializing, so the tile is off; the health then turnsonlinewithout the tile being worked out again; and every later event of a radio or a stopped zone is identical, so it is ignored.
A zone whose time moves every second (a track) comes right within a second, which is why this shows with radios, and with stopped zones as missing volume buttons.
Fix
Send an app connection the zones' state once its secure/authenticate has been answered; after 5 s without one, send it anyway, as before. The Miniserver's connection and the group list on connect are unchanged.
@@ -24,6 +24,12 @@ import type { ZoneManagerFacade } from '@/application/zones/createZoneManager';
import type { GroupSyncSnapshotPort } from '@/application/groups/groupManager';
import type { ConfigPort } from '@/ports/ConfigPort';
+/**
+ * How long an app connection waits for its `secure/authenticate` before it is sent the zones' state
+ * anyway: a client that does not authenticate gets it as it did before.
+ */
+const APP_SNAPSHOT_FALLBACK_MS = 5_000;
+
interface ServerRuntime {
definition: LoxoneServerOptions;
httpServer: http.Server;
@@ -56,6 +62,8 @@ export class LoxoneHttpService {
private readonly processor: LoxoneCommandProcessor;
private readonly servers: ServerRuntime[] = [];
private readonly udpDiscovery = new LoxoneUdpDiscovery();
+ /** App connections whose zone snapshot waits for their authentication, and how to send it. */
+ private readonly awaitingAuthentication = new Map<WebSocketConnection, () => void>();
constructor(
private readonly config: LoxoneHttpConfig,
@@ -220,15 +228,16 @@ export class LoxoneHttpService {
// audiopath/title/station guards, so a reconnect during grouped playback
// showed ungrouped zones, and a raw service-native id could surface in a
// field the native client renders verbatim.
- for (const state of this.options.zoneManager.getAllZoneStates()) {
- try {
- connection.sendUTF(
- JSON.stringify({ audio_event: [this.options.notifier.projectForLoxone(state)] }),
- );
- } catch {
- // connection will be cleaned up via the error/close event
- break;
- }
+ //
+ // To the app, once it has authenticated. Until then the server is still
+ // "initializing" to it, and the app works out whether a zone's tile on its main
+ // page shows it playing, with its volume buttons, from that first state; it
+ // works it out again only on a state that differs (app 17.1.1). A zone whose
+ // state then stayed the same (a radio, a stopped zone) was shown off.
+ if (definition.name === 'msHttp') {
+ this.sendZoneSnapshot(connection);
+ } else {
+ this.sendZoneSnapshotOnceAuthenticated(connection);
}
// Groups are announced only when they change, so one restored from config at
@@ -253,6 +262,34 @@ export class LoxoneHttpService {
connection.on('error', () => this.options.connectionRegistry.unregisterConnection(connection));
}
+ private sendZoneSnapshot(connection: WebSocketConnection): void {
+ for (const state of this.options.zoneManager.getAllZoneStates()) {
+ try {
+ connection.sendUTF(
+ JSON.stringify({ audio_event: [this.options.notifier.projectForLoxone(state)] }),
+ );
+ } catch {
+ // connection will be cleaned up via the error/close event
+ break;
+ }
+ }
+ }
+
+ private sendZoneSnapshotOnceAuthenticated(connection: WebSocketConnection): void {
+ const forget = (): void => {
+ clearTimeout(fallback);
+ this.awaitingAuthentication.delete(connection);
+ };
+ const send = (): void => {
+ forget();
+ if (connection.connected) this.sendZoneSnapshot(connection);
+ };
+ const fallback = setTimeout(send, APP_SNAPSHOT_FALLBACK_MS);
+ fallback.unref?.();
+ this.awaitingAuthentication.set(connection, send);
+ connection.on('close', forget);
+ }
+
private async handleWebSocketMessage(
message: Message,
connection: WebSocketConnection,
@@ -271,6 +308,9 @@ export class LoxoneHttpService {
try {
const response = await this.processor.execute(command);
connection.sendUTF(response);
+ if (command.startsWith('secure/authenticate/')) {
+ this.awaitingAuthentication.get(connection)?.();
+ }
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
this.log.error('ws handler error', { name: definition.name, msg });
Tests in a new tests/loxoneConnectZoneSnapshot.test.ts: the app gets the zones right after the answer to secure/authenticate, once; without it, after the fallback; a connection closed first gets nothing and leaves no timer; the Miniserver still gets them on connect. loxoneConnectGroupSnapshot.test.ts passes unchanged; full suite green.
With the change, a probe connection to the app port gets only the identification until it authenticates, then the zones' state, and the tile is right as soon as the app starts. Not covered: a health change while the app stays open would still wait for an event that differs; not seen so far.
Environment
sonn core in Docker on dev (0fd45d23), a Loxone Miniserver, the Loxone app on a phone (observed) and its macOS build 17.1.1 (read).
Thanks, your reading of the app was right, I checked it against the app code here. It's on dev (517a427), along your lines and with you as co-author: an app connection gets the zones' state once its secure/authenticate is answered, or after 5 s without one, and the Miniserver still gets it on connect.