Repository navigation
Shelly: Messkanal passend zum Schaltkanal auswählen - #4305
Conversation
There was a problem hiding this comment.
🟡 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
| if channel is not None: | ||
| # Einzelnen Verbraucherkanal auf die konfigurierte Phase abbilden. | ||
| meters = [meters[channel]] |
There was a problem hiding this comment.
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>| 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) |
There was a problem hiding this comment.
🟠 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.
#4214 (comment)
Summary by Sourcery
Select the Shelly measurement channel that matches the configured switching channel.
Bug Fixes:
Enhancements:
Tests: