Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ha Cover Support (Initial) #2165

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 88 additions & 52 deletions src/components/dashboard/views/widgets/HomeAssistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import {
STATES_OFF,
} from "@/utils/homeAssistant/const";
import { domainIcon } from "@/utils/homeAssistant/icons";
import { ExpandedHomeAssistantCover } from "@/components/dashboard/views/widgets/expanded/homeAssistant/Cover";
import { primaryColorRgb } from "@/utils/theme";
import { useHomeAssistant } from "@/providers/HomeAssistantProvider";
import { WidgetAction } from "@/types/widget.type";
import { WidgetImage } from "@/components/dashboard/views/widgets/Image";

const DOMAINS_WITH_ACTIVATE_CONDITION = new Set(["cover"]);

export function WidgetHomeAssistant({
data,
editing,
Expand All @@ -33,11 +37,16 @@ export function WidgetHomeAssistant({
return homeAssistant.entities[data.entityId];
}, [data.entityId, homeAssistant.entities]);

const clickable = useMemo<boolean>(() => {
const canTurnOnOff = useMemo<boolean>(() => {
if (!homeAssistant.client || !entity) return false;
return homeAssistant.client.entityCanTurnOnOff(entity);
}, [entity, homeAssistant.client]);

const clickable = useMemo<boolean>(() => {
if (!homeAssistant.client || !entity) return false;
return canTurnOnOff;
}, [canTurnOnOff, entity, homeAssistant.client]);

const mdiIcon = useMemo<string>(() => {
if (!entity) return DEFAULT_DOMAIN_ICON;
const domain = entity.entity_id.split(".")[0];
Expand Down Expand Up @@ -65,39 +74,6 @@ export function WidgetHomeAssistant({
return DOMAINS_TOGGLE.has(domain) && !STATES_OFF.includes(entity.state);
}, [entity]);

const state = useMemo<JSX.Element | null>(() => {
if (!entity || !data.showState) return null;
const domain = entity.entity_id.split(".")[0];

switch (domain) {
case "camera":
return (
<WidgetImage
data={{
url: `${homeAssistant.client?.baseUrl()}${entity.attributes?.entity_picture}`,
widgetId: data.widgetId,
}}
editing={editing}
handleInteraction={handleInteraction}
/>
);
default:
if (clickable && data.showIcon) return null;
return (
<Typography variant="body1">
{entity.state} {entity.attributes?.unit_of_measurement}
</Typography>
);
}
}, [
clickable,
data,
editing,
entity,
homeAssistant.client,
handleInteraction,
]);

const icon = useMemo<JSX.Element>(
() => (
<Icon
Expand All @@ -106,7 +82,7 @@ export function WidgetHomeAssistant({
? "rgba(255, 255, 255, 0.5)"
: entityIsOn
? `rgba(${
entity?.attributes?.rgb_color?.join(", ") || "126, 87, 194"
entity?.attributes?.rgb_color?.join(", ") || primaryColorRgb
}, ${entity?.attributes?.brightness / 255 || 1})`
: "currentColor"
}
Expand All @@ -131,6 +107,83 @@ export function WidgetHomeAssistant({
]
);

const state = useMemo<JSX.Element | null>(() => {
if (!entity || !data.showState) return null;
const domain = entity.entity_id.split(".")[0];

if (expanded && DOMAINS_WITH_ACTIVATE_CONDITION.has(domain)) {
switch (domain) {
case "cover":
return <ExpandedHomeAssistantCover entity={entity} />;
default:
return null;
}
} else {
switch (domain) {
case "camera":
return (
<WidgetImage
data={{
url: `${homeAssistant.client?.baseUrl()}${
entity.attributes?.entity_picture
}`,
widgetId: data.widgetId,
}}
editing={editing}
handleInteraction={handleInteraction}
/>
);
default:
return (
<>
{data.showIcon && mdiIcon && (
<>
{clickable ? (
<IconButton
aria-label={entity.attributes.friendly_name}
disabled={editing}
onClick={() => {
if (
canTurnOnOff &&
!DOMAINS_WITH_ACTIVATE_CONDITION.has(
entity.entity_id.split(".")[0]
)
)
homeAssistant.client?.entityTurnOnOff(
entity,
!entityIsOn
);
else handleInteraction(WidgetAction.ToggleExpanded);
}}
>
{icon}
</IconButton>
) : (
icon
)}
</>
)}
<Typography variant="body1">
{entity.state} {entity.attributes?.unit_of_measurement}
</Typography>
</>
);
}
}
}, [
canTurnOnOff,
clickable,
data,
editing,
entity,
entityIsOn,
expanded,
handleInteraction,
homeAssistant.client,
icon,
mdiIcon,
]);

return (
<>
{entity ? (
Expand All @@ -140,23 +193,6 @@ export function WidgetHomeAssistant({
{entity.attributes.friendly_name}
</Typography>
)}
{data.showIcon && mdiIcon && (
<>
{clickable ? (
<IconButton
aria-label={entity.attributes.friendly_name}
disabled={editing}
onClick={() =>
homeAssistant.client?.entityTurnOnOff(entity, !entityIsOn)
}
>
{icon}
</IconButton>
) : (
icon
)}
</>
)}
{state}
{data.secondaryInfo && (
<Typography variant="body2">
Expand Down
Loading