Skip to content

Commit

Permalink
Merge branch 'release/3.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonhochkins committed Feb 15, 2024
2 parents c79dce3 + 1583333 commit e1ba27b
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 542 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

# 3.1.3
## @hakit/components
- No changes here - aligning semantic versions

## @hakit/core
- BUGFIX - entities that have a custom icon defined in HA will now show and take priority over icons derived from the state of the entity
- BUGFIX - previously, once the icon was set for an entity, it would never update, this has been fixed and will now update when the entity state changes thanks to @Brewno88 for reporting this one

# 3.1.2
## @hakit/components
- Licence.md file updated
Expand Down
4 changes: 2 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hakit/components",
"type": "module",
"version": "3.1.2",
"version": "3.1.3",
"private": false,
"keywords": [
"react",
Expand Down Expand Up @@ -68,7 +68,7 @@
"@emotion/react": ">=11.x",
"@emotion/styled": ">=11.x",
"@fullcalendar/react": ">=6.x.x",
"@hakit/core": "^3.1.2",
"@hakit/core": "^3.1.3",
"@use-gesture/react": ">=10.x",
"autolinker": ">=4.x",
"framer-motion": ">=10.x",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hakit/core",
"version": "3.1.2",
"version": "3.1.3",
"private": false,
"type": "module",
"keywords": [
Expand Down
34 changes: 26 additions & 8 deletions packages/core/src/hooks/useEntity/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useState, useCallback } from "react";
import { isEmpty, omit } from "lodash";
import { cloneDeep, isEmpty, omit } from "lodash";
import type { HassEntityWithService, HassEntityCustom, ExtractDomain, EntityName } from "@typings";
import type { HassEntity } from "home-assistant-js-websocket";
import { useService, useHistory, useSubscribeEntity, getIconByEntity } from "@core";
Expand Down Expand Up @@ -57,9 +57,6 @@ export function useEntity<E extends EntityName, O extends UseEntityOptions = Use
const timeDiff = Math.abs(now.getTime() - then.getTime());
const active = relativeTime === "just now";
const { hexColor, rgbColor, brightness, brightnessValue, rgbaColor, color } = getCssColorValue(entity);
if (!entity.attributes.icon) {
entity.attributes.icon = getIconByEntity(computeDomain(entity.entity_id as EntityName), entity);
}
return {
...entity,
custom: {
Expand Down Expand Up @@ -87,12 +84,33 @@ export function useEntity<E extends EntityName, O extends UseEntityOptions = Use
useEffect(() => {
const foundEntity = getEntity(true);
if (foundEntity && $entity) {
// have to omit attributes.icon here as the original icon may not contain any icon,
// however there's custom functionality to determine icon based on state which needs to be omitted from
// this check to avoid recursive updates
const diffed = diff(
omit(foundEntity, "custom", "last_changed", "last_updated", "context"),
omit($entity, "custom", "last_changed", "last_updated", "context"),
omit(foundEntity, "custom", "last_changed", "last_updated", "context", "attributes.icon"),
omit($entity, "custom", "last_changed", "last_updated", "context", "attributes.icon"),
);
if (!isEmpty(diffed)) {
debounceUpdate(foundEntity);
const clonedEntity = cloneDeep(foundEntity);
// Check for icon differences
const haHasCustomIcon = typeof clonedEntity.attributes.icon === "string";
const derivedIcon = typeof $entity.attributes.icon === "string";
// Logic for handling icon comparison and updates
let shouldUpdate = !isEmpty(diffed);
if (haHasCustomIcon && derivedIcon && clonedEntity.attributes.icon !== $entity.attributes.icon) {
// Condition 1: Both icons are strings and differ
shouldUpdate = true;
} else if (!haHasCustomIcon) {
// Condition 2: clonedEntity's icon is not a string, compute and compare
const currentIcon = getIconByEntity(computeDomain(clonedEntity.entity_id as EntityName), clonedEntity);
if (currentIcon !== $entity.attributes.icon) {
// Replace clonedEntity's icon with the computed icon and mark for update
clonedEntity.attributes.icon = currentIcon;
shouldUpdate = true;
}
}
if (shouldUpdate) {
debounceUpdate(clonedEntity);
}
}
}, [$entity, debounceUpdate, getEntity]);
Expand Down
Loading

0 comments on commit e1ba27b

Please sign in to comment.