-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add Home Assistant 1.1.0 * move state component * add Search Entity Attributes root command * add heat_cool mode support * rename command * update states after action
- Loading branch information
Showing
16 changed files
with
246 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { StatesAttributesList } from "./components/attributes_all"; | ||
|
||
export default function main() { | ||
return <StatesAttributesList domain="" />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ActionPanel, CopyToClipboardAction, List } from "@raycast/api"; | ||
import { State } from "../haapi"; | ||
|
||
export function EntityAttributesList(props: { state: State }) { | ||
const state = props.state; | ||
const title = state.attributes.friendly_name | ||
? `${state.attributes.friendly_name} (${state.entity_id})` | ||
: `${state.entity_id}`; | ||
return ( | ||
<List searchBarPlaceholder="Search entity attributes"> | ||
<List.Section title={`Attributes of ${title}`}> | ||
{Object.entries(state.attributes).map(([k, v]) => ( | ||
<List.Item | ||
key={state.entity_id + k} | ||
title={k} | ||
accessoryTitle={`${v}`} | ||
actions={ | ||
<ActionPanel> | ||
<CopyToClipboardAction content={`${v}`} /> | ||
</ActionPanel> | ||
} | ||
/> | ||
))} | ||
</List.Section> | ||
</List> | ||
); | ||
} |
104 changes: 104 additions & 0 deletions
104
extensions/homeassistant/src/components/attributes_all.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { ActionPanel, CopyToClipboardAction, List, showToast, ToastStyle } from "@raycast/api"; | ||
import { useState, useEffect } from "react"; | ||
import { createHomeAssistantClient } from "../common"; | ||
|
||
export const ha = createHomeAssistantClient(); | ||
|
||
class Attribute { | ||
public name = ""; | ||
public value: any; | ||
} | ||
|
||
export function StatesAttributesList(props: { domain: string }) { | ||
const [searchText, setSearchText] = useState<string>(); | ||
const { attributes, error, isLoading } = useSearch(searchText, props.domain); | ||
|
||
if (error) { | ||
showToast(ToastStyle.Failure, "Cannot search Home Assistant states", error); | ||
} | ||
|
||
if (!attributes) { | ||
return <List isLoading={true} searchBarPlaceholder="Loading" />; | ||
} | ||
|
||
return ( | ||
<List | ||
searchBarPlaceholder="Filter by entity ID or attribute name" | ||
isLoading={isLoading} | ||
onSearchTextChange={setSearchText} | ||
> | ||
{attributes?.map((attr) => ( | ||
<List.Item | ||
key={attr.name} | ||
title={attr.name} | ||
accessoryTitle={`${attr.value}`.substring(0, 50)} | ||
actions={ | ||
<ActionPanel> | ||
<CopyToClipboardAction content={`${attr.value}`} /> | ||
</ActionPanel> | ||
} | ||
/> | ||
))} | ||
</List> | ||
); | ||
} | ||
|
||
export function useSearch( | ||
query: string | undefined, | ||
domain: string | ||
): { | ||
attributes?: Attribute[]; | ||
error?: string; | ||
isLoading: boolean; | ||
} { | ||
const [attributes, setAttributes] = useState<Attribute[]>(); | ||
const [error, setError] = useState<string>(); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
let cancel = false; | ||
|
||
useEffect(() => { | ||
async function fetchData() { | ||
if (query === null || cancel) { | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
setError(undefined); | ||
|
||
try { | ||
const haStates = await ha.getStates({ domain: domain, query: "" }); | ||
let attributesData: Attribute[] = []; | ||
haStates.forEach((e) => | ||
Object.entries(e.attributes).forEach(([k, v]) => | ||
attributesData.push({ name: `${e.entity_id}.${k}`, value: v }) | ||
) | ||
); | ||
if (query) { | ||
const lquery = query.toLocaleLowerCase().trim(); | ||
attributesData = attributesData.filter((v) => v.name.toLocaleLowerCase().includes(lquery)); | ||
} | ||
attributesData = attributesData.slice(0, 100); | ||
if (!cancel) { | ||
setAttributes(attributesData); | ||
} | ||
} catch (e: any) { | ||
if (!cancel) { | ||
setError(e.toString()); | ||
} | ||
} finally { | ||
if (!cancel) { | ||
setIsLoading(false); | ||
} | ||
} | ||
} | ||
|
||
fetchData(); | ||
|
||
return () => { | ||
cancel = true; | ||
}; | ||
}, [query]); | ||
|
||
return { attributes, error, isLoading }; | ||
} |
Oops, something went wrong.