Skip to content

Commit

Permalink
refactor: replace getStates with getState for improved entity retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Dec 29, 2024
1 parent bac797b commit aed25df
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 197 deletions.
2 changes: 1 addition & 1 deletion src/common/services/ComparatorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class ComparatorService {
if (cValue === '') return false;
return actualValue?.startsWith(cValue);
case 'in_group': {
const ent = this.#homeAssistant?.websocket.getStates(cValue);
const ent = this.#homeAssistant?.websocket.getState(cValue);
const groupEntities =
selectn('attributes.entity_id', ent) || [];
return groupEntities.includes(actualValue);
Expand Down
2 changes: 1 addition & 1 deletion src/common/services/IssueService/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function getInvalidIds(
}
break;
case IssueType.StateId:
if (!ha.websocket.getStates(id)) {
if (!ha.websocket.getState(id)) {
invalidIds.push(id);
}
break;
Expand Down
74 changes: 32 additions & 42 deletions src/common/services/JSONataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class JSONataService {
expr.assign('device', this.device.bind(this));
expr.assign('entity', () => entity);
expr.assign('entities', (val: string) => {
return this.#homeAssistant?.websocket?.getStates(val);
return this.#homeAssistant?.websocket?.getState(val);
});
expr.assign('outputData', (obj: string) => {
if (!obj) {
Expand Down Expand Up @@ -115,7 +115,7 @@ export default class JSONataService {
* @returns An array of devices associated with the area, or an empty array if the area does not exist or has no associated devices.
*/
areaDevices(areaId: string): HassDevice[] {
const areas = this.#homeAssistant?.websocket?.getAreas(areaId);
const areas = this.#homeAssistant?.websocket?.getArea(areaId);
const devicesInArea: HassDevice[] = [];
if (areas) {
const devices = this.#homeAssistant?.websocket?.getDevices();
Expand Down Expand Up @@ -143,31 +143,27 @@ export default class JSONataService {
* @returns An array of entities associated with the area, or an empty array if the area does not exist or has no associated entities.
*/
areaEntities(areaId: string): HassEntity[] {
const areas = this.#homeAssistant?.websocket?.getAreas(areaId);
const areas = this.#homeAssistant?.websocket?.getArea(areaId);
const entitiesInArea: HassEntity[] = [];
if (areas) {
const entityRegistry =
this.#homeAssistant?.websocket?.getEntities();
if (entityRegistry) {
const devices = this.#homeAssistant?.websocket?.getDevices();
entityRegistry.forEach((entry) => {
const entity = this.#homeAssistant?.websocket?.getStates(
entry.entity_id,
);
if (entity) {
if (entry.area_id === areaId) {
const devices = this.#homeAssistant?.websocket?.getDevices();
this.#homeAssistant?.websocket?.getEntities().forEach((entry) => {
const entity = this.#homeAssistant?.websocket?.getState(
entry.entity_id,
);
if (entity) {
if (entry.area_id === areaId) {
entitiesInArea.push(entity);
} else {
const device = devices?.find(
(device) => device.id === entry.device_id,
);
if (device?.area_id === areaId) {
entitiesInArea.push(entity);
} else {
const device = devices?.find(
(device) => device.id === entry.device_id,
);
if (device?.area_id === areaId) {
entitiesInArea.push(entity);
}
}
}
});
}
}
});
}

return entitiesInArea;
Expand Down Expand Up @@ -201,7 +197,7 @@ export default class JSONataService {
}

if (validEntityId(lookup)) {
const entity = this.#homeAssistant?.websocket?.getEntities(lookup);
const entity = this.#homeAssistant?.websocket?.getEntity(lookup);

if (entity) {
// check if entity has area id and return area name
Expand All @@ -217,7 +213,7 @@ export default class JSONataService {

// check if entity has device id and return area name
if (entity.device_id) {
const device = this.#homeAssistant?.websocket?.getDevices(
const device = this.#homeAssistant?.websocket?.getDevice(
entity.device_id,
);
if (device) {
Expand Down Expand Up @@ -261,7 +257,7 @@ export default class JSONataService {
* @returns The device associated with the entity or the device that matches the name, or undefined if the lookup value does not match any entity or device.
*/
device(lookup: string): HassDevice | undefined {
const entities = this.#homeAssistant?.websocket?.getEntities(lookup);
const entities = this.#homeAssistant?.websocket?.getEntity(lookup);
const devices = this.#homeAssistant?.websocket?.getDevices();
if (entities) {
if (devices) {
Expand Down Expand Up @@ -291,25 +287,19 @@ export default class JSONataService {
* @returns An array of entities associated with the device, or an empty array if the device does not exist or has no associated entities.
*/
deviceEntities(deviceId: string): HassEntity[] {
const devices = this.#homeAssistant?.websocket?.getDevices(deviceId);
let entities: HassEntity[] = [];
const devices = this.#homeAssistant?.websocket?.getDevice(deviceId);
const entities: HassEntity[] = [];
if (devices) {
const entityRegistry =
this.#homeAssistant?.websocket?.getEntities();
if (entityRegistry) {
entities = entityRegistry.reduce((acc, entry) => {
if (entry.device_id === deviceId) {
const entity =
this.#homeAssistant?.websocket?.getStates(
entry.entity_id,
);
if (entity) {
acc.push(entity);
}
this.#homeAssistant?.websocket?.getEntities().forEach((entry) => {
if (entry.device_id === deviceId) {
const entity = this.#homeAssistant?.websocket?.getState(
entry.entity_id,
);
if (entity) {
entities.push(entity);
}
return acc;
}, [] as HassEntity[]);
}
}
});
}
return entities;
}
Expand Down
65 changes: 16 additions & 49 deletions src/homeAssistant/Websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
HassDeviceCapabilities,
HassDevices,
HassDeviceTriggers,
HassEntityRegistry,
HassEntityRegistryEntry,
HassFloor,
HassLabel,
Expand Down Expand Up @@ -120,7 +121,7 @@ export default class Websocket {
areas: HassAreas = [];
client!: Connection;
devices: HassDevices = [];
entities: HassEntityRegistryEntry[] = [];
entities: HassEntityRegistry = new Map();
floors: HassFloor[] = [];
labels: HassLabel[] = [];
connectionState = ClientState.Disconnected;
Expand Down Expand Up @@ -278,7 +279,9 @@ export default class Websocket {
});
subscribeEntityRegistry(this.client, (entities) => {
this.#isEntityRegistryLoaded = true;
this.entities = entities;
entities.forEach((entity) =>
this.entities.set(entity.entity_id, entity),
);
this.#checkIfAllRegistriesLoaded();
this.#emitEvent(HaEvent.EntityRegistryUpdated, entities);
});
Expand Down Expand Up @@ -609,27 +612,15 @@ export default class Websocket {
this.#emitEvent(ClientEvent.Close);
}

getAreas(areaId: string): HassArea | undefined;
getAreas(): HassAreas;
getAreas(areaId?: unknown): unknown {
if (typeof areaId === 'string') {
this.getArea(areaId);
}

getAreas(): HassAreas {
return cloneDeep(this.areas);
}

getArea(areaId: string): HassArea | undefined {
return cloneDeep(this.areas.find((area) => area.area_id === areaId));
}

getDevices(deviceId: string): HassDevice | undefined;
getDevices(): HassDevices;
getDevices(deviceId?: unknown): unknown {
if (typeof deviceId === 'string') {
this.getDevice(deviceId);
}

getDevices(): HassDevices {
return cloneDeep(this.devices);
}

Expand Down Expand Up @@ -693,29 +684,15 @@ export default class Websocket {
return results.extra_fields;
}

getEntities(): HassEntityRegistryEntry[];
getEntities(entityId?: string): HassEntityRegistryEntry | undefined;
getEntities(entityId?: unknown): unknown {
if (typeof entityId === 'string') {
this.getEntity(entityId);
}

getEntities(): HassEntityRegistry {
return cloneDeep(this.entities);
}

getEntity(entityId: string): HassEntityRegistryEntry | undefined {
return cloneDeep(
this.entities.find((entity) => entity.entity_id === entityId),
);
return cloneDeep(this.entities.get(entityId));
}

getFloors(floorId: string): HassFloor | undefined;
getFloors(): HassFloor[];
getFloors(floorId?: unknown): unknown {
if (typeof floorId === 'string') {
this.getFloor(floorId);
}

getFloors(): HassFloor[] {
return cloneDeep(this.floors);
}

Expand All @@ -725,13 +702,7 @@ export default class Websocket {
);
}

getLabels(labelId: string): HassLabel | undefined;
getLabels(): HassLabel[];
getLabels(labelId?: unknown): unknown {
if (typeof labelId === 'string') {
this.getLabels(labelId);
}

getLabels(): HassLabel[] {
return cloneDeep(this.labels);
}

Expand All @@ -741,18 +712,14 @@ export default class Websocket {
);
}

getStates(): HassEntities;
getStates(entityId?: string): HassEntity | null;
getStates(entityId?: unknown): unknown {
if (typeof entityId === 'string') {
return this.states[entityId]
? cloneDeep(this.states[entityId])
: null;
}

getStates(): HassEntities {
return cloneDeep(this.states);
}

getState(entityId: string): HassEntity | undefined {
return cloneDeep(this.states[entityId]);
}

getServices(): HassServices {
return cloneDeep(this.services);
}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/current-state/CurrentStateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class CurrentStateController extends InputOutputController<
this.#homeAssistant.websocket.getStates(),
);

const entity = this.#homeAssistant.websocket.getStates(
const entity = this.#homeAssistant.websocket.getState(
entityId,
) as HassEntity;
if (!entity) {
Expand Down
Loading

0 comments on commit aed25df

Please sign in to comment.