Skip to content

Expose Shelly Input components, including on input-only devices - #42

Open
drHouse-gif wants to merge 1 commit into
victronenergy:masterfrom
drHouse-gif:feat/input-genericinput-v221
Open

drHouse-gif wants to merge 1 commit into
victronenergy:masterfrom
drHouse-gif:feat/input-genericinput-v221

Conversation

@drHouse-gif

@drHouse-gif drHouse-gif commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Rebased onto master (d8cb65d, v2.21). This replaces #33, whose branch was based on sensor-addon-temperature rather than master, so its diff showed 13 files and +1122/-47 of which most was upstream's own work. The change itself is 7 files, +524/-4. It also supersedes #32, which it contains, and which no longer applies to v2.21.

The mechanism it needed, register_secondary_handler, is in master since v2.21, so this no longer has to target a feature branch. The port adapts to the new handler registry: the kind is genericinput, appended to FUNCTIONAL_HANDLERS so the persistence contract on channel indices is preserved, and registered in _CHANNEL_TYPE_BY_KIND so the channel is shown as an input alongside digitalinput, temperature and humidity.

Problem

A device with nothing but Input components — an i4, for instance — is rejected outright:

if not shelly_handlers.has_functional_handler(self._capabilities):
    logger.error("Unsupported Shelly device %s", self._serial)

Input had no handler at all before #32, and the handler it adds there is secondary-only, so
it can only ever attach to a channel that already exists for the same id. Three things stood in
the way of a channel of its own:

  1. has_functional_handler returns False for a device whose capabilities are all secondary
  2. one registry slot per capability, so Input could not be both "own channel" and "attaches"
  3. no channel kind for an input, so nothing would be enumerated

Your branch solves (2) already, which is why this is built on it.

Solution

Input gets both handlers, exactly as Temperature does on this branch:

class effect
@register_secondary_handler('Input') ShellyHandler_input adds /GenericInput/<id>/ to a channel with the same id
@register_handler('Input', kind=HANDLER_KIND_INPUT) ShellyHandler_input_channel registers a service when no channel has that id

so the existing rule decides which applies:

device result
switch:0 + input:0 input:0 adds /GenericInput/0/ to the switch channel
input:0..3, nothing else four input channels, each its own service
switch:0 + input:0,1 input:0 attaches, input:1 becomes a channel of its own

HANDLER_KIND_INPUT is appended to FUNCTIONAL_HANDLERS, after the kinds an Input can be
added to, so existing channel indices and the settings keyed by them are untouched.

A channel of its own is registered as a switch service — that is where gui-v2 documents the
generic input paths (src/genericinput.h: com.victronenergy.switch[.suffix]/GenericInput/<channelId>)
— and reuses PRODUCT_ID_INPUT / "Shelly input device".

The type mapping, the null handling and the Settings/Labels array type are as described in
#32, so I will not repeat them here.

Not covered — deliberately, and not claimed as supported

  • count mode publishes counts.total only. Shelly also reports counts.xtotal, freq
    and xfreq; one /Value cannot carry all four, and which to expose is a design question.
  • /GenericInput/<id>/Event has no consumer. It is in the Venus spec, but gui-v2,
    venus-html5-app and node-red-contrib-victron all read nothing from it, so button events
    reach D-Bus and nothing renders them. Only single tap has a defined encoding.
  • An input channel of its own has no CustomName. ShellyHandler_channel_config_mixin.handler_ainit
    returns True without chaining to super(), so composing it with the input handler silently
    skips the path publishing. Making it cooperative is a change to the mixin that affects every
    handler using it, which does not belong in this commit. Say the word and I will send it
    separately.
  • No hardware. Everything here is against mock/mock_shelly_server.py, driven from the
    official Input documentation. I have not held an i4, so the README's tested-device list is not
    extended and I am not claiming device-level verification.

Tests

  • tests/test_input.py (12) — the four types, the value following the device, disabled and
    error cases, sharing a channel's service, the event toggle bit
  • tests/test_input_only.py (5) — an input-only device being supported at all, its channels
    registering their own services, analog and count channels, an input that shares an id still
    attaching, and a surplus input taking a channel of its own

Separate modules because each Stack starts a driver and the legacy server always binds
port 8000.

Compatibility

Additive. No existing D-Bus path changed or removed, no new dependency, no new source file.
FUNCTIONAL_HANDLERS is appended to, never reordered. Devices with no Input component behave
exactly as before.

Verified on Python 3.12.16 against both:

aioshelly this branch sensor-addon-temperature baseline
14.1.0 (CI installs it unpinned) 93 passed, 4 xfailed 76 passed, 4 xfailed
12.0.0 (the Venus OS recipe ships it) 92 passed, 4 xfailed, 1 failed 75 passed, 4 xfailed, 1 failed

make testinstall passes.

The 12.0.0 failure

tests/test_timeouts.py::test_freeze_longer_than_reconnect_loop fails on aioshelly 12.0.0
and passes on 14.1.0. It is pre-existing on your branch too — I measured the baseline
above before applying anything. Since meta-victronenergy pins python3-aioshelly_12.0.0.bb
while CI installs aioshelly unpinned, the suite is green in CI but not against the version on a
GX. Flagged in #26 as well; happy to open a separate issue if useful.

Verified on v2.21

tests/test_input.py and tests/test_input_only.py: 17 passed. Hardware, on a GX with 9 real devices: 11 of 19 channel services carried /GenericInput; the two switch-mode inputs published /Value with labels, the nine button-mode ones published /Event only.

Full suite in combination with the other proposals: 119 passed / 1 failed on aioshelly 12.0.0 and 120 passed / 0 failed on 14.1.0, against a pristine v2.21 baseline of 75/4/1 and 76/4/0. The single 12.0.0 failure, test_timeouts.py::test_freeze_longer_than_reconnect_loop, is pre-existing on unmodified v2.21.

Shelly devices report their physical inputs as Input components, and nearly every
relay, dimmer and Pro device has at least one. The driver ignored them: no
handler, no capability, no dbus path. A device with nothing but inputs, such as
an i4, was rejected outright as unsupported.

Venus documents a generic input API that is the counterpart of the switchable
output API, and gui-v2 and venus-html5-app both implement it, so publishing
/GenericInput/<id>/... is enough to get a value into the switching pane.

Uses the primary/secondary split this branch introduces, so an Input takes a
channel of its own only when no earlier channel already uses its id:

  switch:0 + input:0   input:0 adds /GenericInput/0/ to the switch channel
  input:0..3 only      four input channels, each its own switch service
  switch:0 + input:0,1 input:0 attaches, input:1 becomes a channel of its own

The new input kind is appended to FUNCTIONAL_HANDLERS, after the kinds whose
channels an Input can be added to, so existing channel indices and the settings
keyed by them are unchanged.

The four Shelly input types map onto the generic input types below. The type
comes from the device's own Input.GetConfig and ValidTypes is left with only that
one bit set, since the mode is configured on the shelly and not here:

  switch  discrete value indicator, /Value 0 or 1, Labels ["/off", "/on"]
  analog  value indicator with range, /Value from percent, range from range_map
  count   value indicator without range, /Value from counts.total
  button  stateless, so no /Value at all, only /Event

A disabled input and a stateless one both report null, published as an invalid
value rather than a zero. An input reporting errors raises /Status to fault. An
input channel of its own is registered as a switch service, which is where gui-v2
documents the generic input paths, and reuses the input device product id.

Not covered, deliberately:
- count mode publishes counts.total only; one /Value cannot carry total, xtotal,
  freq and xfreq at once.
- /GenericInput/<id>/Event is in the Venus spec but read by neither gui-v2 nor
  venus-html5-app, and node-red-contrib-victron has no generic input paths, so
  button events reach dbus and nothing renders them yet.
- an input channel of its own has no CustomName path; the channel config mixin's
  handler_ainit does not chain to super(), so composing the two needs a change to
  the mixin that does not belong in this commit.

The mock server gains Input components (--input-modes), Input.GetStatus/GetConfig/
SetConfig/Trigger/ResetCounters and a Mock.SetInput test hook. tests/test_input.py
covers the four types and the attached case; tests/test_input_only.py covers
input-only devices and inputs with no channel to share, in its own module because
each Stack starts a driver and the legacy server always binds port 8000.
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