Skip to content

Shelly: Messkanal passend zum Schaltkanal auswählen - #4305

Merged
LKuemmel merged 2 commits into
openWB:masterfrom
LKuemmel:shelly
Nov 12, 2026
Merged

LKuemmel merged 2 commits into
openWB:masterfrom
LKuemmel:shelly

Conversation

@LKuemmel

@LKuemmel LKuemmel commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

#4214 (comment)

Summary by Sourcery

Select the Shelly measurement channel that matches the configured switching channel.

Bug Fixes:

  • Ensure Shelly consumer measurements are read from the configured switch or meter channel instead of always using channel zero.
  • Reject unsupported or missing configured channels rather than silently falling back to another channel.

Enhancements:

  • Extend Shelly status parsing with optional channel selection while preserving existing default behavior.

Tests:

  • Add coverage for configured-channel measurement and switching across Shelly generations, channel validation, fallback prevention, and EM1 compatibility.

Copilot AI left a comment

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.

🟡 Changes recommended

An active moderate issue can cause an uncaught IndexError during Gen1 Shelly status parsing for valid channel configurations with too few meter entries.

1 open issue
What changed in this PR

Updates Shelly power-measuring consumers so measurements are read from the same configured channel used for switching, supporting multi-channel Shelly devices.

Changes:

  • Adds optional channel selection to Shelly status parsing for Gen1 and Gen2+ devices.
  • Passes the configured channel from the Shelly PM consumer to the parser.
  • Adds parameterized tests covering channel selection, generations, sign factors, default behavior, and missing channels.
File Description
packages/​modules/​devices/​shelly/​shelly/​status_handler.py Selects measurements from the configured Shelly channel while preserving existing aggregate behavior.
packages/​modules/​consumers/​shelly/​shelly_pm/​consumer.py Passes the configured channel when reading Shelly consumer measurements.
packages/​modules/​consumers/​shelly/​shelly_pm/​consumer_test.py Tests channel-specific measurement and switching behavior across supported generations and factors.

Review effort: Lite

Comment on lines +75 to +77
if channel is not None:
# Einzelnen Verbraucherkanal auf die konfigurierte Phase abbilden.
meters = [meters[channel]]

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="packages/modules/consumers/shelly/shelly_pm/consumer.py" line_range="57" />
<code_context>
         status = request_status(config.configuration.ip_address, generation, auth=auth)
-        powers, voltages, currents, _, power, _ = parse_data(1, config.configuration.factor, status)
+        powers, voltages, currents, _, power, _ = parse_data(
+            1, config.configuration.factor, status, channel=config.configuration.channel)
         return ConsumerState(
             power=power,
</code_context>
<issue_to_address>
**Combined power replaces selected phase**

When a Shelly Pro 3EM exposes measurements under `em:0` and is configured for an individual channel, `parse_data` handles `em:0` without consulting `channel` and uses `total_act_power`, so the consumer reports combined three-phase power instead of the selected channel’s reading.

Make the `em:0` handling in `parse_data` return the reading for the configured channel.
</issue_to_address>

### Comment 2
<location path="packages/modules/devices/shelly/shelly/status_handler.py" line_range="127" />
<code_context>
             power = meters['apower'] * factor
             frequency = meters['freq']
-        elif 'switch:0' in status and 'apower' in status['switch:0']:
+        elif switch_key in status and 'apower' in status[switch_key]:
             log.debug("single phase shelly")
             powers = [0.0, 0.0, 0.0]
</code_context>
<issue_to_address>
**Unavailable channel uses another meter**

When the configured channel is nonzero, its `switch:<channel>` entry is missing, and status contains `em1:0`, `parse_data` falls through to the `em1:0` fallback without checking the configured channel, so the consumer publishes power from a different source instead of rejecting the unavailable channel.

Reject a missing configured switch channel instead of falling back to `em1:0`.

Also at `packages/modules/consumers/shelly/shelly_pm/consumer.py:57`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

status = request_status(config.configuration.ip_address, generation, auth=auth)
powers, voltages, currents, _, power, _ = parse_data(1, config.configuration.factor, status)
powers, voltages, currents, _, power, _ = parse_data(
1, config.configuration.factor, status, channel=config.configuration.channel)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Combined power replaces selected phase

When a Shelly Pro 3EM exposes measurements under em:0 and is configured for an individual channel, parse_data handles em:0 without consulting channel and uses total_act_power, so the consumer reports combined three-phase power instead of the selected channel’s reading.

Make the em:0 handling in parse_data return the reading for the configured channel.

Prompt for AI agents
In `packages/modules/consumers/shelly/shelly_pm/consumer.py` at line 57:

**Combined power replaces selected phase**

When a Shelly Pro 3EM exposes measurements under `em:0` and is configured for an individual channel, `parse_data` handles `em:0` without consulting `channel` and uses `total_act_power`, so the consumer reports combined three-phase power instead of the selected channel’s reading.

Make the `em:0` handling in `parse_data` return the reading for the configured channel.

@LKuemmel
LKuemmel merged commit b8a5a04 into openWB:master Nov 12, 2026
2 checks passed
@LKuemmel
LKuemmel deleted the shelly branch November 12, 2026 11:34
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.

2 participants