mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Add support for capability attributes in demo (#21263)
This commit is contained in:
parent
d833910796
commit
2b5fba4a30
@ -10,6 +10,18 @@ const now = () => new Date().toISOString();
|
|||||||
const randomTime = () =>
|
const randomTime = () =>
|
||||||
new Date(new Date().getTime() - Math.random() * 80 * 60 * 1000).toISOString();
|
new Date(new Date().getTime() - Math.random() * 80 * 60 * 1000).toISOString();
|
||||||
|
|
||||||
|
const CAPABILITY_ATTRIBUTES = [
|
||||||
|
"friendly_name",
|
||||||
|
"unit_of_measurement",
|
||||||
|
"icon",
|
||||||
|
"entity_picture",
|
||||||
|
"supported_features",
|
||||||
|
"hidden",
|
||||||
|
"assumed_state",
|
||||||
|
"device_class",
|
||||||
|
"state_class",
|
||||||
|
"restored",
|
||||||
|
];
|
||||||
export class Entity {
|
export class Entity {
|
||||||
public domain: string;
|
public domain: string;
|
||||||
|
|
||||||
@ -29,16 +41,28 @@ export class Entity {
|
|||||||
|
|
||||||
public hass?: any;
|
public hass?: any;
|
||||||
|
|
||||||
constructor(domain, objectId, state, baseAttributes) {
|
static CAPABILITY_ATTRIBUTES = new Set(CAPABILITY_ATTRIBUTES);
|
||||||
|
|
||||||
|
constructor(domain, objectId, state, attributes) {
|
||||||
this.domain = domain;
|
this.domain = domain;
|
||||||
this.objectId = objectId;
|
this.objectId = objectId;
|
||||||
this.entityId = `${domain}.${objectId}`;
|
this.entityId = `${domain}.${objectId}`;
|
||||||
this.lastChanged = randomTime();
|
this.lastChanged = randomTime();
|
||||||
this.lastUpdated = randomTime();
|
this.lastUpdated = randomTime();
|
||||||
this.state = String(state);
|
this.state = String(state);
|
||||||
|
|
||||||
// These are the attributes that we always write to the state machine
|
// These are the attributes that we always write to the state machine
|
||||||
|
const baseAttributes = {};
|
||||||
|
const capabilityAttributes =
|
||||||
|
TYPES[domain]?.CAPABILITY_ATTRIBUTES || Entity.CAPABILITY_ATTRIBUTES;
|
||||||
|
for (const key of Object.keys(attributes)) {
|
||||||
|
if (capabilityAttributes.has(key)) {
|
||||||
|
baseAttributes[key] = attributes[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.baseAttributes = baseAttributes;
|
this.baseAttributes = baseAttributes;
|
||||||
this.attributes = baseAttributes;
|
this.attributes = attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async handleService(domain, service, data: Record<string, any>) {
|
public async handleService(domain, service, data: Record<string, any>) {
|
||||||
@ -54,7 +78,7 @@ export class Entity {
|
|||||||
this.lastUpdated = now();
|
this.lastUpdated = now();
|
||||||
this.lastChanged =
|
this.lastChanged =
|
||||||
state === this.state ? this.lastChanged : this.lastUpdated;
|
state === this.state ? this.lastChanged : this.lastUpdated;
|
||||||
this.attributes = { ...this.baseAttributes, ...attributes };
|
this.attributes = { ...this.attributes, ...attributes };
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
console.log("update", this.entityId, this);
|
console.log("update", this.entityId, this);
|
||||||
@ -68,7 +92,7 @@ export class Entity {
|
|||||||
return {
|
return {
|
||||||
entity_id: this.entityId,
|
entity_id: this.entityId,
|
||||||
state: this.state,
|
state: this.state,
|
||||||
attributes: this.attributes,
|
attributes: this.state === "off" ? this.baseAttributes : this.attributes,
|
||||||
last_changed: this.lastChanged,
|
last_changed: this.lastChanged,
|
||||||
last_updated: this.lastUpdated,
|
last_updated: this.lastUpdated,
|
||||||
};
|
};
|
||||||
@ -76,6 +100,16 @@ export class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class LightEntity extends Entity {
|
class LightEntity extends Entity {
|
||||||
|
static CAPABILITY_ATTRIBUTES = new Set([
|
||||||
|
...CAPABILITY_ATTRIBUTES,
|
||||||
|
"min_color_temp_kelvin",
|
||||||
|
"max_color_temp_kelvin",
|
||||||
|
"min_mireds",
|
||||||
|
"max_mireds",
|
||||||
|
"effect_list",
|
||||||
|
"supported_color_modes",
|
||||||
|
]);
|
||||||
|
|
||||||
public async handleService(domain, service, data) {
|
public async handleService(domain, service, data) {
|
||||||
if (!["homeassistant", this.domain].includes(domain)) {
|
if (!["homeassistant", this.domain].includes(domain)) {
|
||||||
return;
|
return;
|
||||||
@ -188,6 +222,12 @@ class AlarmControlPanelEntity extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MediaPlayerEntity extends Entity {
|
class MediaPlayerEntity extends Entity {
|
||||||
|
static CAPABILITY_ATTRIBUTES = new Set([
|
||||||
|
...CAPABILITY_ATTRIBUTES,
|
||||||
|
"source_list",
|
||||||
|
"sound_mode_list",
|
||||||
|
]);
|
||||||
|
|
||||||
public async handleService(
|
public async handleService(
|
||||||
domain,
|
domain,
|
||||||
service,
|
service,
|
||||||
@ -223,7 +263,11 @@ class CoverEntity extends Entity {
|
|||||||
if (service === "open_cover") {
|
if (service === "open_cover") {
|
||||||
this.update("open");
|
this.update("open");
|
||||||
} else if (service === "close_cover") {
|
} else if (service === "close_cover") {
|
||||||
this.update("closing");
|
this.update("closed");
|
||||||
|
} else if (service === "set_cover_position") {
|
||||||
|
this.update(data.position > 0 ? "open" : "closed", {
|
||||||
|
current_position: data.position,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
super.handleService(domain, service, data);
|
super.handleService(domain, service, data);
|
||||||
}
|
}
|
||||||
@ -288,6 +332,19 @@ class InputSelectEntity extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ClimateEntity extends Entity {
|
class ClimateEntity extends Entity {
|
||||||
|
static CAPABILITY_ATTRIBUTES = new Set([
|
||||||
|
...CAPABILITY_ATTRIBUTES,
|
||||||
|
"hvac_modes",
|
||||||
|
"min_temp",
|
||||||
|
"max_temp",
|
||||||
|
"target_temp_step",
|
||||||
|
"fan_modes",
|
||||||
|
"preset_modes",
|
||||||
|
"swing_modes",
|
||||||
|
"min_humidity",
|
||||||
|
"max_humidity",
|
||||||
|
]);
|
||||||
|
|
||||||
public async handleService(domain, service, data) {
|
public async handleService(domain, service, data) {
|
||||||
if (domain !== this.domain) {
|
if (domain !== this.domain) {
|
||||||
return;
|
return;
|
||||||
@ -357,6 +414,14 @@ class ClimateEntity extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class WaterHeaterEntity extends Entity {
|
class WaterHeaterEntity extends Entity {
|
||||||
|
static CAPABILITY_ATTRIBUTES = new Set([
|
||||||
|
...CAPABILITY_ATTRIBUTES,
|
||||||
|
"current_temperature",
|
||||||
|
"min_temp",
|
||||||
|
"max_temp",
|
||||||
|
"operation_list",
|
||||||
|
]);
|
||||||
|
|
||||||
public async handleService(domain, service, data) {
|
public async handleService(domain, service, data) {
|
||||||
if (domain !== this.domain) {
|
if (domain !== this.domain) {
|
||||||
return;
|
return;
|
||||||
|
@ -278,6 +278,8 @@ export const provideHass = (
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
async callService(domain, service, data) {
|
async callService(domain, service, data) {
|
||||||
if (data && "entity_id" in data) {
|
if (data && "entity_id" in data) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.log("Entity service call", domain, service, data);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
ensureArray(data.entity_id).map((ent) =>
|
ensureArray(data.entity_id).map((ent) =>
|
||||||
entities[ent].handleService(domain, service, data)
|
entities[ent].handleService(domain, service, data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user