Skip to content

Commit

Permalink
fix input_boolean type, fix input intial value
Browse files Browse the repository at this point in the history
  • Loading branch information
Matchlighter committed Feb 2, 2024
1 parent a735f42 commit 8b24df6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/plugins/home_assistant/api_entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { chainedDecorators, dec_once } from "../../common/decorators";
import { current } from "../../hypervisor/current";
import { persistent } from "../../runtime/persistence";
import { Annotable, assert_application_context, client_call_safe, getOrCreateLocalData, notePluginAnnotation } from "../base";
import { ButtonOptions, InputButton, InputEntity, InputOptions, InputSelect, NumberInputOptions, TDEntity } from "./entity_api";
import { ButtonOptions, HABoolean, InputButton, InputEntity, InputOptions, InputSelect, NumberInputOptions, TDEntity } from "./entity_api";
import { trackAutocleanEntity } from "./entity_api/auto_cleaning";
import { domain_entities } from "./entity_api/domains";
import { EntityClass, EntityClassConstructor, EntityClassOptions, EntityClassType } from "./entity_api/domains/base";
Expand Down Expand Up @@ -64,7 +64,7 @@ export const _entitySubApi = (_plugin: () => HomeAssistantPlugin) => {
}

function _linkFieldEntityBase<T extends TDEntity<any>>(
construct: () => T,
construct: (self: any) => T,
context: DecoratorContext,
roptions: EntityRegistrationOptions,
init_callback?: (self, ent: T) => void,
Expand All @@ -80,7 +80,7 @@ export const _entitySubApi = (_plugin: () => HomeAssistantPlugin) => {
const init_linked = async (self) => {
if (get_linked(self, false)) return;

const ent = construct();
const ent = construct(self);
init_callback?.(self, ent);
ents.set(self, ent);

Expand Down Expand Up @@ -219,13 +219,28 @@ export const _entitySubApi = (_plugin: () => HomeAssistantPlugin) => {
return ((access, context) => {
const { entity_options, registration_options } = separateRegistrationOptions(options);

if (entity_options.existing) {
if (registration_options.auto_clean) throw new Error("Cannot use auto_clean: true with existing: true");
registration_options.auto_clean = false;
}

const initialValues = new WeakMap<any, any>();

const { get_linked } = _linkFieldEntityBase(
() => new InputEntity(entity_options.id || String(context.name), { domain, ...entity_options }),
(self) => new InputEntity(entity_options.id || String(context.name), {
domain,
initial: initialValues.get(self),
...entity_options,
}),
context,
registration_options,
)

return {
init(v) {
initialValues.set(this, v);
return v;
},
get() {
return get_linked(this, true).state;
},
Expand Down Expand Up @@ -330,10 +345,10 @@ export const _entitySubApi = (_plugin: () => HomeAssistantPlugin) => {
text: inputApi<string, { min?: number, max?: number, pattern?: string | RegExp, mode?: 'text' | 'password' } & InputOptions<string>>("input_text"),

/** Create an `input_boolean` helper and sync it with the decorated accessor */
boolean: inputApi<boolean, InputOptions<boolean>>("input_boolean"),
boolean: inputApi<HABoolean, InputOptions<HABoolean>>("input_boolean"),

/** Create an `input_boolean` helper and sync it with the decorated accessor */
bool: inputApi<boolean, InputOptions<boolean>>("input_boolean"),
bool: inputApi<HABoolean, InputOptions<HABoolean>>("input_boolean"),

/** Create an `input_datetime` helper and sync it with the decorated accessor */
datetime: inputApi<Date | number | Iso8601String, { has_date?: boolean, has_time?: boolean } & InputOptions<string>>("input_datetime"),
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/home_assistant/entity_api/input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { observable, runInAction } from "mobx";
import * as moment from "moment";

import type { HomeAssistantPlugin } from "..";
import { client_call_safe } from "../../base";
Expand Down Expand Up @@ -31,6 +32,8 @@ export interface NumberInputOptions extends InputOptions<number> {
mode?: 'slider' | 'box';
}

export type HABoolean = "on" | "off";

class InputEntity<T> extends TDEntity<T> {
constructor(readonly id: string, readonly options: InputOptions<T>) {
super();
Expand Down Expand Up @@ -79,6 +82,16 @@ class InputEntity<T> extends TDEntity<T> {
entity_id: this.uuid,
};

if (this.domain == "input_datetime" && value instanceof Date) {
value = value.toISOString() as any;
}
if (this.domain == "input_datetime" && moment.isMoment(value)) {
value = value.toISOString() as any;
}
if (this.domain == "input_boolean" && typeof value == "boolean") {
value = value ? "on" : "off" as any;
}

if (this.domain == "input_select") {
service = "input_select.select_option";
service_params["option"] = value;
Expand Down

0 comments on commit 8b24df6

Please sign in to comment.