// Home Assistant Controls
// Purpose: Defines the Home Assistant Controls module and the local helpers/components used in this file.
// Scope: Keeps behavior unchanged while isolating this concern into a clear, single-responsibility unit.
import { useMemo } from 'react';
import { useSessionActions, useSessionSelector } from '../../context/SessionContext.jsx';
import { useControlSystem } from '../../controls/index.js';
import { formatKeyLabel } from '../../controls/keymapUtils.js';
import CardFrame from '../CardFrame/index.jsx';
const COLOR_SWATCHES = Object.freeze([
{ id: 'white', label: 'White', hex: '#ffffff', action: 'white' },
{ id: 'red', label: 'Red', hex: '#ff0000' },
{ id: 'green', label: 'Green', hex: '#00ff00' },
{ id: 'cyan', label: 'Cyan', hex: '#00c7be' },
{ id: 'blue', label: 'Blue', hex: '#0000ff' },
{ id: 'purple', label: 'Purple', hex: '#bf5af2' },
]);
function StatusBadge({ label, tone = 'muted' }) {
const styles =
tone === 'success'
? 'bg-emerald-900 text-emerald-100'
: tone === 'warn'
? 'bg-amber-900 text-amber-100'
: 'bg-slate-800 text-slate-200';
return (
{label}
);
}
function cx(...values) {
return values.filter(Boolean).join(' ');
}
function buildTileStyle(entity, { unavailable, isOn, supportsColor }) {
if (unavailable) return undefined;
if (!isOn) return undefined;
const colorHex = supportsColor ? entity.colorHex : null;
if (!colorHex) return undefined;
// The server owns Home Assistant color-format conversion and exposes a CSS
// hex string. React only uses it as a display value here, which keeps browser
// rendering decoupled from HA-specific rgb/hs attribute shapes.
return {
backgroundColor: colorHex,
borderColor: 'rgba(255,255,255,0.38)',
};
}
function LampSwatch({ entity, swatch, disabled, onSetColor, onSetWhite }) {
const handleClick = (event) => {
// Swatches live inside a tile that toggles the lamp when clicked. Stop the
// event here so choosing a color is a single, unambiguous action instead of
// setting color and then also toggling the lamp off.
event.stopPropagation();
if (disabled) return;
// White gets its own server action because Home Assistant white/temperature
// mode is not the same as sending pure RGB white on many smart bulbs.
if (swatch.action === 'white') {
onSetWhite(entity.id).catch(() => {});
return;
}
onSetColor(entity.id, swatch.hex).catch(() => {});
};
return (