initial v2 list

This commit is contained in:
ari committed 2022-07-27 19:27:30 -04:00
1 parent dfcbe9e76a
commit f20eb271d9
23 files changed
+1379 -54

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
import { createHAEntitiesState } from "./HAEntities";
import { EntitySource } from "./types";
import { HassEntities } from "home-assistant-js-websocket";
export type MockHAEntitiesProps =
| {
loading: true;
}
| {
loading: false;
entities: HassEntities;
entitySource: Record<string, EntitySource>;
};
export const useMockHAEntities = (args: MockHAEntitiesProps) => {
if (args.loading) {
return createHAEntitiesState(
{
additional: {},
ready: false,
collection: {
state: {},
refresh: () => new Promise(() => {}),
subscribe: () => () => {},
},
},
{
additional: {},
ready: false,
collection: {
state: {},
refresh: () => new Promise(() => {}),
subscribe: () => () => {},
},
}
);
} else {
return createHAEntitiesState(
{
additional: {},
ready: true,
collection: {
state: args.entities,
refresh: () => new Promise(() => {}),
subscribe: () => () => {},
},
},
{
additional: {},
ready: true,
collection: {
state: args.entitySource,
refresh: () => new Promise(() => {}),
subscribe: () => () => {},
},
}
);
}
};
+17 -4
View File
@@ -1,17 +1,30 @@
import { Option } from "components/Inputs/InputAutoComplete";
import { useHassCollection } from "./useHassCollection";
import { entitiesColl } from "home-assistant-js-websocket";
import { HACollectionState, useHassCollection } from "./useHassCollection";
import { entitiesColl, HassEntities } from "home-assistant-js-websocket";
import { entitySourceColl } from "./additionalCollections";
import { EntitySource } from "./types";
export type EntityOption = Option<{ domain: string; integration: string }>;
export type HAEntitiesState = ReturnType<typeof useHAEntities>;
export const useHAEntities = () => {
export type HAEntitiesState = ReturnType<typeof createHAEntitiesState>;
export const useHAEntities = (): HAEntitiesState => {
const entities = useHassCollection(entitiesColl, (state) => ({
domains: Array.from(
new Set(Object.keys(state).map((x) => x.split(".")[0])).keys()
).sort(),
}));
const entitySource = useHassCollection(entitySourceColl);
return createHAEntitiesState(entities, entitySource);
};
export const createHAEntitiesState = (
entities: HACollectionState<
HassEntities,
{
domains: string[];
}
>,
entitySource: HACollectionState<Record<string, EntitySource>, {}>
) => {
const methods = {
getStates(inp: string[] | string, attribute?: string): string[] {
if (!entities.ready) {
+34 -2
View File
@@ -1,12 +1,44 @@
import React, { FC } from "react";
import React, { FC, useState } from "react";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useHAEntities } from "haService/HAEntities";
const Test: FC<{ onRender: () => void }> = ({ onRender }) => {
const [searchTerm, setSearch] = useState("");
const conn = useHAEntities();
onRender && onRender();
return <code>{JSON.stringify(conn, null, 4)}</code>;
return (
<>
<input
key="text"
type="text"
value={searchTerm}
onChange={(e) => setSearch(e.target.value.toLowerCase())}
/>
<ul
key="list"
style={{
maxHeight: "60vh",
overflow: "auto",
}}
>
{Object.entries(conn.collection?.state ?? {})
.filter(([key, _]) => key.toLowerCase().includes(searchTerm))
.map(([key, value]) => {
return (
<li key={key}>
<b>{key}</b> <br />
Context: {JSON.stringify(value.context)} <br />
Attributes: {JSON.stringify(value.attributes)} <br />
State: {JSON.stringify(value.state)} <br />
Updated at: {JSON.stringify(value.last_updated)} <br />
Changed at: {JSON.stringify(value.last_changed)} <br />
</li>
);
})}
</ul>
</>
);
};
export default {
+37 -32
View File
@@ -1,45 +1,50 @@
import React, { FC } from "react";
import { ComponentMeta, ComponentStory } from '@storybook/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 => <>
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}>
{row.entity_id}
{/* {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>
}
{/* <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>
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} />
export const Base: ComponentStory<typeof Test> = (props) => {
return (
<Page>
<Test {...props} />
</Page>
};
);
};