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

Fix inputs enumeration #515

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions python_scripts/shellies_discovery_gen2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This script adds MQTT discovery support for Shellies Gen2 devices."""

VERSION = "3.5.0"
VERSION = "3.5.1"

ATTR_BATTERY_POWERED = "battery_powered"
ATTR_BINARY_SENSORS = "binary_sensors"
Expand Down Expand Up @@ -1520,11 +1520,18 @@
}


def get_component_number(component: str, config) -> int:
def get_component_number(component: str, config):
"""Return the number of components."""
return len([key for key in config if key.startswith(f"{component}:")])


def get_component_ids(component: str, config):
"""Return the list of IDs for component."""
return [
int(key.split(":")[-1]) for key in config if key.startswith(f"{component}:")
]


SUPPORTED_MODELS = {
MODEL_BLU_TRV: {
ATTR_NAME: "Shelly BLU TRV",
Expand Down Expand Up @@ -3944,7 +3951,7 @@ def configure_device():
topic, payload = get_switch(switch_id, ATTR_SWITCH, ATTR_SWITCH)
config[topic] = payload

for input_id in range(inputs):
for input_id in inputs:
input_type = device_config[f"input:{input_id}"]["type"]

topic, payload = get_event(input_id, input_type)
Expand Down Expand Up @@ -4392,7 +4399,7 @@ def remove_old_script_versions(device_topic, script_topic):
KEY_CONFIGURATION_URL: device_url,
}

inputs = get_component_number(ATTR_INPUT, device_config)
inputs = get_component_ids(ATTR_INPUT, device_config)
input_events = SUPPORTED_MODELS[model].get(ATTR_INPUT_EVENTS, [])
input_binary_sensors = SUPPORTED_MODELS[model].get(ATTR_INPUT_BINARY_SENSORS, {})
input_sensors = SUPPORTED_MODELS[model].get(ATTR_INPUT_SENSORS, {})
Expand Down
Loading