feat: improve Symcon designer controls and KNX temperature selection
This commit is contained in:
1 parent
804f2d0494
commit
b934f78b9a
24 files changed
+908
-562
No files matched your search
@@ -192,8 +192,9 @@ export const tempUnit = (hass: HomeAssistant): "°C" | "°F" => (hass.config?.un
|
||||
|
||||
/** A reading in its unit (°F, K, anything else counts as °C) converted to °C. */
|
||||
export function toCelsius(value: number, unit: unknown): number {
|
||||
if (unit === "°F") return ((value - 32) * 5) / 9;
|
||||
if (unit === "K") return value - 273.15;
|
||||
const normalized = String(unit ?? "").replace(/\s/g, "").toLowerCase();
|
||||
if (normalized === "°f" || normalized === "℉") return ((value - 32) * 5) / 9;
|
||||
if (normalized === "k") return value - 273.15;
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,24 @@ import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { entity, floorWith, box, hassOf } from "./fixtures.ts";
|
||||
import { registerPlugin, resetPlugins } from "../plugins/registry.ts";
|
||||
import { areaEntities, entityAreaId, entityKind, groupByDevice, isHubDevice, isRelevant, isRoomClimateSensor, otherAreaEntities, powerSensorsOf, primaryEntities, roomClimateSensors, roomClimateValue, unassignedEntities } from "./registry.ts";
|
||||
import { areaEntities, entityAreaId, entityKind, groupByDevice, isClimateValueSensor, isHubDevice, isRelevant, isRoomClimateSensor, otherAreaEntities, powerSensorsOf, primaryEntities, roomClimateSensors, roomClimateValue, unassignedEntities } from "./registry.ts";
|
||||
|
||||
test("a KNX numeric temperature remains selectable and readable even with an inferred actuator domain", () => {
|
||||
const hass = hassOf([
|
||||
entity("climate.12345", "heat", { symcon_id: 12345, symcon_variable_type: 2, symcon_numeric_value: 21.75, unit_of_measurement: "°C" }),
|
||||
entity("sensor.23456", "22.5", { symcon_id: 23456, symcon_variable_type: 2, unit_of_measurement: " °C " }),
|
||||
entity("sensor.power", "12", { device_class: "power", unit_of_measurement: "W" }),
|
||||
entity("light.lamp", "on", { unit_of_measurement: "°C" }),
|
||||
entity("sensor.fahrenheit", "68", { unit_of_measurement: "℉" }),
|
||||
]);
|
||||
assert.equal(isClimateValueSensor(hass, "climate.12345", "temperature"), true);
|
||||
assert.equal(isClimateValueSensor(hass, "sensor.23456", "temperature"), true);
|
||||
assert.equal(isClimateValueSensor(hass, "sensor.power", "temperature"), false);
|
||||
assert.equal(isClimateValueSensor(hass, "light.lamp", "temperature"), false);
|
||||
const room = { ...box("room", 0, 0, 4, 4), climate: { temperature: "climate.12345" } };
|
||||
assert.equal(roomClimateValue(hass, null, room, "temperature"), 21.75);
|
||||
assert.equal(roomClimateValue(hass, null, { ...room, climate: { temperature: "sensor.fahrenheit" } }, "temperature"), 20);
|
||||
});
|
||||
|
||||
const lounge = hassOf(
|
||||
[
|
||||
|
||||
@@ -209,6 +209,17 @@ export const primaryEntities = (hass: HomeAssistant, ids: readonly string[]): st
|
||||
export const CLIMATE_CLASSES = { temperature: "temperature", humidity: "humidity", co2: "carbon_dioxide" } as const;
|
||||
export type ClimateKey = keyof typeof CLIMATE_CLASSES;
|
||||
|
||||
/** Numeric room values, including Symcon readings whose legacy domain was inferred from a parent name. */
|
||||
export function isClimateValueSensor(hass: HomeAssistant, id: string, key: ClimateKey): boolean {
|
||||
const a = hass.states[id]?.attributes;
|
||||
if (!a) return false;
|
||||
const symconNumber = (a.symcon_variable_type === 1 || a.symcon_variable_type === 2) && typeof a.symcon_id === "number";
|
||||
if (!symconNumber && !/^(sensor|input_number|number)\./.test(id)) return false;
|
||||
const unit = String(a.unit_of_measurement ?? "").replace(/\s/g, "").toLowerCase();
|
||||
if (key === "temperature" && ["°c", "°f", "℃", "℉", "k"].includes(unit)) return true;
|
||||
return a.device_class === CLIMATE_CLASSES[key];
|
||||
}
|
||||
|
||||
/** A device with one of these domains is not a room sensor: a heat pump, a printer, a TV. */
|
||||
const APPLIANCE_DOMAINS = new Set(["climate", "water_heater", "switch", "button", "camera", "media_player", "vacuum", "light", "fan", "lawn_mower"]);
|
||||
/** Words in a name (English and German) that mark a temperature that is not the room's: pipes, printers, chips, outdoors, cold appliances. */
|
||||
@@ -245,14 +256,15 @@ export function roomClimateSensors(hass: HomeAssistant, floor: Floor | null, roo
|
||||
const inside = placedSensors.filter((p) => pointInPolygon([p.x, p.z], room.points)).map((p) => p.entity_id);
|
||||
const insideElsewhere = new Set(placedSensors.filter((p) => !pointInPolygon([p.x, p.z], room.points)).map((p) => p.entity_id));
|
||||
const candidates = new Set([...roomEntities(hass, room).filter((id) => !insideElsewhere.has(id)), ...inside]);
|
||||
return [...candidates].filter((id) => id.startsWith("sensor.") && hass.states[id]?.attributes.device_class === CLIMATE_CLASSES[key] && isRoomClimateSensor(hass, id));
|
||||
return [...candidates].filter((id) => id.startsWith("sensor.") && isClimateValueSensor(hass, id, key) && isRoomClimateSensor(hass, id));
|
||||
}
|
||||
|
||||
/** The average of the room's sensors, or null when none reports. Temperatures are always °C, whatever the sensor's unit. */
|
||||
export function roomClimateValue(hass: HomeAssistant, floor: Floor | null, room: Room, key: ClimateKey): number | null {
|
||||
const readings = roomClimateSensors(hass, floor, room, key)
|
||||
.map((id) => {
|
||||
const reading = Number(hass.states[id]?.state);
|
||||
const state = hass.states[id];
|
||||
const reading = Number(state?.attributes.symcon_numeric_value ?? state?.state);
|
||||
return key === "temperature" ? toCelsius(reading, hass.states[id]?.attributes.unit_of_measurement) : reading;
|
||||
})
|
||||
.filter(Number.isFinite);
|
||||
|
||||
Reference in new issue
Block a user