device id
This commit is contained in:
1 parent
8a90b56ac3
commit
f1b24ec783
17 files changed
+438
-65
No files matched your search
@@ -0,0 +1,21 @@
|
||||
import { Connection, getCollection } from "home-assistant-js-websocket";
|
||||
import { DeviceRegistryItem, HassDevices } from './types';
|
||||
|
||||
|
||||
export const fetchDeviceRegistry = (conn: Connection): Promise<Record<string, DeviceRegistryItem>> =>
|
||||
conn.sendMessagePromise<HassDevices>({
|
||||
"type": "config/device_registry/list"
|
||||
}).then(
|
||||
data =>
|
||||
data.filter(
|
||||
({ disabled_by }) => !disabled_by
|
||||
).reduce<Record<string, DeviceRegistryItem>>((all, n) => {
|
||||
all[n.id] = n
|
||||
return all
|
||||
}, {}
|
||||
))
|
||||
export const deviceRegistryColl = (conn: Connection) => getCollection<Record<string, DeviceRegistryItem>>(
|
||||
conn,
|
||||
"_$drc",
|
||||
fetchDeviceRegistry
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Connection, getCollection } from "home-assistant-js-websocket";
|
||||
import { HassDevices, HassEntitiesRegistry } from './types';
|
||||
|
||||
|
||||
export const fetchEntityRegistry = (conn: Connection) => conn.sendMessagePromise<HassEntitiesRegistry>({
|
||||
"type": "config/entity_registry/list"
|
||||
}).then(data => data.filter(({ disabled_by }) => !disabled_by))
|
||||
export const entityRegistryColl = (conn: Connection) => getCollection<HassEntitiesRegistry>(conn, "_$erc", fetchEntityRegistry)
|
||||
@@ -0,0 +1,42 @@
|
||||
import React, { FC } from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { Page } from "components/Page";
|
||||
import { useHADeviceRegistry } from "haService";
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
|
||||
const Test: FC<{ onRender: () => void }> = ({ onRender }) => {
|
||||
const conn = useHADeviceRegistry();
|
||||
onRender && onRender()
|
||||
if (conn.ready) {
|
||||
return <div>
|
||||
{Object.keys(conn.collection.state).length} items
|
||||
<ul style={{
|
||||
maxHeight: '100vh',
|
||||
overflow: 'auto'
|
||||
}}>
|
||||
{Object.keys(conn.collection.state).map(k => <li key={k}>
|
||||
<b>{k}</b>
|
||||
{/* <u>{conn.collection.state[k].length > 1 ? conn.collection.state[k].length : ''}</u> */}
|
||||
<InputYaml value={conn.collection.state[k]} onChange={() => { }} label={k} />
|
||||
</li>)}
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
return <code>
|
||||
Loading...
|
||||
</code>
|
||||
}
|
||||
|
||||
export default {
|
||||
title: 'Services/Device Registry',
|
||||
component: Test,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {}
|
||||
} as ComponentMeta<typeof Test>
|
||||
|
||||
export const Base: ComponentStory<typeof Test> = props => {
|
||||
|
||||
return <Page>
|
||||
<Test {...props} />
|
||||
</Page>
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
import React, { FC } from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { Page } from "components/Page";
|
||||
import { useHAEntityRegistry } from "haService";
|
||||
import InputYaml from "components/Inputs/InputYaml";
|
||||
|
||||
const Test: FC<{ onRender: () => void }> = ({ onRender }) => {
|
||||
const conn = useHAEntityRegistry();
|
||||
onRender && onRender()
|
||||
if (conn.ready) {
|
||||
return <div>
|
||||
{conn.collection.state.length}
|
||||
<ul style={{
|
||||
maxHeight: '100vh',
|
||||
overflow: 'auto'
|
||||
}}>
|
||||
{conn.collection.state.map((row, i) => <li key={i}>
|
||||
{/* {Object.keys(row).map(k => <>
|
||||
<span key={k}>
|
||||
<b>{k}</b>: {JSON.stringify(row[k] as any)}
|
||||
</span><br key={`${k}-br`} />
|
||||
</>)} */}
|
||||
{/* <InputYaml label={String(i)} value={row} onChange={() => { }} /> */}
|
||||
</li>)}
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
return <code>
|
||||
Loading...
|
||||
</code>
|
||||
}
|
||||
|
||||
export default {
|
||||
title: 'Services/Entity Registry',
|
||||
component: Test,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {}
|
||||
} as ComponentMeta<typeof Test>
|
||||
|
||||
export const Base: ComponentStory<typeof Test> = props => {
|
||||
|
||||
return <Page>
|
||||
<Test {...props} />
|
||||
</Page>
|
||||
};
|
||||
@@ -1,11 +1,14 @@
|
||||
import { entitiesColl, servicesColl } from "home-assistant-js-websocket"
|
||||
import { deviceRegistryColl } from "./DeviceRegistry";
|
||||
import { entityRegistryColl } from "./EntityRegistry";
|
||||
import { DeviceRegistryItem } from "./types";
|
||||
import { useHassCollection } from "./useHassCollection"
|
||||
|
||||
export type Option = string | {
|
||||
export type Option<T = {}> = string | ({
|
||||
id: string,
|
||||
label: string,
|
||||
domain: string,
|
||||
}
|
||||
} & T)
|
||||
export type EntityOption = Option<{ domain: string; }>
|
||||
export type HAEntitiesState = ReturnType<typeof useHAEntities>;
|
||||
export const useHAEntities = () => {
|
||||
const entities = useHassCollection(entitiesColl, state => ({
|
||||
@@ -33,7 +36,7 @@ export const useHAEntities = () => {
|
||||
}
|
||||
return null
|
||||
},
|
||||
getOptions(restrictToDomain?: string[]): Option[] {
|
||||
getOptions(restrictToDomain?: string[]): EntityOption[] {
|
||||
if (!entities.ready) {
|
||||
return []
|
||||
}
|
||||
@@ -48,7 +51,7 @@ export const useHAEntities = () => {
|
||||
domain: key.split('.')[0] ?? "n/a",
|
||||
}))
|
||||
},
|
||||
getLabel: (opt: Option): string => {
|
||||
getLabel: (opt: EntityOption): string => {
|
||||
if (typeof opt === 'string') {
|
||||
if (entities.ready) {
|
||||
return entities.collection.state[opt]?.attributes.friendly_name ?? opt;
|
||||
@@ -57,7 +60,7 @@ export const useHAEntities = () => {
|
||||
}
|
||||
return opt.label ?? opt.id
|
||||
},
|
||||
getID: (opt: Option): string => {
|
||||
getID: (opt: EntityOption): string => {
|
||||
if (typeof opt === 'string') {
|
||||
return opt
|
||||
}
|
||||
@@ -74,4 +77,47 @@ export const useHAServices = () => {
|
||||
const services = useHassCollection(servicesColl);
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
export type DeviceRegistryOption = Option<{ manufacturer: string }>;
|
||||
export const useHADeviceRegistry = () => {
|
||||
const dr = useHassCollection(deviceRegistryColl)
|
||||
const getLabelForItem = (item: DeviceRegistryItem) => item ? item.name_by_user ??
|
||||
item.name ??
|
||||
item.id :
|
||||
"";
|
||||
const methods = {
|
||||
getOptions(): DeviceRegistryOption[] {
|
||||
if (!dr.ready) {
|
||||
return []
|
||||
}
|
||||
const keys = Object.keys(dr.collection.state);
|
||||
return keys.map(key => ({
|
||||
id: key,
|
||||
label: getLabelForItem(dr.collection.state[key]) ?? key,
|
||||
manufacturer: dr.collection.state[key].manufacturer ?? "",
|
||||
}))
|
||||
},
|
||||
getLabel: (opt: DeviceRegistryOption): string => {
|
||||
if (typeof opt === 'string') {
|
||||
if (dr.ready) {
|
||||
return getLabelForItem(dr.collection.state[opt]) ?? opt
|
||||
}
|
||||
return opt
|
||||
}
|
||||
return opt.label ?? opt.id
|
||||
},
|
||||
getID: (opt: DeviceRegistryOption): string => {
|
||||
if (typeof opt === 'string') {
|
||||
return opt
|
||||
}
|
||||
return opt.id
|
||||
}
|
||||
}
|
||||
return { ...dr, ...methods }
|
||||
}
|
||||
export const useHAEntityRegistry = () => {
|
||||
const dr = useHassCollection(entityRegistryColl)
|
||||
|
||||
return dr
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
export type EntityRegisteryItem = {
|
||||
area_id: string | null;
|
||||
config_entry_id: string | null;
|
||||
device_id: string | null;
|
||||
disabled_by:
|
||||
| "integration"
|
||||
| "user"
|
||||
| "config_entry"
|
||||
| string
|
||||
| null
|
||||
;
|
||||
entity_category:
|
||||
| "diagnostic"
|
||||
| "config"
|
||||
| string
|
||||
| null
|
||||
;
|
||||
hidden_by:
|
||||
| "integration"
|
||||
| "user"
|
||||
| string
|
||||
| null
|
||||
;
|
||||
icon: string | null;
|
||||
name: string | null;
|
||||
platform: string | null;
|
||||
}
|
||||
|
||||
export type DeviceRegistryItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
name_by_user: string | null;
|
||||
manufacturer: string | null;
|
||||
entry_type: 'service' | string | null;
|
||||
disabled_by: EntityRegisteryItem['disabled_by'];
|
||||
area_id: string | null;
|
||||
}
|
||||
|
||||
export type HassDevices = Array<DeviceRegistryItem>;
|
||||
export type HassEntitiesRegistry = Array<EntityRegisteryItem>;
|
||||
@@ -2,18 +2,21 @@ import { Collection, Connection } from "home-assistant-js-websocket";
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useHAConnection } from "./connection";
|
||||
|
||||
export type HACollectionState<T> = {
|
||||
type _HACollectionState<T> = {
|
||||
ready: false,
|
||||
collection?: Collection<T>,
|
||||
} | {
|
||||
ready: true,
|
||||
collection: Collection<T>,
|
||||
}
|
||||
export type HACollectionState<T, D = {}> = _HACollectionState<T> & {
|
||||
additional: Partial<D>
|
||||
}
|
||||
export const useHassCollection = <T, D extends {} = {}>(
|
||||
getHACollection: (c: Connection) => Collection<T>,
|
||||
updateAdditionalProps?: (n: T | {}) => D,
|
||||
) => {
|
||||
const [state, setState] = useState<HACollectionState<T>>({
|
||||
): HACollectionState<T, D> => {
|
||||
const [state, setState] = useState<_HACollectionState<T>>({
|
||||
ready: false
|
||||
});
|
||||
const conn = useHAConnection();
|
||||
|
||||
Reference in new issue
Block a user