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

picture cards: add person image support #20593

Merged
merged 11 commits into from
Jul 23, 2024

Conversation

Quentame
Copy link
Member

@Quentame Quentame commented Apr 23, 2024

Proposed change

Add person profile picture support for picture cards, this avoid finding ths profile picture URL in Web Dev Tools, and doing so every time the profile pic is changed/updated.

Picture Card
image

Picture Elements Card
image

Picture Entity Card
image

Picture Glance Card
image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Already in gallery/src/pages/lovelace files

Additional information

Checklist

  • The code change is tested and works locally. (Tested with Gallery, not as actual HA frontend)
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced support for "person" entities, allowing for the display of person-related data such as user pictures and GPS coordinates in various Lovelace picture cards.
  • UI Enhancements

    • Updated picture elements, picture entity, and picture glance cards to handle and display person entities.
    • Added new configuration options to support person entities in Lovelace card editors.
  • Configuration

    • Enhanced Lovelace config to include person entities, improving customization and user experience.
  • Translations

    • Added new translation entry for "Person entity" to enhance user accessibility.

@github-actions github-actions bot added the Design Related to Home Assistant design gallery label Apr 23, 2024
Copy link
Contributor

coderabbitai bot commented May 31, 2024

Walkthrough

Walkthrough

The changes enhance the representation of "Paulus" and a battery sensor across various Lovelace cards by adding new entity configurations and refining the handling of person entities. The introduction of PersonEntity and PersonEntityAttributes interfaces improves the management of person-related data. Modifications to image handling logic ensure compatibility with the new entity type, while updates in card editors and translations facilitate a seamless integration throughout the system.

Changes

File Path Summary
gallery/src/pages/lovelace/picture-elements-card.ts, gallery/src/pages/lovelace/picture-entity-card.ts, gallery/src/pages/lovelace/picture-glance-card.ts Added new entities for "Paulus" and a battery sensor, updating ENTITIES and CONFIGS for display in various card types.
src/data/person.ts Introduced PersonEntity and PersonEntityAttributes interfaces for structured handling of person-related data.
src/panels/lovelace/cards/hui-picture-elements-card.ts, src/panels/lovelace/cards/hui-picture-entity-card.ts, src/panels/lovelace/cards/hui-picture-glance-card.ts Updated to handle PersonEntity, adjusting image rendering logic and accommodating new configurations.
src/panels/lovelace/cards/types.ts Added image_entity property to the PictureGlanceCardConfig interface, allowing broader configuration options.
src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts, src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts Modified schema to accept image_entity from any domain, enhancing card editor flexibility.
src/translations/en.json Added translation entry "person_entity": "Person entity".

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 75c1632 and 0eb8b77.

Files selected for processing (4)
  • src/panels/lovelace/cards/hui-picture-card.ts (4 hunks)
  • src/panels/lovelace/cards/hui-picture-elements-card.ts (3 hunks)
  • src/panels/lovelace/cards/hui-picture-entity-card.ts (4 hunks)
  • src/panels/lovelace/cards/hui-picture-glance-card.ts (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/panels/lovelace/cards/hui-picture-card.ts
  • src/panels/lovelace/cards/hui-picture-entity-card.ts
Additional comments not posted (7)
src/panels/lovelace/cards/hui-picture-elements-card.ts (4)

11-11: New import for computeDomain looks good.

The import is necessary for determining the domain of the entity.


20-20: New import for PersonEntity looks good.

The import is necessary for handling person-related data.


143-143: LGTM!

The changes correctly integrate the new image variable.


121-135: LGTM! But verify the function usage in the codebase.

The code changes are approved. The logic correctly handles both ImageEntity and PersonEntity.

However, ensure that all function calls to render match the new logic.

src/panels/lovelace/cards/hui-picture-glance-card.ts (3)

34-34: New import for PersonEntity looks good.

The import is necessary for handling person-related data.


225-225: LGTM!

The changes correctly integrate the new image variable.


187-201: LGTM! But verify the function usage in the codebase.

The code changes are approved. The logic correctly handles both ImageEntity and PersonEntity.

However, ensure that all function calls to render match the new logic.

Verification successful

Verified: No issues found with the function usage.

The render function is defined within hui-picture-glance-card.ts, and there are no direct calls to it within the specified directory or the file itself. The logic within the function is self-contained and does not impact other parts of the codebase directly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `render` match the new logic.

# Test: Search for the function usage. Expect: Only occurrences of the new logic.
rg --type js -A 5 $'render'

Length of output: 2097


Script:

#!/bin/bash
# Description: Verify all function calls to `render` within the src/panels/lovelace/cards directory.

# Test: Search for the function usage within the specific directory.
rg --type js -A 5 'render' src/panels/lovelace/cards

Length of output: 52


Script:

#!/bin/bash
# Description: Verify the definition and usage of the `render` function within hui-picture-glance-card.ts and its parent classes.

# Test: Search for the definition of the `render` function within hui-picture-glance-card.ts.
rg --type js 'render' src/panels/lovelace/cards/hui-picture-glance-card.ts

# Test: If the `render` function is not defined within hui-picture-glance-card.ts, search for its parent class and check for the `render` function definition.
parent_class=$(rg --type js 'extends' src/panels/lovelace/cards/hui-picture-glance-card.ts | awk '{print $3}')
if [ -n "$parent_class" ]; then
  rg --type js 'render' $(fd "$parent_class.js")
fi

Length of output: 808


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

src/panels/lovelace/cards/hui-picture-entity-card.ts Outdated Show resolved Hide resolved
@Quentame Quentame force-pushed the picture-card/add_person_support branch from 73193a2 to 86b38ba Compare June 14, 2024 17:29
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range comments (11)
gallery/src/pages/lovelace/picture-elements-card.ts (1)

Line range hint 22-22: Detected a potential security issue with an API key exposed in the code. Consider using environment variables or secure storage mechanisms to handle sensitive keys.

- access_token: "2f5bb163fb91cd8770a9494fa5e7eab172d8d34f4aba806eb6b59411b8c720b8",
+ access_token: process.env.DEMO_CAMERA_ACCESS_TOKEN,
src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (1)

Line range hint 84-84: Replace non-null assertions with optional chaining to prevent runtime errors.

- this.hass!.localize(...)
+ this.hass?.localize(...)

Also applies to: 88-88, 124-124, 137-137, 139-139, 143-143, 147-147

src/panels/lovelace/cards/hui-picture-elements-card.ts (2)

Line range hint 66-79: Remove unnecessary else clauses to simplify control flow.

- } else if (condition) {
+ } if (condition) {

Also applies to: 77-79


Line range hint 201-201: Replace non-null assertion with optional chaining to prevent runtime errors.

- this._elements!
+ this._elements?
src/panels/lovelace/cards/hui-picture-entity-card.ts (1)

Line range hint 163-163: Replace non-null assertions with optional chaining to prevent runtime errors.

- this.hass!
+ this.hass?

Also applies to: 164-164, 226-226, 226-226, 226-226

src/panels/lovelace/cards/hui-picture-glance-card.ts (1)

Line range hint 105-105: Address multiple instances of forbidden non-null assertions and excessive complexity detected in the code.

- this.hass!
+ this.hass?
- handleAction(this, this.hass!, config, ev.detail.action!);
+ handleAction(this, this.hass?, config, ev.detail.action);

Also, consider refactoring the function to reduce its complexity.

Also applies to: 107-107, 117-117, 211-211, 212-212, 236-236, 241-241, 254-254, 265-265, 297-297, 315-315, 315-315, 316-316, 316-316

src/panels/lovelace/common/generate-lovelace-config.ts (5)

Line range hint 65-65: Refactor to reduce cognitive complexity.

The complexity of this function exceeds the recommended limit, potentially making maintenance and future updates more challenging.

Consider breaking down the function into smaller, more manageable functions or simplifying the logic where possible.


Line range hint 223-226: Avoid assignments within expressions.

Assignments within expressions can lead to code that is harder to read and maintain.

Consider revising these lines to separate the condition check from the assignment for clearer logic.

Also applies to: 237-240


Line range hint 130-130: Address excessive function complexity.

Several functions in this file exceed the recommended complexity limits, which could hinder understanding and maintenance.

Refactor these functions to simplify the logic, possibly by extracting parts of the code into separate functions or reducing the number of conditional branches.

Also applies to: 251-251, 349-349, 491-491


Line range hint 419-419: Optimize by avoiding the delete operator.

Using delete can lead to performance issues due to deoptimization of the underlying data structures in JavaScript engines.

- delete allEntities[entity.entity_id];
+ allEntities[entity.entity_id] = undefined;

This change avoids the performance penalty associated with the delete operator.


Line range hint 620-620: Replace non-null assertions with optional chaining.

Non-null assertions can lead to runtime errors if the assumptions about data presence are incorrect.
[REFACTOR_Suggestion]

- const grid = energyPrefs.energy_sources.find(source => source.type === "grid") as GridSourceTypeEnergyPreference;
+ const grid = energyPrefs.energy_sources.find(source => source.type === "grid")?. as GridSourceTypeEnergyPreference;

This change adds a safety check to ensure that the object exists before attempting to access its properties.

Also applies to: 626-626

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

export interface PersonEntity extends HassEntityBase {
attributes: PersonEntityAttributes;
}

Copy link
Contributor

@coderabbitai coderabbitai bot Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the attributes field from fetchPersons is properly handled in all calling contexts.

Would you like me to help implement or verify this handling?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Quentame, it appears that the attributes field from the PersonEntity is not directly referenced or manipulated in the ha-config-person.ts file or other related components in the src/panels/config/person/ directory. This suggests that the handling of the attributes field might be abstracted elsewhere or not required in the current context of the fetchPersons usage.

If there are specific functionalities or components where you expect the attributes field to be used, please let me know, and I can further investigate those areas. Otherwise, the current implementation in ha-config-person.ts seems to focus primarily on sorting and managing person data without directly interacting with the detailed attributes of PersonEntity.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Find all files that call `fetchPersons` and display the relevant code snippets.
rg "fetchPersons" --context 5

Length of output: 2122



Script:

#!/bin/bash
# Search for direct references to the `attributes` field in `ha-config-person.ts` and other related components.
rg "attributes" --files-with-matches src/panels/config/person/

Length of output: 62

Copy link
Contributor

@silamon silamon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution. 👍

Could you refactor person_entity into image_entity so that image_entity can be either a person or a image entity in the picture element card and picture elements card? That would align it with the picture card where entity is either a camera, image or person entity and be clear to the user that setting image_entity will set the background of the card to the image of the entity regardless if it's a person or image entity.

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft June 18, 2024 16:52
@Quentame
Copy link
Member Author

@silamon { name: "image_entity", selector: { entity: { domain: "image" } } }, => Could we set domain to allow only image and person domains ?

@piitaya
Copy link
Member

piitaya commented Jul 22, 2024

@Quentame, you can provide multiple domains as a list (https://www.home-assistant.io/docs/blueprint/selectors/#entity-selector). Not sure if it works without the filter key.

@Quentame Quentame force-pushed the picture-card/add_person_support branch from bf44289 to b38d0f8 Compare July 22, 2024 08:31
@Quentame
Copy link
Member Author

@Quentame, you can provide multiple domains as a list (https://www.home-assistant.io/docs/blueprint/selectors/#entity-selector). Not sure if it works without the filter key.

OK, thanks !

As my dev env is not ready to test that, I am leaving it as at the moment.

@Quentame Quentame marked this pull request as ready for review July 22, 2024 08:58
@home-assistant home-assistant bot requested a review from silamon July 22, 2024 08:58
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Copy link
Contributor

@silamon silamon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested the code below, should be good to add:

image = computeImageUrl(stateObj as ImageEntity);
break;
case "person":
image = (stateObj as PersonEntity).attributes.entity_picture;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, picture entity cards are already used for person entities. The default generated lovelace config adds picture entity cards for every known person. If that person entity doesn't have a picture, entity_picture is undefined and it should fallback to the this._config.image.

Suggested change
image = (stateObj as PersonEntity).attributes.entity_picture;
if ((stateObj as PersonEntity).attributes.entity_picture) {
image = (stateObj as PersonEntity).attributes.entity_picture;
}

Copy link
Member Author

@Quentame Quentame Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 349b116 0eb8b77 (Ups 😄)

@Quentame
Copy link
Member Author

Doc updated to latest changes: home-assistant/home-assistant.io#32733

Copy link
Contributor

@silamon silamon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for working on this!

@piitaya piitaya merged commit 87ba0e7 into home-assistant:dev Jul 23, 2024
13 checks passed
@Quentame Quentame deleted the picture-card/add_person_support branch July 23, 2024 12:03
@danielnil
Copy link

danielnil commented Aug 18, 2024

@Quentame Is there a way to diable this fuctionality, the person image does not support "moving" GIFs, my picture entity card used to be a looping gif, now its just a static image.

Update:
I figured out that if i removed the image from the person i was able to use a custom image for the picture entity card. Hovever, it would still be nice to be able to do this without having to remove the image from the user/person.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Design Related to Home Assistant design gallery Documentation Added Feature PRs that are adding a new feature frontend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants