Skip to content

Conversation

@mik-laj
Copy link

@mik-laj mik-laj commented Sep 3, 2025

Proposed Changes

Hi. WLED allows you to set your own segment names, but unfortunately this name is not available in HA and HA generates names based on ID, which is a bit confusing. To solve this, I add name deserialization. As a next step, we can update HA to use this field.

The segment name is available at n field in API.
https://github.com/wled/WLED/blob/705f2035f4b31eb997e1d5718f30352657c7d930/wled00/json.cpp#L140-L147
https://github.com/paul-fornage/wled-json-api-library/blob/87abb06b1ae878b6c8f5ba6d4c11f9f5c9dc68a7/src/structures/state.rs#L244-L249

Related Issues

home-assistant/core#124271

(Github link to related issues or pull requests)

Summary by CodeRabbit

  • New Features

    • Segments now support optional custom names shown in the UI and API responses.
    • When no name is set, a friendly default like "Segment {ID}" is displayed.
    • Existing segments without names remain compatible; no configuration required.
    • Improves clarity when managing multiple segments in lists and views.
  • Bug Fixes

    • Empty-string names are normalized so they behave like unset names.

@coderabbitai
Copy link

coderabbitai bot commented Sep 3, 2025

Walkthrough

Adds an optional name field to the Segment dataclass in src/wled/models.py (alias "n"), plus an effective_name property that returns the user name or a UI fallback, and a __post_deserialize__ hook that normalizes empty-string names to None.

Changes

Cohort / File(s) Summary
Models: Segment additions
src/wled/models.py
Added `name: str

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch my nose and add a name,
A gentle tag for segments’ frame.
Empty strings I tuck away,
So defaults guide the bright display.
A rabbit’s hop—small change, pure glee.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 6ed2188 and a321a5d.

📒 Files selected for processing (1)
  • src/wled/models.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/wled/models.py
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • 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.
  • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

@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

🧹 Nitpick comments (2)
src/wled/models.py (2)

298-302: Tighten docstring and remove trailing whitespace

Clarify fallback wording, reference None correctly, and avoid trailing spaces to keep linters happy.

-    """The name of the segment. 
-    
-    Names are not present by default. If this is none, use 
-    "Segment{segment_id}" to match the WLED UI.
-    """
+    """User-defined segment name (alias: "n").
+
+    Not present by default. If None or empty, callers may fall back to a UI-like
+    default, e.g., "Segment {segment_id}".
+    """

297-303: Optional: expose a computed fallback to reduce duplication downstream

Consider providing a convenience property (and optional normalization) so consumers don’t have to replicate fallback logic.

Example (outside the changed hunk):

# Inside class Segment
@property
def effective_name(self) -> str:
    if self.name:
        return self.name
    if self.segment_id is not None:
        return f"Segment {self.segment_id}"
    return "Segment"

@classmethod
def __post_deserialize__(cls, obj: "Segment") -> "Segment":
    # Normalize empty string to None for consistency
    if obj.name == "":
        obj.name = None
    return obj
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 0bdb185 and 6ed2188.

📒 Files selected for processing (1)
  • src/wled/models.py (1 hunks)
🔇 Additional comments (1)
src/wled/models.py (1)

297-303: Addition of name (alias "n") looks correct

Matches WLED’s segment JSON field and will omit on serialization when None, which is desirable. Nice, minimal change.

@mik-laj
Copy link
Author

mik-laj commented Sep 22, 2025

@coderabbitai I pushed a fix, please review it.

@mik-laj
Copy link
Author

mik-laj commented Sep 22, 2025

I checked that this change is correct at the real device.

At the beginning, I didn't have any segments name

(wled-py3.12) vscode ➜ /workspaces/python-wled (segment-name) $ python test_segments.py
Device:
Segment 0:
  Name: None
  Effective name: Segment 0
Segment 1:
  Name: None
  Effective name: Segment 1

Then I set a name for two segments:

(wled-py3.12) vscode ➜ /workspaces/python-wled (segment-name) $ python test_segments.py
Device:
Segment 0:
  Name: Left window
  Effective name: Left window
Segment 1:
  Name: Right window
  Effective name: Right window

And later I deleted the name of the segment:

(wled-py3.12) vscode ➜ /workspaces/python-wled (segment-name) $ python test_segments.py
Device:
Segment 0:
  Name: Left window
  Effective name: Left window
Segment 1:
  Name: None
  Effective name: Segment 1

@github-actions
Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Oct 23, 2025
@mik-laj
Copy link
Author

mik-laj commented Oct 23, 2025

This is waiting for review from the project maintainer.

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant