Improve DEVICE_TRIGGERS typing in tasmota (#72149)
* Improve DEVICE_TRIGGERS typing in tasmota * fix CI * Simplify * Simplify some more
This commit is contained in:
parent
53d7eaa1a6
commit
52808562ab
1 changed files with 24 additions and 11 deletions
|
@ -188,11 +188,13 @@ async def async_setup_trigger(
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Got update for trigger with hash: %s '%s'", discovery_hash, trigger_config
|
"Got update for trigger with hash: %s '%s'", discovery_hash, trigger_config
|
||||||
)
|
)
|
||||||
|
device_triggers: dict[str, Trigger] = hass.data[DEVICE_TRIGGERS]
|
||||||
if not trigger_config.is_active:
|
if not trigger_config.is_active:
|
||||||
# Empty trigger_config: Remove trigger
|
# Empty trigger_config: Remove trigger
|
||||||
_LOGGER.debug("Removing trigger: %s", discovery_hash)
|
_LOGGER.debug("Removing trigger: %s", discovery_hash)
|
||||||
if discovery_id in hass.data[DEVICE_TRIGGERS]:
|
if discovery_id in device_triggers:
|
||||||
device_trigger = hass.data[DEVICE_TRIGGERS][discovery_id]
|
device_trigger = device_triggers[discovery_id]
|
||||||
|
assert device_trigger.tasmota_trigger
|
||||||
await device_trigger.tasmota_trigger.unsubscribe_topics()
|
await device_trigger.tasmota_trigger.unsubscribe_topics()
|
||||||
device_trigger.detach_trigger()
|
device_trigger.detach_trigger()
|
||||||
clear_discovery_hash(hass, discovery_hash)
|
clear_discovery_hash(hass, discovery_hash)
|
||||||
|
@ -200,7 +202,8 @@ async def async_setup_trigger(
|
||||||
remove_update_signal()
|
remove_update_signal()
|
||||||
return
|
return
|
||||||
|
|
||||||
device_trigger = hass.data[DEVICE_TRIGGERS][discovery_id]
|
device_trigger = device_triggers[discovery_id]
|
||||||
|
assert device_trigger.tasmota_trigger
|
||||||
if device_trigger.tasmota_trigger.config_same(trigger_config):
|
if device_trigger.tasmota_trigger.config_same(trigger_config):
|
||||||
# Unchanged payload: Ignore to avoid unnecessary unsubscribe / subscribe
|
# Unchanged payload: Ignore to avoid unnecessary unsubscribe / subscribe
|
||||||
_LOGGER.debug("Ignoring unchanged update for: %s", discovery_hash)
|
_LOGGER.debug("Ignoring unchanged update for: %s", discovery_hash)
|
||||||
|
@ -209,6 +212,7 @@ async def async_setup_trigger(
|
||||||
# Non-empty, changed trigger_config: Update trigger
|
# Non-empty, changed trigger_config: Update trigger
|
||||||
_LOGGER.debug("Updating trigger: %s", discovery_hash)
|
_LOGGER.debug("Updating trigger: %s", discovery_hash)
|
||||||
device_trigger.tasmota_trigger.config_update(trigger_config)
|
device_trigger.tasmota_trigger.config_update(trigger_config)
|
||||||
|
assert remove_update_signal
|
||||||
await device_trigger.update_tasmota_trigger(
|
await device_trigger.update_tasmota_trigger(
|
||||||
trigger_config, remove_update_signal
|
trigger_config, remove_update_signal
|
||||||
)
|
)
|
||||||
|
@ -230,7 +234,8 @@ async def async_setup_trigger(
|
||||||
|
|
||||||
if DEVICE_TRIGGERS not in hass.data:
|
if DEVICE_TRIGGERS not in hass.data:
|
||||||
hass.data[DEVICE_TRIGGERS] = {}
|
hass.data[DEVICE_TRIGGERS] = {}
|
||||||
if discovery_id not in hass.data[DEVICE_TRIGGERS]:
|
device_triggers: dict[str, Trigger] = hass.data[DEVICE_TRIGGERS]
|
||||||
|
if discovery_id not in device_triggers:
|
||||||
device_trigger = Trigger(
|
device_trigger = Trigger(
|
||||||
hass=hass,
|
hass=hass,
|
||||||
device_id=device.id,
|
device_id=device.id,
|
||||||
|
@ -240,10 +245,10 @@ async def async_setup_trigger(
|
||||||
type=tasmota_trigger.cfg.type,
|
type=tasmota_trigger.cfg.type,
|
||||||
remove_update_signal=remove_update_signal,
|
remove_update_signal=remove_update_signal,
|
||||||
)
|
)
|
||||||
hass.data[DEVICE_TRIGGERS][discovery_id] = device_trigger
|
device_triggers[discovery_id] = device_trigger
|
||||||
else:
|
else:
|
||||||
# This Tasmota trigger is wanted by device trigger(s), set them up
|
# This Tasmota trigger is wanted by device trigger(s), set them up
|
||||||
device_trigger = hass.data[DEVICE_TRIGGERS][discovery_id]
|
device_trigger = device_triggers[discovery_id]
|
||||||
await device_trigger.set_tasmota_trigger(tasmota_trigger, remove_update_signal)
|
await device_trigger.set_tasmota_trigger(tasmota_trigger, remove_update_signal)
|
||||||
await device_trigger.arm_tasmota_trigger()
|
await device_trigger.arm_tasmota_trigger()
|
||||||
|
|
||||||
|
@ -251,14 +256,20 @@ async def async_setup_trigger(
|
||||||
async def async_remove_triggers(hass: HomeAssistant, device_id: str) -> None:
|
async def async_remove_triggers(hass: HomeAssistant, device_id: str) -> None:
|
||||||
"""Cleanup any device triggers for a Tasmota device."""
|
"""Cleanup any device triggers for a Tasmota device."""
|
||||||
triggers = await async_get_triggers(hass, device_id)
|
triggers = await async_get_triggers(hass, device_id)
|
||||||
|
|
||||||
|
if not triggers:
|
||||||
|
return
|
||||||
|
device_triggers: dict[str, Trigger] = hass.data[DEVICE_TRIGGERS]
|
||||||
for trig in triggers:
|
for trig in triggers:
|
||||||
device_trigger = hass.data[DEVICE_TRIGGERS].pop(trig[CONF_DISCOVERY_ID])
|
device_trigger = device_triggers.pop(trig[CONF_DISCOVERY_ID])
|
||||||
if device_trigger:
|
if device_trigger:
|
||||||
discovery_hash = device_trigger.discovery_hash
|
discovery_hash = device_trigger.discovery_hash
|
||||||
|
|
||||||
|
assert device_trigger.tasmota_trigger
|
||||||
await device_trigger.tasmota_trigger.unsubscribe_topics()
|
await device_trigger.tasmota_trigger.unsubscribe_topics()
|
||||||
device_trigger.detach_trigger()
|
device_trigger.detach_trigger()
|
||||||
clear_discovery_hash(hass, discovery_hash)
|
clear_discovery_hash(hass, discovery_hash)
|
||||||
|
assert device_trigger.remove_update_signal
|
||||||
device_trigger.remove_update_signal()
|
device_trigger.remove_update_signal()
|
||||||
|
|
||||||
|
|
||||||
|
@ -271,7 +282,8 @@ async def async_get_triggers(
|
||||||
if DEVICE_TRIGGERS not in hass.data:
|
if DEVICE_TRIGGERS not in hass.data:
|
||||||
return triggers
|
return triggers
|
||||||
|
|
||||||
for discovery_id, trig in hass.data[DEVICE_TRIGGERS].items():
|
device_triggers: dict[str, Trigger] = hass.data[DEVICE_TRIGGERS]
|
||||||
|
for discovery_id, trig in device_triggers.items():
|
||||||
if trig.device_id != device_id or trig.tasmota_trigger is None:
|
if trig.device_id != device_id or trig.tasmota_trigger is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -297,12 +309,13 @@ async def async_attach_trigger(
|
||||||
"""Attach a device trigger."""
|
"""Attach a device trigger."""
|
||||||
if DEVICE_TRIGGERS not in hass.data:
|
if DEVICE_TRIGGERS not in hass.data:
|
||||||
hass.data[DEVICE_TRIGGERS] = {}
|
hass.data[DEVICE_TRIGGERS] = {}
|
||||||
|
device_triggers: dict[str, Trigger] = hass.data[DEVICE_TRIGGERS]
|
||||||
device_id = config[CONF_DEVICE_ID]
|
device_id = config[CONF_DEVICE_ID]
|
||||||
discovery_id = config[CONF_DISCOVERY_ID]
|
discovery_id = config[CONF_DISCOVERY_ID]
|
||||||
|
|
||||||
if discovery_id not in hass.data[DEVICE_TRIGGERS]:
|
if discovery_id not in device_triggers:
|
||||||
# The trigger has not (yet) been discovered, prepare it for later
|
# The trigger has not (yet) been discovered, prepare it for later
|
||||||
hass.data[DEVICE_TRIGGERS][discovery_id] = Trigger(
|
device_triggers[discovery_id] = Trigger(
|
||||||
hass=hass,
|
hass=hass,
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
discovery_hash=None,
|
discovery_hash=None,
|
||||||
|
@ -311,5 +324,5 @@ async def async_attach_trigger(
|
||||||
subtype=config[CONF_SUBTYPE],
|
subtype=config[CONF_SUBTYPE],
|
||||||
tasmota_trigger=None,
|
tasmota_trigger=None,
|
||||||
)
|
)
|
||||||
trigger: Trigger = hass.data[DEVICE_TRIGGERS][discovery_id]
|
trigger: Trigger = device_triggers[discovery_id]
|
||||||
return await trigger.add_trigger(action, automation_info)
|
return await trigger.add_trigger(action, automation_info)
|
||||||
|
|
Loading…
Add table
Reference in a new issue