Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read single entities #511

Open
Tracked by #516
RonnyWinkler opened this issue Jul 21, 2024 · 3 comments
Open
Tracked by #516

Read single entities #511

RonnyWinkler opened this issue Jul 21, 2024 · 3 comments

Comments

@RonnyWinkler
Copy link

Use case:
An app is using a small set of entities, On startup, the current entity state should be read from HA to initialize the app with current/valid states.

The app can listen to entity state changed, but this will only update if the state changes on HA (event state_changed).
The app can read all entities (message get_states).
But it's not possible to read single entity states. Did I miss that or is it not implemented yet?

@vecna-signal vecna-signal mentioned this issue Aug 1, 2024
@wjtje
Copy link

wjtje commented Oct 7, 2024

After facing the same issue I went looking into the code and found an (undocumented) command that can be used for this:

{
  "id": 1,
  "type": "subscribe_entities",
  "entity_ids": [
    "light.my_light"
  ]
}

This will create an subscription to that entity, but will also send the current state. You can use Unsubscribing from events to unsubscribe.

Sources:

Other notable sources:

@Tsjippy
Copy link

Tsjippy commented Oct 28, 2024

After facing the same issue I went looking into the code and found an (undocumented) command that can be used for this:

{
  "id": 1,
  "type": "subscribe_entities",
  "entity_ids": [
    "light.my_light"
  ]
}

This will create an subscription to that entity, but will also send the current state. You can use Unsubscribing from events to unsubscribe.

Sources:

Other notable sources:

Thank you for this answer.

But I don't understand the code well enough to understand your answer. Can you give me an example? To which function do you supply this data?

@Tsjippy
Copy link

Tsjippy commented Nov 10, 2024

After facing the same issue I went looking into the code and found an (undocumented) command that can be used for this:

{
  "id": 1,
  "type": "subscribe_entities",
  "entity_ids": [
    "light.my_light"
  ]
}

This will create an subscription to that entity, but will also send the current state. You can use Unsubscribing from events to unsubscribe.
Sources:

Other notable sources:

Thank you for this answer.

But I don't understand the code well enough to understand your answer. Can you give me an example? To which function do you supply this data?

I figured it out! But I don't understand typescript so i cannot make a pull request. This is what I changed:
entities.js:

export const entitiesColl = (conn, entity_ids=[]) => atLeastHaVersion(conn.haVersion, 2022, 4, 0)
    ? getCollection(conn, "_ent", undefined, subscribeUpdates, {unsubGrace: true, entity_ids: entity_ids})
    : getCollection(conn, "_ent", legacyFetchEntities, legacySubscribeUpdates);
export const subscribeEntities = (conn, onChange, entity_ids=[]) => entitiesColl(conn, entity_ids).subscribe(onChange)
const subscribeUpdates = (conn, store, entity_ids=[]) => {
    conn.subscribeMessage((ev) => processEvent(store, ev), {
        type: "subscribe_entities",
        entity_ids: entity_ids,
    });
}

collection.js:

export const getCollection = (conn, key, fetchCollection, subscribeUpdates, options = { unsubGrace: true, entity_ids: [] }) => {
....
(line 48)
if (subscribeUpdates) {
    unsubProm = subscribeUpdates(conn, store, options.entity_ids);
}
...

So maybe someone can implement in case this project is still maintained?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants