fix(devices): write devices in Firestore schema v1 so the apps can read them

The FlutterFlow tablet and phone apps parse each device doc inside safeGet:
one wrongly typed field makes them silently drop the whole device from the
user's list. The console was the source of every such field found in
production on 2026-11-08:

- New devices: DeviceCreate defaulted times to "" and IPs to [], and
  create_device wrote them as-is. Every console-created device was invisible
  in the apps from birth. It also set device_id but not serial_number.
- Round trip: reads formatted Timestamps as "15 January 2026 at 22:01:00
  UTC+0000", the frontend echoed that back on save, and _restore_timestamps
  could not parse it (nor ""), so it stored the string. towerClockTime was
  not in its list at all.
- Key spelling: the console wrote bellGuardSafetyOn / useStaticIP; the apps
  read bellguardSafetyOn / usesStaticIP, so console edits never reached them
  and docs ended up with both spellings disagreeing.
- MelodyMainItem used the melodies-collection shape (information, url, ...)
  instead of the device shape the apps read (mainInformation, binaryUrl, ...);
  Pydantic dropped every real key on read.
- Manufacturing batches created devices with serial_number but no device_id,
  which both apps use to connect over MQTT (7 of 14 devices affected).

The contract now lives in the bellsystems-schema repo (v1 = what the shipped
apps read). backend/devices/firestore_v1.py enforces it on every device
write (to_firestore) and read (to_api):
- Timestamps: ISO 8601 in the API, Firestore Timestamps in the DB, null for
  unset. The old display format is still accepted on write so an open
  browser tab can't corrupt data; anything unparseable is a 422.
- IPs: dotted strings; octet arrays are joined; invalid values are a 422.
- Legacy keys are renamed (v1 key wins over a duplicate); users_list dropped.
- device_id mirrors serial_number (decision D1); conflicting values are a 422.
- user_list / melodyReference entries become DocumentReferences.
- update_device re-normalizes the merged nested maps, so saving a device
  also repairs pre-v1 junk already stored in it.

Models and the frontend now use v1 names and send null (not "") for unset
times. The firmware's network.set_config MQTT payload keeps its own names.

Verified: 114 backend tests pass (new tests/test_firestore_v1.py covers the
production failure shapes; test_timezones.py not run locally, needs tzdata).
A read-only dry run of a console save against all 14 production devices
produced v1-valid docs with no fields lost. Frontend builds. Not deployed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 20:00:33 +02:00
co-authored by Claude Opus 5.5
parent 2a6f9697b8
commit 62be80c9d8
12 changed files with 511 additions and 122 deletions
@@ -20,7 +20,7 @@ function extractTime(tsStr) {
}
function timeToIso(hhmm) {
if (!hhmm) return ''
if (!hhmm) return null
return `2000-01-01T${hhmm}:00Z`
}
@@ -183,11 +183,11 @@ export default function EditAlertsModal({ open, onClose, onSaved, clock, attr, i
halfhourAlertsBell: Number(halfBell),
quarterAlertsBell: Number(quarterBell),
isDaySilenceOn: isDayOn,
daySilenceFrom: isDayOn && daySilenceFrom ? timeToIso(daySilenceFrom) : (clock?.daySilenceFrom || ''),
daySilenceTo: isDayOn && daySilenceTo ? timeToIso(daySilenceTo) : (clock?.daySilenceTo || ''),
daySilenceFrom: isDayOn && daySilenceFrom ? timeToIso(daySilenceFrom) : (clock?.daySilenceFrom ?? null),
daySilenceTo: isDayOn && daySilenceTo ? timeToIso(daySilenceTo) : (clock?.daySilenceTo ?? null),
isNightSilenceOn: isNightOn,
nightSilenceFrom: isNightOn && nightFrom ? timeToIso(nightFrom) : (clock?.nightSilenceFrom || ''),
nightSilenceTo: isNightOn && nightTo ? timeToIso(nightTo) : (clock?.nightSilenceTo || ''),
nightSilenceFrom: isNightOn && nightFrom ? timeToIso(nightFrom) : (clock?.nightSilenceFrom ?? null),
nightSilenceTo: isNightOn && nightTo ? timeToIso(nightTo) : (clock?.nightSilenceTo ?? null),
},
},
})
@@ -9,7 +9,7 @@ const toBool = (v) => v === 'true' || v === true
export default function EditAttributesModal({ open, onClose, onSaved, attr, id }) {
const [bellGuardOn, setBellGuardOn] = useState(String(attr?.bellGuardOn ?? false))
const [bellGuardSafetyOn, setBellGuardSafetyOn] = useState(String(attr?.bellGuardSafetyOn ?? false))
const [bellGuardSafetyOn, setBellGuardSafetyOn] = useState(String(attr?.bellguardSafetyOn ?? false))
const [warningsOn, setWarningsOn] = useState(String(attr?.warningsOn ?? false))
const [saving, setSaving] = useState(false)
const [error, setError] = useState('')
@@ -23,7 +23,7 @@ export default function EditAttributesModal({ open, onClose, onSaved, attr, id }
device_attributes: {
bellGuardOn: toBool(bellGuardOn),
warningsOn: toBool(warningsOn),
bellGuardSafetyOn: toBool(bellGuardSafetyOn),
bellguardSafetyOn: toBool(bellGuardSafetyOn),
},
})
await onSaved()
@@ -17,7 +17,7 @@ function extractTime(tsStr) {
}
function timeToIso(hhmm) {
if (!hhmm) return ''
if (!hhmm) return null
return `2000-01-01T${hhmm}:00Z`
}
@@ -62,8 +62,8 @@ export default function EditBacklightModal({ open, onClose, onSaved, attr, clock
...clock,
isBacklightAutomationOn: enabled,
backlightOutput: Number(output),
backlightTurnOnTime: enabled && onTime ? timeToIso(onTime) : '',
backlightTurnOffTime: enabled && offTime ? timeToIso(offTime) : '',
backlightTurnOnTime: enabled && onTime ? timeToIso(onTime) : null,
backlightTurnOffTime: enabled && offTime ? timeToIso(offTime) : null,
},
},
})
@@ -91,7 +91,7 @@ function deepMerge(base, overrides) {
}
function timeToIso(hhmm) {
if (!hhmm) return ''
if (!hhmm) return null
return `2000-01-01T${hhmm}:00Z`
}
function extractTime(tsStr) {
@@ -1353,11 +1353,11 @@ function ConfigureStep({ device, onDeviceUpdated, onNext, onBack }) {
halfhourAlertsBell: Number(alert.halfBell),
quarterAlertsBell: Number(alert.quarterBell),
isDaySilenceOn: alert.isDayOn,
daySilenceFrom: alert.isDayOn && alert.dayFrom ? timeToIso(alert.dayFrom) : (clock.daySilenceFrom || ''),
daySilenceTo: alert.isDayOn && alert.dayTo ? timeToIso(alert.dayTo) : (clock.daySilenceTo || ''),
daySilenceFrom: alert.isDayOn && alert.dayFrom ? timeToIso(alert.dayFrom) : (clock.daySilenceFrom ?? null),
daySilenceTo: alert.isDayOn && alert.dayTo ? timeToIso(alert.dayTo) : (clock.daySilenceTo ?? null),
isNightSilenceOn: alert.isNightOn,
nightSilenceFrom: alert.isNightOn && alert.nightFrom ? timeToIso(alert.nightFrom) : (clock.nightSilenceFrom || ''),
nightSilenceTo: alert.isNightOn && alert.nightTo ? timeToIso(alert.nightTo) : (clock.nightSilenceTo || ''),
nightSilenceFrom: alert.isNightOn && alert.nightFrom ? timeToIso(alert.nightFrom) : (clock.nightSilenceFrom ?? null),
nightSilenceTo: alert.isNightOn && alert.nightTo ? timeToIso(alert.nightTo) : (clock.nightSilenceTo ?? null),
},
},
},
@@ -1377,8 +1377,8 @@ function ConfigureStep({ device, onDeviceUpdated, onNext, onBack }) {
clockSettings: {
isBacklightAutomationOn: backlight.enabled,
backlightOutput: Number(backlight.output),
backlightTurnOnTime: backlight.enabled && backlight.onTime ? timeToIso(backlight.onTime) : '',
backlightTurnOffTime: backlight.enabled && backlight.offTime ? timeToIso(backlight.offTime) : '',
backlightTurnOnTime: backlight.enabled && backlight.onTime ? timeToIso(backlight.onTime) : null,
backlightTurnOffTime: backlight.enabled && backlight.offTime ? timeToIso(backlight.offTime) : null,
},
},
},
@@ -388,15 +388,15 @@ export default function ClockTab({
halfhourAlertsBell: trueHalfBell,
quarterAlertsBell: trueQuarterBell,
isDaySilenceOn: !!data.daytime_silence,
daySilenceFrom: data.daytime_on ? `2000-01-01T${data.daytime_on}:00Z` : (clock?.daySilenceFrom || ''),
daySilenceTo: data.daytime_off ? `2000-01-01T${data.daytime_off}:00Z` : (clock?.daySilenceTo || ''),
daySilenceFrom: data.daytime_on ? `2000-01-01T${data.daytime_on}:00Z` : (clock?.daySilenceFrom ?? null),
daySilenceTo: data.daytime_off ? `2000-01-01T${data.daytime_off}:00Z` : (clock?.daySilenceTo ?? null),
isNightSilenceOn: !!data.night_silence,
nightSilenceFrom: data.night_on ? `2000-01-01T${data.night_on}:00Z` : (clock?.nightSilenceFrom || ''),
nightSilenceTo: data.night_off ? `2000-01-01T${data.night_off}:00Z` : (clock?.nightSilenceTo || ''),
nightSilenceFrom: data.night_on ? `2000-01-01T${data.night_on}:00Z` : (clock?.nightSilenceFrom ?? null),
nightSilenceTo: data.night_off ? `2000-01-01T${data.night_off}:00Z` : (clock?.nightSilenceTo ?? null),
isBacklightAutomationOn: !!data.backlight,
backlightOutput: trueBacklightOutput,
backlightTurnOnTime: data.backlight_on ? `2000-01-01T${data.backlight_on}:00Z` : '',
backlightTurnOffTime: data.backlight_off ? `2000-01-01T${data.backlight_off}:00Z` : '',
backlightTurnOnTime: data.backlight_on ? `2000-01-01T${data.backlight_on}:00Z` : null,
backlightTurnOffTime: data.backlight_off ? `2000-01-01T${data.backlight_off}:00Z` : null,
faceSyncMode: data.face_sync_mode ?? clock?.faceSyncMode ?? 'force',
smartMaxWaitMin: data.smart_max_wait_min ?? clock?.smartMaxWaitMin ?? 61,
},
@@ -1170,7 +1170,7 @@ export default function GeneralTab({
<div style={{ display: 'flex', flexDirection: 'column', gap: 'var(--space-2)', flex: 1, justifyContent: 'center' }}>
<SafetyRow label="Bell Guard" description="Automated striking lock" enabled={attr?.bellGuardOn} />
<SafetyRow label="Warnings" description="Audible system alerts" enabled={attr?.warningsOn} />
<SafetyRow label="Safety" description="Manual override lock" enabled={attr?.bellGuardSafetyOn} />
<SafetyRow label="Safety" description="Manual override lock" enabled={attr?.bellguardSafetyOn} />
</div>
</GlassCard>
</div>