Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Grammar & functionality around InjectEntityProxy can be improved #79

Open
zoe-codez opened this issue Apr 27, 2023 · 0 comments
Open

Grammar & functionality around InjectEntityProxy can be improved #79

zoe-codez opened this issue Apr 27, 2023 · 0 comments
Labels
📈 enhancement New feature or request. Attach project label also 🏡 home assistant @digital-alchemy/home-assistant

Comments

@zoe-codez
Copy link
Owner

Issue

Clumsy grammar

It is far too easy to write comparisons that aren't obviously badly formatted at a casual read. Example code to demonstrate issue:

@SceneRoom({  
  name: "bedroom",
  scenes: {
	off: {},
    high_dimmed: {},
    high: {}
  }
})
class Bedroom {
  constructor(
    @InjectCallProxy()
    private readonly call: iCallService,
    private readonly logger: AutoLogService,
    @SendFrom(GotifyChannels.reminders) private readonly message: GotifyApp,
    @InjectEntityProxy("binary_sensor.is_late")
    private readonly isLate: ENTITY_STATE<"binary_sensor.is_late">,
    @InjectEntityProxy("binary_sensor.is_evening")
    private readonly isEvening: ENTITY_STATE<"binary_sensor.is_evening">,
    @InjectEntityProxy("binary_sensor.is_early")
    private readonly isEarly: ENTITY_STATE<"binary_sensor.is_early">,
    @InjectEntityProxy("sensor.bedroom_current_scene")
    private readonly sceneEntity: ENTITY_STATE<"sensor.bedroom_current_scene">,
  ) {}

  public get sceneHigh() {
    if (this.currentScene === "high_dimmed") {
      return "scene.bedroom_high";
    }
    return this.isLate? "scene.bedroom_high_dimmed" : "scene.bedroom_high";
  }

  private get currentScene() {
    return this.sceneEntity.attributes.scene as ROOM_SCENES<"bedroom">;
  }

  public async setHigh(): Promise<void> {
    await this.call.scene.turn_on({
      entity_id: this.highScene,
    });
  }
}

In this code, the this.isLate? ternary in get sceneHigh will always choose the high_dimmed scene, since the comparison is looking at an object and not a boolean

Missing functionality

@InjectEntityProxy only injects the whole object, and is intended to be paired with ENTITY_STATE to provide the type definitions. Again - confusing grammar.

There is no ability to inject just the state, or a single attribute.

Proposed change

The type definitions and injector should be expanded to extract state / attributes, and coerce if needed. Examples:

@SceneRoom({  
  name: "bedroom",
  scenes: {
	off: {},
    high_dimmed: {},
    high: {}
  }
})
class Bedroom {
  constructor(
    @InjectCallProxy()
    private readonly call: iCallService,
    private readonly logger: AutoLogService,
    @SendFrom(GotifyChannels.reminders) private readonly message: GotifyApp,
    @InjectEntityState("binary_sensor.is_late")
    private readonly isLate: boolean,
    @InjectEntityState("binary_sensor.is_evening")
    private readonly isEvening: boolean,
    @InjectEntityState("binary_sensor.is_early")
    private readonly isEarly: boolean,
    @InjectEntityAttribute(["sensor.bedroom_current_scene", "scene"])
    private readonly currentScene: ROOM_SCENES<"bedroom">,
  ) {}

  public get sceneHigh() {
    if (this.currentScene === "high_dimmed") {
      return "scene.bedroom_high";
    }
    return this.isLate? "scene.bedroom_high_dimmed" : "scene.bedroom_high";
  }

  public async setHigh(): Promise<void> {
    await this.call.scene.turn_on({
      entity_id: this.highScene,
    });
  }
}

Not illustrated in this example: conversion to date / dayjs for date strings, potentially others

@zoe-codez zoe-codez added 📈 enhancement New feature or request. Attach project label also 🏡 home assistant @digital-alchemy/home-assistant labels Apr 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
📈 enhancement New feature or request. Attach project label also 🏡 home assistant @digital-alchemy/home-assistant
Projects
Development

No branches or pull requests

1 participant