Skip to content

Commit

Permalink
Merge pull request #11 from Digital-Alchemy-TS/registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe authored Apr 16, 2024
2 parents 9ba7b1a + 09a854b commit 55b650d
Show file tree
Hide file tree
Showing 23 changed files with 861 additions and 103 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"zeroconf"
]
}
13 changes: 11 additions & 2 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ ignorePaths: []
dictionaryDefinitions: []
dictionaries: []
words:
- codeowners
- entites
- hass
- quickboot
- hassio
- entites
- homekit
- endregion
- macaddress
- quickboot
- ssdp
- systype
- zeroconf
- homeassistant
- rrule
ignoreWords: []
import: []
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@digital-alchemy/hass",
"repository": "https://github.com/Digital-Alchemy-TS/hass",
"homepage": "https://docs.digital-alchemy.app/Hass",
"version": "0.3.9",
"version": "0.3.10",
"scripts": {
"build": "rm -rf dist/; tsc",
"lint": "eslint src",
Expand Down
6 changes: 6 additions & 0 deletions src/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// @ts-nocheck
import { PICK_ENTITY } from "./helpers";

export type TFloorId = string & { floor: boolean };
export type TAreaId = string & { area: true };
export type TLabelId = string & { label: string };
export type TDeviceId = string & { device: true };
export type TZoneId = string & { zone: true };

/**
* ## THIS FILE IS INTENDED TO BE REPLACED
*
Expand Down
48 changes: 48 additions & 0 deletions src/extensions/area.extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TServiceParams } from "@digital-alchemy/core";

import { TAreaId } from "../dynamic";
import { AreaCreate, AreaDetails } from "../helpers";

export function Area({ hass, context, config, logger }: TServiceParams) {
hass.socket.onConnect(async () => {
if (!config.hass.AUTO_CONNECT_SOCKET || !config.hass.MANAGE_REGISTRY) {
return;
}
hass.area.current = await hass.area.list();
hass.socket.subscribe({
context,
event_type: "area_registry_updated",
async exec() {
hass.area.current = await hass.area.list();
logger.debug(`area registry updated`);
},
});
});

return {
async create(details: AreaCreate) {
await hass.socket.sendMessage({
type: "config/area_registry/create",
...details,
});
},
current: [] as AreaDetails[],
async delete(area_id: TAreaId) {
await hass.socket.sendMessage({
area_id,
type: "config/area_registry/delete",
});
},
async list() {
return await hass.socket.sendMessage<AreaDetails[]>({
type: "config/area_registry/list",
});
},
async update(details: AreaDetails) {
await hass.socket.sendMessage({
type: "config/area_registry/update",
...details,
});
},
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HALF, SECOND, sleep, TServiceParams } from "@digital-alchemy/core";

import { BackupResponse, HASSIO_WS_COMMAND, HomeAssistantBackup } from "..";
import { BackupResponse, HomeAssistantBackup } from "../helpers";

export function Utilities({ logger, hass }: TServiceParams) {
export function Backup({ logger, hass }: TServiceParams) {
async function generate(): Promise<HomeAssistantBackup> {
let current = await list();
// const originalLength = current.backups.length;
Expand All @@ -13,9 +13,7 @@ export function Utilities({ logger, hass }: TServiceParams) {
);
} else {
logger.info({ name: generate }, "initiating new backup");
hass.socket.sendMessage({
type: HASSIO_WS_COMMAND.generate_backup,
});
hass.socket.sendMessage({ type: "backup/generate" });
while (current.backing_up === false) {
logger.debug({ name: generate }, "... waiting");
await sleep(HALF * SECOND);
Expand All @@ -34,17 +32,14 @@ export function Utilities({ logger, hass }: TServiceParams) {
async function list(): Promise<BackupResponse> {
logger.trace({ name: list }, "send");
return await hass.socket.sendMessage<BackupResponse>({
type: HASSIO_WS_COMMAND.backup_info,
type: "backup/info",
});
}

async function remove(slug: string): Promise<void> {
logger.trace({ name: remove }, "send");
await hass.socket.sendMessage(
{ slug, type: HASSIO_WS_COMMAND.remove_backup },
false,
);
await hass.socket.sendMessage({ slug, type: "backup/remove" }, false);
}

return { backup: { generate, list, remove } };
return { generate, list, remove };
}
45 changes: 45 additions & 0 deletions src/extensions/conversation.extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TServiceParams } from "@digital-alchemy/core";

import { EditAliasOptions, ToggleExpose, UPDATE_REGISTRY } from "../helpers";

export function Conversation({ hass, logger }: TServiceParams) {
async function addAlias({ entity, alias }: EditAliasOptions) {
const current = await hass.entity.registry.get(entity);
if (current?.aliases?.includes(alias)) {
logger.debug({ name: entity }, `already has alias {%s}`, alias);
return;
}
await hass.socket.sendMessage({
entity_id: entity,
type: UPDATE_REGISTRY,
});
}

async function removeAlias({ entity, alias }: EditAliasOptions) {
const current = await hass.entity.registry.get(entity);
if (!current?.aliases?.includes(alias)) {
logger.debug({ name: entity }, `does not have alias {%s}`, alias);
return;
}
await hass.socket.sendMessage({ entity_id: entity, type: UPDATE_REGISTRY });
}

async function setConversational({
entity_ids,
assistants,
should_expose,
}: ToggleExpose) {
await hass.socket.sendMessage({
assistants: [assistants].flat(),
entity_ids: [entity_ids].flat(),
should_expose,
type: UPDATE_REGISTRY,
});
}

return {
addAlias,
removeAlias,
setConversational,
};
}
39 changes: 39 additions & 0 deletions src/extensions/device.extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { TServiceParams } from "@digital-alchemy/core";

import { DeviceDetails } from "../helpers";

export function Device({ hass, config, context, logger }: TServiceParams) {
hass.socket.onConnect(async () => {
if (!config.hass.AUTO_CONNECT_SOCKET || !config.hass.MANAGE_REGISTRY) {
return;
}
hass.device.current = await hass.device.list();
hass.socket.subscribe({
context,
event_type: "device_registry_updated",
async exec() {
hass.device.current = await hass.device.list();
logger.debug(`device registry updated`);
},
});
await SubscribeUpdates();
});

async function SubscribeUpdates() {
await hass.socket.sendMessage({
event_type: "device_registry_updated",
type: "subscribe_events",
});
}

async function list() {
return await hass.socket.sendMessage<DeviceDetails[]>({
type: "config/device_registry/list",
});
}

return {
current: [] as DeviceDetails[],
list,
};
}
Loading

0 comments on commit 55b650d

Please sign in to comment.