Skip to content

Add monthly energy sensors#568

Merged
jwillemsen merged 7 commits intomasterfrom
jwi-monthlysensor
Dec 5, 2025
Merged

Add monthly energy sensors#568
jwillemsen merged 7 commits intomasterfrom
jwi-monthlysensor

Conversation

@jwillemsen
Copy link
Copy Markdown
Owner

@jwillemsen jwillemsen commented Dec 5, 2025

Summary by CodeRabbit

  • New Features

    • Added four monthly energy consumption sensors (cooling/heating — electrical and gas).
    • Extended period tracking to include monthly measurement intervals.
  • Documentation

    • Added English and Dutch translations for the new monthly consumption sensors.

✏️ Tip: You can customize this high-level summary in your review settings.

    * custom_components/daikin_onecta/const.py:
    * custom_components/daikin_onecta/sensor.py:
    * custom_components/daikin_onecta/const.py:
    * custom_components/daikin_onecta/sensor.py:
    * custom_components/daikin_onecta/translations/en.json:
    * custom_components/daikin_onecta/translations/nl.json:
    * tests/snapshots/test_init.ambr:
@jwillemsen jwillemsen linked an issue Dec 5, 2025 that may be closed by this pull request
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 5, 2025

Warning

Rate limit exceeded

@jwillemsen has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 47 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 3cfa304 and ada385c.

📒 Files selected for processing (2)
  • custom_components/daikin_onecta/const.py (5 hunks)
  • custom_components/daikin_onecta/sensor.py (4 hunks)

Walkthrough

Adds a new "monthly" energy period and four monthly energy sensor entries, updates sensor value extraction to index into period arrays (weekly/monthly/other variants), and adds English and Dutch translations for the new sensors. The new monthly period constant value is misspelled as "montlhly".

Changes

Cohort / File(s) Summary
Constants & Mappings
custom_components/daikin_onecta/const.py
Introduced SENSOR_PERIOD_MONTHLY (value "montlhly" — misspelled), added it to SENSOR_PERIODS and SENSOR_PERIODS_ARRAY, and added four new entries in VALUE_SENSOR_MAPPING: CoolingMonthlyElectricalConsumption, HeatingMonthlyElectricalConsumption, CoolingMonthlyGasConsumption, HeatingMonthlyGasConsumption (ENERGY device class, TOTAL_INCREASING, kWh, icons, enabled by default, translation keys).
Sensor logic
custom_components/daikin_onecta/sensor.py
Extended energy sensor handling and value extraction to use SENSOR_PERIODS_ARRAY[self._period], added per-period index logic: weekly uses start=7, monthly uses start=11+current_month and end=start+1, others use start=12; creates monthly sensor entry when handling yearly period and appends monthly-period sensor instances.
Translations
custom_components/daikin_onecta/translations/en.json
custom_components/daikin_onecta/translations/nl.json
Added four translation keys under entity.sensor for the new monthly sensors in English and Dutch. (Note: English file includes heatingmonthlyconsumption key name in summary — translations added for monthly gas/electric variants as stated.)
Manifest
manifest.json
Listed in summary as touched (no logic details provided).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review const.py for the SENSOR_PERIOD_MONTHLY spelling and consistency across mappings and translation keys.
  • Inspect sensor.py index arithmetic and slicing logic for monthly/yearly/weekly to ensure correct bounds and off-by-one handling.
  • Verify translation keys in en.json and nl.json match the mapping keys in VALUE_SENSOR_MAPPING.

Possibly related PRs

Poem

🐇 I nibbled logs by moonlit light,

monthly numbers jumping bright,
watts and gas in tidy rows,
month by month the ledger grows,
hop — the sensors hum tonight.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add monthly energy sensors' directly and concisely summarizes the main change: adding new monthly sensor entries for energy consumption tracking.

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.72%. Comparing base (01b56e3) to head (ada385c).
⚠️ Report is 23 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #568      +/-   ##
==========================================
+ Coverage   96.68%   96.72%   +0.03%     
==========================================
  Files          16       16              
  Lines        1719     1738      +19     
==========================================
+ Hits         1662     1681      +19     
  Misses         57       57              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
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

🧹 Nitpick comments (2)
custom_components/daikin_onecta/sensor.py (2)

40-77: Clarify yearly→monthly sensor creation path

The extra block for period == SENSOR_PERIOD_YEARLY creates a second sensor with period rewritten to SENSOR_PERIOD_MONTHLY while still relying on the same underlying period data ("m" via SENSOR_PERIODS_ARRAY). Functionally this gives you both yearly and monthly sensors from the same source, which seems intended, but the reuse/mutation of period is a bit opaque.

Consider assigning to a separate local (e.g. monthly_period = SENSOR_PERIOD_MONTHLY) instead of mutating period, and perhaps add a brief comment that the backend only exposes "m" and you fan out to both yearly and monthly sensors in HA.

Please confirm this behavior matches the Daikin API semantics (single "m" bucket providing both yearly aggregate and per‑month values) to avoid surprising duplication.


220-247: Verify monthly index math and guard against unexpected array layouts

The new logic

  • reuses SENSOR_PERIODS_ARRAY[self._period] so both yearly and monthly sensors read from the "m" array, and
  • for monthly uses start_index = 11 + date.today().month / end_index = start_index + 1.

This assumes:

  • the "m" array exists for all created sensors, and
  • it has at least 11 + month entries (so indices 12–23 for months 1–12), otherwise energy_values[start_index:end_index] will be empty and monthly consumption will silently be 0.

If that layout is guaranteed by the API, this is fine; if not, consider:

  • Adding a simple bounds check before slicing (logging and leaving energy_value as None if the slice would be empty), and/or
  • Documenting the assumed array structure (e.g. “positions 12–23 are months 1–12”).

Also, if the backend month indexing is 0‑based rather than 1‑based, this could be off by one; worth confirming against real payloads.

Please validate the "m" array shape and month index mapping with a couple of live responses (e.g. print len(energy_values) and sample contents) to ensure the current‑month element is correctly addressed.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 01b56e3 and be8ff0b.

📒 Files selected for processing (4)
  • custom_components/daikin_onecta/const.py (5 hunks)
  • custom_components/daikin_onecta/sensor.py (4 hunks)
  • custom_components/daikin_onecta/translations/en.json (4 hunks)
  • custom_components/daikin_onecta/translations/nl.json (4 hunks)
🔇 Additional comments (5)
custom_components/daikin_onecta/sensor.py (1)

3-3: New imports for date and period constants look consistent

date and the new period-related imports are all used in sensor_value and handle_energy_sensors; no issues spotted.

Also applies to: 18-23

custom_components/daikin_onecta/const.py (2)

29-44: Monthly period constant and array mapping are internally consistent

Defining SENSOR_PERIOD_MONTHLY = "mo" and mapping both yearly and monthly to "m" in SENSOR_PERIODS_ARRAY cleanly separates HA’s notion of periods from the backend’s "m" bucket while keeping lookups simple. No issues from a constants/layout perspective.

Please double‑check against the Daikin API docs/sample payloads that "m" is indeed the correct key for both yearly aggregate and per‑month arrays to avoid surprises if the API ever introduces a distinct key.


491-499: New monthly energy VALUE_SENSOR_MAPPING entries align with existing ones

The four new mappings:

  • CoolingMonthlyElectricalConsumption
  • HeatingMonthlyElectricalConsumption
  • CoolingMonthlyGasConsumption
  • HeatingMonthlyGasConsumption

all match the existing daily/weekly/yearly counterparts in terms of device class (ENERGY), state class (TOTAL_INCREASING), unit (kWh), icons, and enabled state, and their translation keys line up with the new translation entries.

Looks good and should integrate cleanly with the new monthly period handling in sensor.py.

Also applies to: 527-535, 563-571, 599-607

custom_components/daikin_onecta/translations/en.json (1)

67-69: Monthly electrical/gas sensor translations match const mappings

The three new entries:

  • coolingmonthlyelectricalconsumption
  • heatingmonthlyelectricalconsumption
  • coolingmonthlygasconsumption

all use clear English names and align with the corresponding TRANSLATION_KEY values in VALUE_SENSOR_MAPPING. No issues.

Also applies to: 79-81, 91-93

custom_components/daikin_onecta/translations/nl.json (1)

66-68: Dutch monthly energy sensor translations are consistent and aligned

The four new NL entries for monthly electrical/gas consumption match the TRANSLATION_KEY values in VALUE_SENSOR_MAPPING and are phrased consistently with the existing daily/weekly/yearly variants.

No issues from a localization perspective.

Also applies to: 78-80, 90-92, 102-104

Comment on lines +103 to +105
"heatingweeklymonthlyconsumption": {
"name": "Heating monthly gas consumption"
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix translation key typo for heating monthly gas consumption

VALUE_SENSOR_MAPPING defines the translation key "heatingmonthlygasconsumption", but the EN translation uses "heatingweeklymonthlyconsumption". As a result, the monthly heating gas sensor will not pick up this name, and you’ll have a dead translation entry.

Recommend renaming the key to match const.py:

-      "heatingweeklymonthlyconsumption": {
+      "heatingmonthlygasconsumption": {
         "name": "Heating monthly gas consumption"
       },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"heatingweeklymonthlyconsumption": {
"name": "Heating monthly gas consumption"
},
"heatingmonthlygasconsumption": {
"name": "Heating monthly gas consumption"
},
🤖 Prompt for AI Agents
In custom_components/daikin_onecta/translations/en.json around lines 103-105,
the translation key "heatingweeklymonthlyconsumption" is wrong; update the JSON
key to "heatingmonthlygasconsumption" to match VALUE_SENSOR_MAPPING in const.py
so the monthly heating gas sensor picks up the name, and remove the old unused
key if present.

    * custom_components/daikin_onecta/const.py:
    * custom_components/daikin_onecta/sensor.py:
    * custom_components/daikin_onecta/translations/en.json:
    * tests/snapshots/test_init.ambr:
Copy link
Copy Markdown
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: 3

🧹 Nitpick comments (1)
custom_components/daikin_onecta/sensor.py (1)

51-64: Consider improving variable naming consistency.

The variable name periodi (line 53) and its usage in the log message (line 55) appear inconsistent with the rest of the codebase. Consider renaming to period_monthly or monthly_period for better clarity.

Apply this diff to improve naming:

         periodName = SENSOR_PERIODS.get(period)
         # When we have the yearly sensor we also add a monthly
         if period == SENSOR_PERIOD_YEARLY:
-            periodi = SENSOR_PERIOD_MONTHLY
+            period_monthly = SENSOR_PERIOD_MONTHLY
             _LOGGER.info(
-                "Device '%s:%s' provides mode %s %s supports periodi %s",
+                "Device '%s:%s' provides mode %s %s supports monthly period %s",
                 device.name,
                 embedded_id,
                 management_point_type,
                 mode,
-                periodi,
+                period_monthly,
             )
             sensor = f"{device.name} {sensor_type} {management_point_type} {mode} {SENSOR_PERIOD_MONTHLY}"
             _LOGGER.info("Proposing sensor '%s'", sensor)
-            sensors.append(DaikinEnergySensor(device, coordinator, embedded_id, management_point_type, sensor_type, mode, periodi))
+            sensors.append(DaikinEnergySensor(device, coordinator, embedded_id, management_point_type, sensor_type, mode, period_monthly))
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be8ff0b and 3cfa304.

📒 Files selected for processing (3)
  • custom_components/daikin_onecta/const.py (5 hunks)
  • custom_components/daikin_onecta/sensor.py (4 hunks)
  • custom_components/daikin_onecta/translations/en.json (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • custom_components/daikin_onecta/translations/en.json
🧰 Additional context used
🧬 Code graph analysis (1)
custom_components/daikin_onecta/sensor.py (2)
custom_components/daikin_onecta/climate.py (1)
  • name (235-239)
tests/test_coordinator.py (1)
  • coordinator (33-47)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Run tests (3.13)
🔇 Additional comments (2)
custom_components/daikin_onecta/const.py (1)

491-499: LGTM!

The four new monthly sensor mappings (CoolingMonthlyElectricalConsumption, HeatingMonthlyElectricalConsumption, CoolingMonthlyGasConsumption, HeatingMonthlyGasConsumption) are correctly configured and consistent with the existing Daily/Weekly/Yearly sensor patterns.

Also applies to: 527-535, 563-571, 599-607

custom_components/daikin_onecta/sensor.py (1)

3-3: LGTM!

The new imports (date, SENSOR_PERIOD_MONTHLY, SENSOR_PERIOD_YEARLY, SENSOR_PERIODS_ARRAY) are appropriate for the monthly sensor functionality being added.

Also applies to: 18-18, 20-20, 22-22

    * custom_components/daikin_onecta/const.py:
    * tests/snapshots/test_init.ambr:
    * custom_components/daikin_onecta/sensor.py:
@jwillemsen jwillemsen merged commit 51eb58e into master Dec 5, 2025
9 checks passed
@jwillemsen jwillemsen deleted the jwi-monthlysensor branch December 5, 2025 12:07
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.

[Feature Request] Add Monthly Electrical Consumption Sensors

1 participant