mirror of
https://github.com/bahmcloud/HA-KNX-Bridge.git
synced 2026-04-06 16:51:14 +00:00
Add port devices and enable toggles
This commit is contained in:
@@ -19,6 +19,7 @@ from .const import (
|
||||
CONF_INVERT_OUTGOING,
|
||||
CONF_MOVE_LONG_ADDRESS,
|
||||
CONF_MOVE_SHORT_ADDRESS,
|
||||
CONF_PORT_ENABLED,
|
||||
CONF_PORTS,
|
||||
CONF_POSITION_ADDRESS,
|
||||
CONF_POSITION_STATE_ADDRESS,
|
||||
@@ -119,7 +120,12 @@ class BridgeManager:
|
||||
switch_ports: list[SwitchPort] = []
|
||||
cover_ports: list[CoverPort] = []
|
||||
|
||||
for port_type, data in _iter_port_configs(self.entry):
|
||||
enabled_overrides = dict(self.entry.options.get(CONF_PORT_ENABLED, {}))
|
||||
for port in _iter_port_configs(self.entry):
|
||||
port_type = port.port_type
|
||||
data = port.data
|
||||
if not _is_port_enabled(port, enabled_overrides):
|
||||
continue
|
||||
if port_type == "binary_sensor":
|
||||
binary_ports.append(
|
||||
BinarySensorPort(
|
||||
@@ -683,15 +689,52 @@ def _event_destination(event: Event) -> str | None:
|
||||
)
|
||||
|
||||
|
||||
def _iter_port_configs(entry: ConfigEntry) -> list[tuple[str, dict[str, Any]]]:
|
||||
ports: list[tuple[str, dict[str, Any]]] = []
|
||||
@dataclass(frozen=True)
|
||||
class PortConfig:
|
||||
port_id: str
|
||||
port_type: str
|
||||
title: str
|
||||
data: dict[str, Any]
|
||||
enabled: bool | None
|
||||
|
||||
|
||||
def _iter_port_configs(entry: ConfigEntry) -> list[PortConfig]:
|
||||
ports: list[PortConfig] = []
|
||||
subentries = getattr(entry, "subentries", [])
|
||||
for subentry in subentries:
|
||||
ports.append((subentry.type, subentry.data))
|
||||
ports.append(
|
||||
PortConfig(
|
||||
port_id=subentry.entry_id,
|
||||
port_type=subentry.type,
|
||||
title=subentry.title or subentry.entry_id,
|
||||
data=subentry.data,
|
||||
enabled=subentry.data.get(CONF_ENABLED),
|
||||
)
|
||||
)
|
||||
option_ports = entry.options.get(CONF_PORTS, [])
|
||||
for port in option_ports:
|
||||
port_type = port.get("type")
|
||||
data = port.get("data", {})
|
||||
if port_type and isinstance(data, dict):
|
||||
ports.append((port_type, data))
|
||||
ports.append(
|
||||
PortConfig(
|
||||
port_id=port.get("id", ""),
|
||||
port_type=port_type,
|
||||
title=port.get("title", port.get("id", "Port")),
|
||||
data=data,
|
||||
enabled=port.get("enabled"),
|
||||
)
|
||||
)
|
||||
return ports
|
||||
|
||||
|
||||
def _is_port_enabled(port: PortConfig, overrides: dict[str, Any]) -> bool:
|
||||
if port.port_id and port.port_id in overrides:
|
||||
return bool(overrides[port.port_id])
|
||||
if port.enabled is None:
|
||||
return True
|
||||
return bool(port.enabled)
|
||||
|
||||
|
||||
def iter_ports(entry: ConfigEntry) -> list[PortConfig]:
|
||||
return _iter_port_configs(entry)
|
||||
|
||||
Reference in New Issue
Block a user