1 """Tasmota entity mixins."""
3 from __future__
import annotations
8 from hatasmota.entity
import (
9 TasmotaAvailability
as HATasmotaAvailability,
10 TasmotaEntity
as HATasmotaEntity,
13 from hatasmota.models
import DiscoveryHashType
16 async_subscribe_connection_status,
17 is_connected
as mqtt_connected,
24 from .discovery
import (
25 TASMOTA_DISCOVERY_ENTITY_UPDATED,
30 _LOGGER = logging.getLogger(__name__)
34 """Base class for Tasmota entities."""
36 _attr_has_entity_name =
True
38 def __init__(self, tasmota_entity: HATasmotaEntity) ->
None:
43 connections={(CONNECTION_NETWORK_MAC, tasmota_entity.mac)}
47 """Subscribe to MQTT events."""
51 """Unsubscribe when removed."""
56 self, update: TasmotaEntityConfig, write_state: bool =
True
58 """Handle updated discovery message."""
65 """(Re)Subscribe to topics."""
69 def name(self) -> str | None:
70 """Return the name of the binary sensor."""
75 """Return the polling state."""
80 """Return a unique ID."""
85 """Base class for Tasmota entities which can be on or off."""
93 """Subscribe to MQTT events."""
99 """Handle state updates."""
105 """Return true if device is on."""
110 """Mixin used for platforms that report availability."""
112 _tasmota_entity: HATasmotaAvailability
115 """Initialize the availability mixin."""
123 """Subscribe to MQTT events."""
133 """Handle updated availability."""
140 """Update state on connection/disconnection to MQTT broker."""
141 if not self.
hasshass.is_stopping:
142 if not mqtt_connected(self.
hasshass):
150 """Return if the device is available."""
155 """Mixin used to handle updated discovery message."""
157 def __init__(self, discovery_hash: DiscoveryHashType, **kwds: Any) ->
None:
158 """Initialize the discovery update mixin."""
164 """Subscribe to discovery updates."""
169 def discovery_callback(config: TasmotaEntityConfig) ->
None:
170 """Handle discovery update.
172 If the config has changed we will create a task to
173 do the discovery update.
175 As this callback can fire when nothing has changed, this
176 is a normal function to avoid task creation until it is needed.
179 "Got update for entity with hash: %s '%s'",
185 _LOGGER.debug(
"Updating component: %s", self.
entity_identity_id)
189 _LOGGER.debug(
"Ignoring unchanged update for: %s", self.
entity_identity_id)
197 TASMOTA_DISCOVERY_ENTITY_UPDATED.format(*self.
_discovery_hash_discovery_hash),
204 """Abort adding an entity to a platform."""
209 """Stop listening to signal and cleanup discovery data.."""
None async_added_to_hass(self)
None availability_updated(self, bool available)
None async_mqtt_connected(self, bool _)
None __init__(self, **Any kwds)
None async_added_to_hass(self)
None add_to_platform_abort(self)
None async_will_remove_from_hass(self)
None __init__(self, DiscoveryHashType discovery_hash, **Any kwds)
None discovery_update(self, TasmotaEntityConfig update, bool write_state=True)
None _subscribe_topics(self)
None async_added_to_hass(self)
None __init__(self, HATasmotaEntity tasmota_entity)
None async_will_remove_from_hass(self)
None state_updated(self, bool state, **Any kwargs)
None async_added_to_hass(self)
None __init__(self, **Any kwds)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None clear_discovery_hash(HomeAssistant hass, tuple[str, str] discovery_hash)
None set_discovery_hash(HomeAssistant hass, tuple[str, str] discovery_hash)
Callable[[], None] async_subscribe_connection_status(HomeAssistant hass, ConnectionStatusCallback connection_status_callback)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)