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

Add conversion to reuse wifi_strength in 'system' #487

Merged
merged 1 commit into from
Jan 28, 2025
Merged

Conversation

DCSBL
Copy link
Collaborator

@DCSBL DCSBL commented Jan 28, 2025

Move measurement.wifi_strength to system.wifi_strength, and add a conversion for wifi_rssi_db to wifi_strength.

Summary by CodeRabbit

  • New Features

    • Added Wi-Fi signal strength percentage tracking for system devices
    • Introduced a method to calculate Wi-Fi signal strength percentage based on RSSI value
  • Improvements

    • Enhanced system model to provide more accurate Wi-Fi signal strength representation
    • Updated test cases to reflect new Wi-Fi strength percentage attribute
  • Changes

    • Replaced wifi_ssid with wifi_strength_pct in system model
    • Added calculation method for converting RSSI to percentage signal strength

Copy link
Contributor

coderabbitai bot commented Jan 28, 2025

Walkthrough

The pull request introduces a new attribute wifi_strength_pct to the System class in the homewizard_energy/models.py file. This attribute represents the Wi-Fi signal strength as a percentage, calculated based on the existing wifi_rssi_db value. The changes are implemented across multiple test snapshot files for both v1 and v2 versions, updating the System class constructor and test cases to incorporate the new wifi_strength_pct attribute.

Changes

File Change Summary
homewizard_energy/models.py - Added `wifi_strength_pct: int
tests/v1/__snapshots__/test_v1_homewizard_energy.ambr
tests/v1/__snapshots__/test_v1_models.ambr
tests/v2/__snapshots__/test_v2_homewizard_energy.ambr
tests/v2/__snapshots__/test_v2_models.ambr
- Updated System class constructor to include wifi_strength_pct
- Modified test snapshots to reflect new wifi_strength_pct attribute

Sequence Diagram

sequenceDiagram
    participant Model as System Model
    participant Deserializer as __post_deserialize__
    participant RSSI as wifi_rssi_db

    RSSI->>Deserializer: Provide RSSI value
    Deserializer->>Model: Calculate wifi_strength_pct
    alt RSSI <= -100 or RSSI = 0
        Deserializer-->>Model: Set wifi_strength_pct = 0
    else RSSI >= -50
        Deserializer-->>Model: Set wifi_strength_pct = 100
    else
        Deserializer-->>Model: Calculate percentage based on RSSI
    end
Loading

Possibly related PRs

Suggested labels

breaking-change

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 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.

@DCSBL DCSBL enabled auto-merge (squash) January 28, 2025 20:58
Copy link

codecov bot commented Jan 28, 2025

Codecov Report

Attention: Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Project coverage is 99.13%. Comparing base (c862f13) to head (385338a).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
homewizard_energy/models.py 81.81% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #487      +/-   ##
==========================================
- Coverage   99.46%   99.13%   -0.34%     
==========================================
  Files           9        9              
  Lines         565      576      +11     
  Branches       41       44       +3     
==========================================
+ Hits          562      571       +9     
- Misses          2        3       +1     
- Partials        1        2       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@DCSBL DCSBL merged commit a685666 into main Jan 28, 2025
30 of 32 checks passed
@DCSBL DCSBL deleted the DCSBL-2025-57 branch January 28, 2025 21:00
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 (3)
tests/v1/__snapshots__/test_v1_homewizard_energy.ambr (1)

150-150: Consider adding test cases for wifi_rssi_db conversion.

While the test snapshots cover the wifi_strength_pct field, they all have wifi_rssi_db set to None. Consider adding test cases that verify the conversion from RSSI values to strength percentages.

Also applies to: 153-153, 156-156, 159-159, 162-162, 165-165, 168-168

homewizard_energy/models.py (2)

549-550: Consider maintaining consistent attribute ordering.

The new wifi_strength_pct attribute should be placed in alphabetical order with other attributes in the System class to maintain consistency.

Apply this diff to reorder the attributes:

@dataclass(kw_only=True)
class System(BaseModel):
    """Represent System config."""

-    wifi_strength_pct: int | None = None
-
    wifi_ssid: str | None = field(default=None)
    wifi_rssi_db: int | None = field(default=None)
    cloud_enabled: bool | None = field(default=None)
    uptime_s: int | None = field(default=None)
    status_led_brightness_pct: int | None = field(default=None)
+    wifi_strength_pct: int | None = None
    api_v1_enabled: bool | None = field(default=None)

558-572: LGTM! Consider documenting the RSSI to percentage conversion formula.

The code correctly implements the RSSI to percentage conversion with appropriate edge cases. Consider adding a docstring to explain the conversion formula:

Apply this diff to add documentation:

    @classmethod
    def __post_deserialize__(cls, obj: System) -> System:
-        _ = cls  # Unused
+        """Post deserialize hook for System object.
+        
+        Calculates wifi_strength_pct based on wifi_rssi_db using the following formula:
+        - RSSI <= -100 dBm or RSSI == 0 -> 0% (very poor or no signal)
+        - RSSI >= -50 dBm -> 100% (excellent signal)
+        - Otherwise -> 2 * (RSSI + 100) to linearly map [-100, -50] to [0, 100]
+        """
+        _ = cls  # Unused

        if obj.wifi_rssi_db is not None:
            obj.wifi_strength_pct = (
                0
                if obj.wifi_rssi_db <= -100 or obj.wifi_rssi_db == 0
                else 100
                if obj.wifi_rssi_db >= -50
                else 2 * (obj.wifi_rssi_db + 100)
            )

        return obj
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c862f13 and 385338a.

📒 Files selected for processing (5)
  • homewizard_energy/models.py (2 hunks)
  • tests/v1/__snapshots__/test_v1_homewizard_energy.ambr (2 hunks)
  • tests/v1/__snapshots__/test_v1_models.ambr (2 hunks)
  • tests/v2/__snapshots__/test_v2_homewizard_energy.ambr (2 hunks)
  • tests/v2/__snapshots__/test_v2_models.ambr (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Scanning
🔇 Additional comments (6)
tests/v1/__snapshots__/test_v1_homewizard_energy.ambr (1)

3-3: LGTM! The wifi strength values are consistently mapped across all device types.

The test snapshots show that the wifi_strength values from Measurement are correctly mapped to wifi_strength_pct in System across all device types:

  • Single-phase meters (HWE-KWH1, SDM230-wifi): 74%
  • Three-phase meters (HWE-KWH3, SDM630-wifi): 84%
  • P1 meters, sockets, and water meters: 100%

Also applies to: 6-6, 9-9, 12-12, 15-15, 18-18, 21-21, 24-24, 27-27, 30-30, 33-33, 36-36, 39-39, 42-42

homewizard_energy/models.py (1)

88-92: LGTM! Backward compatibility is maintained.

The code correctly handles the migration of wifi_strength from measurement to system.wifi_strength_pct, ensuring backward compatibility by creating a new System instance if needed.

tests/v2/__snapshots__/test_v2_models.ambr (2)

24-24: LGTM! The RSSI to percentage conversion is correct.

The test snapshot correctly verifies that an RSSI value of -77 dBm maps to 46% signal strength.


27-27: LGTM! Test coverage is extended to different device types.

The test snapshot verifies that the RSSI to percentage conversion works consistently across different device types.

tests/v2/__snapshots__/test_v2_homewizard_energy.ambr (1)

24-24: Test snapshots are consistent with test_v2_models.ambr.

The changes in this file mirror those in test_v2_models.ambr, providing consistent test coverage.

tests/v1/__snapshots__/test_v1_models.ambr (1)

78-96: Test snapshots are consistent with v2 test files.

The changes in this file follow the same pattern as the v2 test snapshots, ensuring consistent test coverage across API versions.

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