Skip to content

Feat: Added new state attributes to provide day of week information#103

Merged
olibos merged 5 commits intoolibos:mainfrom
geertmeersman:weekday_id
Jan 13, 2025
Merged

Feat: Added new state attributes to provide day of week information#103
olibos merged 5 commits intoolibos:mainfrom
geertmeersman:weekday_id

Conversation

@geertmeersman
Copy link
Contributor

@geertmeersman geertmeersman commented Jan 12, 2025

Interesting for auto sorting

Summary by CodeRabbit

  • Improvements
    • Enhanced day of week tracking in opening hours functionality.
    • Streamlined logic for determining the current day.
    • Added new state attributes to provide more detailed information about opening hours.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 12, 2025

Walkthrough

The changes modify the OpeningHoursEntity class in the recycle app component by introducing a new _day_of_week_index attribute. This attribute calculates the day of the week index using a specific computation method. The update streamlines the day-of-week handling logic by removing a local variable and directly using the new attribute in day comparison operations.

Changes

File Change Summary
custom_components/recycle_app/opening_hours_entity.py - Added _day_of_week_index attribute
- Modified __update_native_value method to use new day index
- Introduced _attr_extra_state_attributes to store day of week information

Poem

🐰 A Rabbit's Ode to Day Indexing 🗓️

Weeks spin 'round like a carousel bright,
Indexes dancing with algorithmic might,
One plus zero, modulo seven's delight,
Code flows smooth, no redundant fight,
Recycling days with computational grace!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f25b0a and db3408c.

📒 Files selected for processing (1)
  • custom_components/recycle_app/opening_hours_entity.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • custom_components/recycle_app/opening_hours_entity.py

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>, please review it.
    • 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 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 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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

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: 0

🧹 Nitpick comments (1)
custom_components/recycle_app/opening_hours_entity.py (1)

45-46: LGTM! Consider these minor improvements.

The implementation successfully adds the day of week index attribute. A few suggestions for improvement:

  1. The modulo operation is redundant since DAYS_OF_WEEK.index() will always return 0-6
  2. Consider using snake_case for the attribute name (e.g., "day_of_week")
-        self._day_of_week_index = (DAYS_OF_WEEK.index(day_of_week) + 1) % 7
-        self._attr_extra_state_attributes = {"day of week": self._day_of_week_index}
+        self._day_of_week_index = DAYS_OF_WEEK.index(day_of_week) + 1
+        self._attr_extra_state_attributes = {"day_of_week": self._day_of_week_index}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c48b4fe and 6707f20.

📒 Files selected for processing (1)
  • custom_components/recycle_app/opening_hours_entity.py (2 hunks)
🔇 Additional comments (1)
custom_components/recycle_app/opening_hours_entity.py (1)

67-67: Verify the day index comparison logic.

The comparison looks correct but let's verify the index matching:

  • _day_of_week_index is 1-based (Monday=1, ..., Sunday=7)
  • Need to confirm if opening_day["day"] from the API uses the same 1-based indexing
✅ Verification successful

Day index comparison is correctly implemented

The comparison is using a consistent 1-based day indexing scheme (Monday=1 through Sunday=7) on both sides:

  • _day_of_week_index is properly converted to 1-based indexing
  • opening_day["day"] from the API uses the same 1-based scheme, as verified by consistent usage in multiple components
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any documentation or tests that might reveal the API's day indexing scheme
rg -l "openingDays.*day" | xargs rg "day.*[0-9]"

Length of output: 583


Script:

#!/bin/bash
# Search for DAYS_OF_WEEK definition and API response handling
rg "DAYS_OF_WEEK.*=" -A 3
rg -B 2 -A 5 "opening.*day.*\[.*day" 

Length of output: 1871

@geertmeersman geertmeersman changed the title Feat: add week of day index attribute to opening hours Feat: Added new state attributes to provide day of week information Jan 12, 2025
@olibos
Copy link
Owner

olibos commented Jan 13, 2025

Hello @geertmeersman,

Great addition to the integration!

I’m curious about the removal of the modulo operation. With the updated code, _day_of_week_index will range from 1 to 7, whereas the API’s openingDays uses day values between 0 and 6.

Am I overlooking something?

@olibos olibos added the enhancement New feature or request label Jan 13, 2025
@olibos
Copy link
Owner

olibos commented Jan 13, 2025

It breaks on my side:
image

I think we should keep the modulo for the link logic with API even if we use a 1 (Monday) → 7 (Sunday) day number for the attribute.

@geertmeersman
Copy link
Contributor Author

Seems to work correctly

image
image

2025-01-13 10:32:06.207 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]
2025-01-13 10:32:06.207 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]
2025-01-13 10:32:06.207 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] Tuesday | 2 - ['13:00 - 17:45']
2025-01-13 10:32:06.207 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]
2025-01-13 10:32:06.207 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] Wednesday | 3 - ['13:00 - 17:45']
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] Thursday | 4 - ['13:00 - 17:45']
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] Friday | 5 - ['13:00 - 17:45']
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] Saturday | 6 - ['09:00 - 16:15']
2025-01-13 10:32:06.208 CRITICAL (MainThread) [custom_components.recycle_app.opening_hours_entity] [{'openingHours': [{'_id': '6749c841b06456006dc73e3e', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3d', 'day': 2}, {'openingHours': [{'_id': '6749c841b06456006dc73e40', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e3f', 'day': 3}, {'openingHours': [{'_id': '6749c841b06456006dc73e42', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e41', 'day': 4}, {'openingHours': [{'_id': '6749c841b06456006dc73e44', 'from': '13:00', 'until': '17:45'}], '_id': '6749c841b06456006dc73e43', 'day': 5}, {'openingHours': [{'_id': '6749c841b06456006dc73e46', 'from': '09:00', 'until': '16:15'}], '_id': '6749c841b06456006dc73e45', 'day': 6}]

@olibos
Copy link
Owner

olibos commented Jan 13, 2025

{
"openingPeriods": [
                {
                    "openingDays": [
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bcd",
                                    "from": "14:00",
                                    "until": "20:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bcc",
                            "day": 1
                        },
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bcf",
                                    "from": "09:00",
                                    "until": "20:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bce",
                            "day": 2
                        },
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bd1",
                                    "from": "09:00",
                                    "until": "20:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bd0",
                            "day": 3
                        },
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bd3",
                                    "from": "09:00",
                                    "until": "16:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bd2",
                            "day": 4
                        },
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bd5",
                                    "from": "09:00",
                                    "until": "16:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bd4",
                            "day": 5
                        },
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bd7",
                                    "from": "09:00",
                                    "until": "16:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bd6",
                            "day": 6
                        },
                        {
                            "openingHours": [
                                {
                                    "_id": "676171b6328d750070a32bd9",
                                    "from": "14:00",
                                    "until": "20:15"
                                }
                            ],
                            "_id": "676171b6328d750070a32bd8",
                            "day": 0
                        }
                    ],
                    "_id": "676171b6328d750070a32bcb",
                    "name": "2020",
                    "from": "2020-01-01T00:00:00.000Z",
                    "until": "2030-12-31T00:00:00.000Z"
                }
            ],
            "displayName": {
                "nl": "Recypark ZUID",
                "fr": "Recypark SUD",
                "en": "Recypark SUD",
                "de": "Recypark SUD"
            },
            "targets": []
        }
    ]
}

@olibos
Copy link
Owner

olibos commented Jan 13, 2025

It works if the recycling parks is closed on sunday 😜

@geertmeersman
Copy link
Contributor Author

Already see the issue, had it different in my running HA instance :-)

@olibos
Copy link
Owner

olibos commented Jan 13, 2025

Fixed. Finally, we keep the attribute value with the US day of the week: 0 = Sunday, 6 = Saturday.
Is this intended? We can still change it since it’s not released yet.

For me, both the US day of the week (Sunday → Saturday) or the Belgian day of the week (Monday → Sunday) are acceptable.

@geertmeersman
Copy link
Contributor Author

Changed

@sonarqubecloud
Copy link

Copy link
Owner

@olibos olibos left a comment

Choose a reason for hiding this comment

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

Great, thank you!

@olibos olibos merged commit a921085 into olibos:main Jan 13, 2025
@geertmeersman geertmeersman deleted the weekday_id branch January 13, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants