Skip to content
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
27 changes: 19 additions & 8 deletions hatasmota/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
),
}

NO_POLL_SWITCHMODES = [SWITCHMODE_PUSHON, SWITCHMODE_PUSHON_INV]


@attr.s(slots=True, frozen=True)
class TasmotaSwitchTriggerConfig:
Expand Down Expand Up @@ -236,6 +238,12 @@ class TasmotaSwitchConfig(TasmotaAvailabilityConfig, TasmotaEntityConfig):
def from_discovery_message(cls, config, idx, platform):
"""Instantiate from discovery message."""
switchmode = config[CONF_SWITCH][idx]
state_topic1 = get_topic_stat_result(config)
state_topic2 = None
state_topic3 = None
if switchmode not in NO_POLL_SWITCHMODES:
state_topic2 = get_topic_tele_sensor(config)
state_topic3 = get_topic_stat_status(config, 10)
binary_sensor, off_delay, _ = SWITCHMODE_MAP[switchmode]
if not binary_sensor:
return None
Expand All @@ -254,9 +262,9 @@ def from_discovery_message(cls, config, idx, platform):
off_delay=off_delay,
state_power_off=config_get_state_power_off(config),
state_power_on=config_get_state_power_on(config),
state_topic1=get_topic_stat_result(config),
state_topic2=get_topic_tele_sensor(config),
state_topic3=get_topic_stat_status(config, 10),
state_topic1=state_topic1,
state_topic2=state_topic2,
state_topic3=state_topic3,
switchname=config_get_switchname(config, idx),
)

Expand Down Expand Up @@ -303,17 +311,20 @@ def state_message_received(msg):
"topic": self._cfg.state_topic1,
"msg_callback": state_message_received,
},
"state_topic2": {
}
if self._cfg.state_topic2:
topics["state_topic2"] = {
"event_loop_safe": True,
"topic": self._cfg.state_topic2,
"msg_callback": state_message_received,
},
"state_topic3": {
}
if self._cfg.state_topic3:
topics["state_topic3"] = {
"event_loop_safe": True,
"topic": self._cfg.state_topic3,
"msg_callback": state_message_received,
},
}
}

topics = {**topics, **availability_topics}

self._sub_state = await self._mqtt_client.subscribe(
Expand Down