1 """The Tasmota integration."""
3 from __future__
import annotations
7 from hatasmota.const
import (
15 from hatasmota.models
import TasmotaDeviceConfig
16 from hatasmota.mqtt
import TasmotaMQTTClient
20 async_prepare_subscribe_topics,
21 async_subscribe_topics,
22 async_unsubscribe_topics,
29 from .
import device_automation, discovery
31 CONF_DISCOVERY_PREFIX,
32 DATA_REMOVE_DISCOVER_COMPONENT,
37 _LOGGER = logging.getLogger(__name__)
41 """Set up Tasmota from a config entry."""
42 hass.data[DATA_UNSUB] = []
46 payload: mqtt.PublishPayloadType,
50 await mqtt.async_publish(hass, topic, payload, qos, retain)
52 async
def _subscribe_topics(sub_state: dict |
None, topics: dict) -> dict:
54 for topic
in topics.values():
55 if "msg_callback" in topic
and "event_loop_safe" in topic:
56 topic[
"msg_callback"] = callback(topic[
"msg_callback"])
61 async
def _unsubscribe_topics(sub_state: dict |
None) -> dict:
64 tasmota_mqtt = TasmotaMQTTClient(_publish, _subscribe_topics, _unsubscribe_topics)
66 device_registry = dr.async_get(hass)
69 """Discover and add a Tasmota device."""
71 hass, mac, config, entry, tasmota_mqtt, device_registry
74 await device_automation.async_setup_entry(hass, entry)
75 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
76 discovery_prefix = entry.data[CONF_DISCOVERY_PREFIX]
77 await discovery.async_start(
78 hass, discovery_prefix, entry, tasmota_mqtt, async_discover_device
85 """Unload a config entry."""
88 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
93 await discovery.async_stop(hass)
96 for unsub
in hass.data[DATA_UNSUB]:
98 hass.data.pop(DATA_REMOVE_DISCOVER_COMPONENT.format(
"device_automation"))()
99 for platform
in PLATFORMS:
100 hass.data.pop(DATA_REMOVE_DISCOVER_COMPONENT.format(platform))()
103 device_registry = dr.async_get(hass)
104 devices = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
105 for device
in devices:
106 await device_automation.async_remove_automations(hass, device.id)
113 config_entry: ConfigEntry,
115 tasmota_mqtt: TasmotaMQTTClient,
116 device_registry: DeviceRegistry,
118 """Remove a discovered Tasmota device."""
119 device = device_registry.async_get_device(
120 connections={(CONNECTION_NETWORK_MAC, mac)}
123 if device
is None or config_entry.entry_id
not in device.config_entries:
126 _LOGGER.debug(
"Removing tasmota from device %s", mac)
127 device_registry.async_update_device(
128 device.id, remove_config_entry_id=config_entry.entry_id
134 config_entry: ConfigEntry,
135 config: TasmotaDeviceConfig,
136 device_registry: DeviceRegistry,
138 """Add or update device registry."""
139 _LOGGER.debug(
"Adding or updating tasmota device %s", config[CONF_MAC])
140 device_registry.async_get_or_create(
141 config_entry_id=config_entry.entry_id,
142 configuration_url=f
"http://{config[CONF_IP]}/",
143 connections={(CONNECTION_NETWORK_MAC, config[CONF_MAC])},
144 manufacturer=config[CONF_MANUFACTURER],
145 model=config[CONF_MODEL],
146 name=config[CONF_NAME],
147 sw_version=config[CONF_SW_VERSION],
154 config: TasmotaDeviceConfig,
155 config_entry: ConfigEntry,
156 tasmota_mqtt: TasmotaMQTTClient,
157 device_registry: DeviceRegistry,
159 """Set up the Tasmota device."""
161 await
_remove_device(hass, config_entry, mac, tasmota_mqtt, device_registry)
167 hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
169 """Remove Tasmota config entry from a device."""
171 connections = device_entry.connections
172 macs = [c[1]
for c
in connections
if c[0] == CONNECTION_NETWORK_MAC]
173 tasmota_discovery = hass.data[discovery.TASMOTA_DISCOVERY_INSTANCE]
175 await tasmota_discovery.clear_discovery_topic(
176 mac, config_entry.data[CONF_DISCOVERY_PREFIX]
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
dict[str, EntitySubscription] async_prepare_subscribe_topics(HomeAssistant hass, dict[str, EntitySubscription]|None sub_state, dict[str, dict[str, Any]] topics)
None async_subscribe_topics(HomeAssistant hass, dict[str, EntitySubscription] sub_state)
None async_setup_device(HomeAssistant hass, str mac, TasmotaDeviceConfig config, ConfigEntry config_entry, TasmotaMQTTClient tasmota_mqtt, DeviceRegistry device_registry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None _remove_device(HomeAssistant hass, ConfigEntry config_entry, str mac, TasmotaMQTTClient tasmota_mqtt, DeviceRegistry device_registry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None _update_device(HomeAssistant hass, ConfigEntry config_entry, TasmotaDeviceConfig config, DeviceRegistry device_registry)
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, dr.DeviceEntry device_entry)