Skip to content

Commit

Permalink
Add tooltips for serial numbers (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus authored Nov 10, 2024
1 parent f776879 commit b6bfaef
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/helpers/hardware_types.ts → src/helpers/module_properties.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
export interface LcnSerial {
year: number;
month: number;
day: number;
serial?: number;
}

const serialRegExp =
/(?<year>[A-F0-9]{2}).(?<month>[A-F0-9])(?<day>[A-F0-9]{2})(?<serial>[A-F0-9]{4})?/;

export function parseSerialNumber(serial_number: number): LcnSerial {
const result = serialRegExp.exec(serial_number.toString(16).toUpperCase());
if (!result) throw new Error("Wrong serial number");

const is_software_serial = result![4] === undefined;

return {
year: Number("0x" + result![1]) + 1990,
month: Number("0x" + result![2]),
day: Number("0x" + result![3]),
serial: is_software_serial ? undefined : Number("0x" + result![4]),
};
}

export function getHardwareType(hardwareId: number): string | undefined {
switch (hardwareId) {
case 1:
Expand Down
52 changes: 46 additions & 6 deletions src/lcn-devices-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { consume } from "@lit-labs/context";
import { deviceConfigsContext } from "components/context";
import { haStyle } from "@ha/resources/styles";
import "@lrnwebcomponents/simple-tooltip/simple-tooltip";
import "@material/mwc-button";
import "@ha/components/ha-clickable-list-item";
import "@ha/components/ha-fab";
Expand All @@ -9,7 +10,6 @@ import "@ha/components/ha-list-item";
import "@ha/components/ha-md-menu-item";
import "@ha/components/ha-help-tooltip";
import "@ha/components/ha-icon-button";
import "@ha/components/ha-switch";
import "@ha/components/ha-checkbox";
import "@ha/components/ha-formfield";
import { stopPropagation } from "@ha/common/dom/stop_propagation";
Expand All @@ -36,7 +36,7 @@ import { navigate } from "@ha/common/navigate";
import type { HASSDomEvent } from "@ha/common/dom/fire_event";
import { updateDeviceConfigs, updateEntityConfigs } from "components/events";
import { renderBrandLogo } from "helpers/brand_logo";
import { getHardwareType } from "helpers/hardware_types";
import { getHardwareType, parseSerialNumber, LcnSerial } from "helpers/module_properties";
import { ProgressDialog } from "./dialogs/progress-dialog";
import {
loadLCNCreateDeviceDialog,
Expand Down Expand Up @@ -162,16 +162,14 @@ export class LCNConfigDashboard extends LitElement {
sortable: true,
filterable: true,
defaultHidden: true,
template: (entry) =>
entry.hardware_serial !== -1 ? entry.hardware_serial.toString(16).toUpperCase() : "-",
template: (entry) => this.renderHardwareSerial(entry.hardware_serial),
},
software_serial: {
title: this.lcn.localize("software-serial"),
sortable: true,
filterable: true,
defaultHidden: true,
template: (entry) =>
entry.software_serial !== -1 ? entry.software_serial.toString(16).toUpperCase() : "-",
template: (entry) => this.renderSoftwareSerial(entry.software_serial),
},
hardware_type: {
title: this.lcn.localize("hardware-type"),
Expand Down Expand Up @@ -213,6 +211,48 @@ export class LCNConfigDashboard extends LitElement {
this._dataTable.then(renderBrandLogo);
}

protected renderSoftwareSerial(software_serial: number) {
let serial: LcnSerial;
try {
serial = parseSerialNumber(software_serial);
} catch (error) {
return html`-`;
}

return html`
${software_serial.toString(16).toUpperCase()}
<simple-tooltip animation-delay="0">
${this.lcn.localize("firmware-date", {
year: serial.year,
month: serial.month,
day: serial.day,
})}
</simple-tooltip>
`;
}

protected renderHardwareSerial(hardware_serial: number) {
let serial: LcnSerial;
try {
serial = parseSerialNumber(hardware_serial);
} catch (error) {
return html`-`;
}

return html`
${hardware_serial.toString(16).toUpperCase()}
<simple-tooltip animation-delay="0">
${this.lcn.localize("hardware-date", {
year: serial.year,
month: serial.month,
day: serial.day,
})}
<br />
${this.lcn.localize("hardware-number", { serial: serial.serial })}
</simple-tooltip>
`;
}

protected render() {
if (!(this.hass && this.lcn && this._deviceConfigs)) {
return nothing;
Expand Down
4 changes: 4 additions & 0 deletions src/localize/languages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"addresses": "Adressen",
"modulesgroups": "Module / Gruppen",

"firmware-date": "Firmware Datum: {day}.{month}.{year}",
"hardware-date": "Modul Datum: {day}.{month}.{year}",
"hardware-number": "Nummer: {serial}",

"domain": "Domäne",
"binary-sensor": "Binärsensor",
"climate": "Klima",
Expand Down
4 changes: 4 additions & 0 deletions src/localize/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"addresses": "Addresses",
"modulesgroups": "Modules / Groups",

"firmware-date": "Firmware date: {month}/{day}/{year}",
"hardware-date": "Module date: {month}/{day}/{year}",
"hardware-number": "Module number: {serial}",

"domain": "Domain",
"binary-sensor": "Binary sensor",
"climate": "Climate",
Expand Down

0 comments on commit b6bfaef

Please sign in to comment.