refactor(devices): store hardware identity as hw_family / hw_revision
The firmware moved its NVS identity keys from hw_type / hw_version to
hw_family / hw_revision (NvsMigration.cpp), and the NVS generator, cloudflash
and the device models already used the new names. Firestore device docs and
the manufacturing code still wrote and read hw_type / hw_version, so the
device API's hw_family / hw_revision were always "" (0 of 14 docs had them)
and the console used two names for one value.
Device docs now use the firmware's names (schema v1.1.0, decision D2 in the
bellsystems-schema repo). The apps don't read these fields, so no app change.
- Manufacturing batch-create writes hw_family / hw_revision; the inventory
API exposes hw_family / hw_revision (filters: ?hw_family= / ?hw_families=).
Reads fall back to hw_type / hw_version so docs not yet migrated still work.
- devices/firestore_v1.py renames hw_type / hw_version on read and write and
rejects a hw_family that isn't a code (e.g. "Bell Plus"): when the console
generates NVS from it, the firmware builds its OTA URL from that value.
- Inventory, provisioning, onboarding and overview pages use the new names.
Unchanged on purpose: the firmware catalog and flash assets
(/api/firmware/{hw_type}/..., hw_type_override) — a separate store and the
URL devices call; same values, only the variable name differs. The public
cloudflash response keeps its hw_type field (public contract).
Data migration (rename + fix 3 display-label values) runs from the
bellsystems-schema repo AFTER this is deployed: the currently deployed
console still reads hw_type.
Verified: 118 backend tests pass; frontend builds; ESLint errors in
ProvisioningWizard.jsx (7) are pre-existing and unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,7 +39,8 @@ IP_KEYS = {"ipAddress", "gateway", "subnet", "dns1", "dns2"}
|
||||
|
||||
# Misspelled key -> v1 key, per nested map. A value of None means "drop".
|
||||
LEGACY_KEYS: dict[str, dict[str, str | None]] = {
|
||||
"": {"users_list": None},
|
||||
# hw_type / hw_version: pre-2026-11-08 names; the firmware NVS uses hw_family / hw_revision.
|
||||
"": {"users_list": None, "hw_type": "hw_family", "hw_version": "hw_revision"},
|
||||
"device_attributes": {"bellGuardSafetyOn": "bellguardSafetyOn"},
|
||||
"device_attributes.networkSettings": {"useStaticIP": "usesStaticIP"},
|
||||
}
|
||||
@@ -51,6 +52,10 @@ _MELODY_ITEM_FORBIDDEN = {"information", "default_settings", "url"}
|
||||
# Still accepted on write so a browser tab opened before the deploy can't corrupt data.
|
||||
_LEGACY_DISPLAY_FMT = "%d %B %Y at %H:%M:%S UTC%z"
|
||||
|
||||
# Board family code: a BoardType value ("vesper_plus") or a bespoke UID. Never a display
|
||||
# label like "Bell Plus" — the firmware builds its OTA URL from this value.
|
||||
_HW_FAMILY = re.compile(r"^[a-z0-9][a-z0-9_-]*$")
|
||||
|
||||
_IPV4 = re.compile(r"^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$")
|
||||
|
||||
|
||||
@@ -163,6 +168,10 @@ def to_firestore(data: dict, db=None) -> dict:
|
||||
if sn or did:
|
||||
out["serial_number"] = out["device_id"] = sn or did
|
||||
|
||||
fam = out.get("hw_family")
|
||||
if fam not in (None, "") and not (isinstance(fam, str) and _HW_FAMILY.match(fam)):
|
||||
raise ValidationError(f"hw_family: expected a board family code like \"vesper_plus\", got {fam!r}")
|
||||
|
||||
if "user_list" in out and db is not None:
|
||||
refs, seen = [], set()
|
||||
for e in out["user_list"] or []:
|
||||
|
||||
@@ -89,8 +89,8 @@ class DeviceUserSummary(BaseModel):
|
||||
class DeviceInventoryItem(BaseModel):
|
||||
id: str
|
||||
serial_number: str
|
||||
hw_type: str
|
||||
hw_version: str
|
||||
hw_family: str # board family code (BoardType value or bespoke UID) = firmware NVS "hw_family"
|
||||
hw_revision: str # hardware revision, e.g. "1.0" = firmware NVS "hw_revision"
|
||||
mfg_status: str
|
||||
mfg_batch_id: Optional[str] = None
|
||||
created_at: Optional[str] = None
|
||||
@@ -134,7 +134,7 @@ class CustomerSearchResult(BaseModel):
|
||||
|
||||
class RecentActivityItem(BaseModel):
|
||||
serial_number: str
|
||||
hw_type: str
|
||||
hw_family: str
|
||||
mfg_status: str
|
||||
owner: Optional[str] = None
|
||||
updated_at: Optional[str] = None
|
||||
|
||||
@@ -71,8 +71,8 @@ async def create_batch(
|
||||
@router.get("/devices", response_model=DeviceInventoryListResponse)
|
||||
def list_devices(
|
||||
status: Optional[str] = Query(None),
|
||||
hw_type: Optional[str] = Query(None),
|
||||
hw_types: Optional[list[str]] = Query(None, description="Multi-select board type filter — repeat param, e.g. ?hw_types=vesper&hw_types=chronos"),
|
||||
hw_family: Optional[str] = Query(None),
|
||||
hw_families: Optional[list[str]] = Query(None, description="Multi-select board family filter — repeat param, e.g. ?hw_families=vesper&hw_families=chronos"),
|
||||
has_users: Optional[bool] = Query(None, description="Filter by whether the device has any app users assigned"),
|
||||
search: Optional[str] = Query(None),
|
||||
limit: int = Query(100, ge=1, le=500),
|
||||
@@ -81,8 +81,8 @@ def list_devices(
|
||||
):
|
||||
items = service.list_devices(
|
||||
status=status,
|
||||
hw_type=hw_type,
|
||||
hw_types=hw_types,
|
||||
hw_family=hw_family,
|
||||
hw_families=hw_families,
|
||||
has_users=has_users,
|
||||
search=search,
|
||||
limit=limit,
|
||||
@@ -605,7 +605,7 @@ def download_bootloader(
|
||||
):
|
||||
"""Return the bootloader.bin for this device's hw_type (flashed at 0x1000)."""
|
||||
item = service.get_device_by_sn(sn)
|
||||
hw_type = hw_type_override or item.hw_type
|
||||
hw_type = hw_type_override or item.hw_family
|
||||
try:
|
||||
data = service.get_flash_asset(hw_type, "bootloader.bin")
|
||||
except NotFoundError as e:
|
||||
@@ -625,7 +625,7 @@ def download_partitions(
|
||||
):
|
||||
"""Return the partitions.bin for this device's hw_type (flashed at 0x8000)."""
|
||||
item = service.get_device_by_sn(sn)
|
||||
hw_type = hw_type_override or item.hw_type
|
||||
hw_type = hw_type_override or item.hw_family
|
||||
try:
|
||||
data = service.get_flash_asset(hw_type, "partitions.bin")
|
||||
except NotFoundError as e:
|
||||
|
||||
@@ -66,8 +66,9 @@ def _doc_to_inventory_item(doc, customer_names: dict | None = None, user_infos:
|
||||
return DeviceInventoryItem(
|
||||
id=doc.id,
|
||||
serial_number=data.get("serial_number", ""),
|
||||
hw_type=data.get("hw_type", ""),
|
||||
hw_version=data.get("hw_version", ""),
|
||||
# hw_type / hw_version: names used before 2026-11-08 (kept as read fallback)
|
||||
hw_family=data.get("hw_family") or data.get("hw_type", ""),
|
||||
hw_revision=data.get("hw_revision") or data.get("hw_version", ""),
|
||||
mfg_status=data.get("mfg_status", "manufactured"),
|
||||
mfg_batch_id=data.get("mfg_batch_id"),
|
||||
created_at=created_str,
|
||||
@@ -136,8 +137,9 @@ def create_batch(data: BatchCreate) -> BatchResponse:
|
||||
"serial_number": sn,
|
||||
"device_id": sn, # schema v1 / D1: both apps connect over MQTT with device_id
|
||||
|
||||
"hw_type": data.board_type.value,
|
||||
"hw_version": data.board_version,
|
||||
# Same names as the firmware NVS keys.
|
||||
"hw_family": data.board_type.value,
|
||||
"hw_revision": data.board_version,
|
||||
"mfg_status": "manufactured",
|
||||
"mfg_batch_id": batch_id,
|
||||
"created_at": now,
|
||||
@@ -170,8 +172,8 @@ def create_batch(data: BatchCreate) -> BatchResponse:
|
||||
|
||||
def list_devices(
|
||||
status: str | None = None,
|
||||
hw_type: str | None = None,
|
||||
hw_types: list[str] | None = None,
|
||||
hw_family: str | None = None,
|
||||
hw_families: list[str] | None = None,
|
||||
has_users: bool | None = None,
|
||||
search: str | None = None,
|
||||
limit: int = 100,
|
||||
@@ -182,15 +184,15 @@ def list_devices(
|
||||
|
||||
if status:
|
||||
query = query.where("mfg_status", "==", status)
|
||||
if hw_type:
|
||||
query = query.where("hw_type", "==", hw_type)
|
||||
if hw_family:
|
||||
query = query.where("hw_family", "==", hw_family)
|
||||
|
||||
docs = list(query.stream())
|
||||
docs_data = [doc.to_dict() or {} for doc in docs]
|
||||
|
||||
if hw_types:
|
||||
hw_type_set = set(hw_types)
|
||||
keep = [i for i, d in enumerate(docs_data) if d.get("hw_type") in hw_type_set]
|
||||
if hw_families:
|
||||
family_set = set(hw_families)
|
||||
keep = [i for i, d in enumerate(docs_data) if d.get("hw_family") in family_set]
|
||||
docs = [docs[i] for i in keep]
|
||||
docs_data = [docs_data[i] for i in keep]
|
||||
|
||||
@@ -274,8 +276,8 @@ def get_nvs_binary(sn: str, hw_type_override: str | None = None, hw_revision_ove
|
||||
item = get_device_by_sn(sn)
|
||||
return generate_nvs_binary(
|
||||
serial_number=item.serial_number,
|
||||
hw_family=hw_type_override if hw_type_override else item.hw_type,
|
||||
hw_revision=hw_revision_override if hw_revision_override else item.hw_version,
|
||||
hw_family=hw_type_override if hw_type_override else item.hw_family,
|
||||
hw_revision=hw_revision_override if hw_revision_override else item.hw_revision,
|
||||
legacy=legacy,
|
||||
)
|
||||
|
||||
@@ -333,7 +335,7 @@ def assign_device(sn: str, data: DeviceAssign) -> DeviceInventoryItem:
|
||||
for item in owned_items
|
||||
)
|
||||
if not already_assigned:
|
||||
device_name = doc_data.get("device_name") or BOARD_TYPE_LABELS.get(doc_data.get("hw_type", ""), sn)
|
||||
device_name = doc_data.get("device_name") or BOARD_TYPE_LABELS.get(doc_data.get("hw_family") or doc_data.get("hw_type", ""), sn)
|
||||
owned_items.append({
|
||||
"type": "console_device",
|
||||
"console_device": {
|
||||
@@ -403,7 +405,7 @@ def get_stats() -> ManufacturingStats:
|
||||
|
||||
activity_candidates.append(RecentActivityItem(
|
||||
serial_number=data.get("serial_number", ""),
|
||||
hw_type=data.get("hw_type", ""),
|
||||
hw_family=data.get("hw_family") or data.get("hw_type", ""),
|
||||
mfg_status=status,
|
||||
owner=data.get("owner"),
|
||||
updated_at=ts_str,
|
||||
@@ -563,7 +565,6 @@ def get_firmware_url(sn: str) -> str:
|
||||
"""Return the FastAPI download URL for the latest stable firmware for this device's hw_type."""
|
||||
from firmware.service import get_latest
|
||||
item = get_device_by_sn(sn)
|
||||
hw_type = item.hw_type.lower()
|
||||
latest = get_latest(hw_type, "stable")
|
||||
latest = get_latest(item.hw_family.lower(), "stable")
|
||||
# download_url is a relative path like /api/firmware/vs/stable/1.4.2/firmware.bin
|
||||
return latest.download_url
|
||||
|
||||
@@ -109,9 +109,9 @@ async def validate_serial(serial_number: str):
|
||||
device = get_device_by_sn(sn)
|
||||
return SerialValidationResult(
|
||||
valid=True,
|
||||
hw_type=device.hw_type,
|
||||
hw_type_label=HW_TYPE_LABELS.get(device.hw_type, device.hw_type.replace("_", " ").title()),
|
||||
hw_version=device.hw_version,
|
||||
hw_type=device.hw_family,
|
||||
hw_type_label=HW_TYPE_LABELS.get(device.hw_family, device.hw_family.replace("_", " ").title()),
|
||||
hw_version=device.hw_revision,
|
||||
)
|
||||
except Exception:
|
||||
return SerialValidationResult(valid=False)
|
||||
|
||||
@@ -104,6 +104,18 @@ def test_v1_key_wins_over_legacy_duplicate():
|
||||
assert attrs["networkSettings"] == {"usesStaticIP": True}
|
||||
|
||||
|
||||
def test_legacy_hw_names_become_firmware_names():
|
||||
out = v1.to_firestore({"hw_type": "vesper_plus", "hw_version": "1.0"})
|
||||
assert out == {"hw_family": "vesper_plus", "hw_revision": "1.0"}
|
||||
assert v1.to_api({"hw_type": "agnus", "hw_version": "1.0"}) == {"hw_family": "agnus", "hw_revision": "1.0"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("label", ["Bell Plus", "Bell Core", "vesper plus"])
|
||||
def test_hw_family_display_labels_are_rejected(label):
|
||||
with pytest.raises(ValidationError):
|
||||
v1.to_firestore({"hw_family": label})
|
||||
|
||||
|
||||
def test_retired_users_list_is_dropped():
|
||||
assert "users_list" not in v1.to_firestore({"users_list": [], "device_name": "x"})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user