Add Tasmota binary sensor (#41380)

This commit is contained in:
Erik Montnemery authored and GitHub committed 2020-10-07 14:41:30 +02:00
1 parent 7c133e4630
commit 6e84f91762
6 files changed
+370 -57

No files matched your search

@@ -0,0 +1,91 @@
"""Support for Tasmota binary sensors."""
import logging
from homeassistant.components import binary_sensor
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.helpers.event as evt
from .const import DOMAIN as TASMOTA_DOMAIN
from .discovery import TASMOTA_DISCOVERY_ENTITY_NEW, clear_discovery_hash
from .mixins import TasmotaAvailability, TasmotaDiscoveryUpdate
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Tasmota binary sensor dynamically through discovery."""
@callback
def async_discover(tasmota_entity, discovery_hash):
"""Discover and add a Tasmota binary sensor."""
try:
async_add_entities(
[
TasmotaBinarySensor(
tasmota_entity=tasmota_entity, discovery_hash=discovery_hash
)
]
)
except Exception:
clear_discovery_hash(hass, discovery_hash)
raise
async_dispatcher_connect(
hass,
TASMOTA_DISCOVERY_ENTITY_NEW.format(binary_sensor.DOMAIN, TASMOTA_DOMAIN),
async_discover,
)
class TasmotaBinarySensor(
TasmotaAvailability,
TasmotaDiscoveryUpdate,
BinarySensorEntity,
):
"""Representation a Tasmota binary sensor."""
def __init__(self, **kwds):
"""Initialize the Tasmota binary sensor."""
self._delay_listener = None
self._state = None
super().__init__(
discovery_update=self.discovery_update,
**kwds,
)
@callback
def off_delay_listener(self, now):
"""Switch device off after a delay."""
self._delay_listener = None
self._state = False
self.async_write_ha_state()
@callback
def state_updated(self, state, **kwargs):
"""Handle new MQTT state messages."""
self._state = state
if self._delay_listener is not None:
self._delay_listener()
self._delay_listener = None
off_delay = self._tasmota_entity.off_delay
if self._state and off_delay is not None:
self._delay_listener = evt.async_call_later(
self.hass, off_delay, self.off_delay_listener
)
self.async_write_ha_state()
@property
def force_update(self):
"""Force update."""
return True
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
@@ -19,6 +19,7 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
SUPPORTED_PLATFORMS = [
"binary_sensor",
"switch",
]
+53 -3
View File
@@ -1,4 +1,4 @@
"""Tasnmota entity mixins."""
"""Tasmota entity mixins."""
import logging
from homeassistant.components.mqtt import (
@@ -6,6 +6,7 @@ from homeassistant.components.mqtt import (
is_connected as mqtt_connected,
)
from homeassistant.core import callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
@@ -25,7 +26,55 @@ class TasmotaEntity(Entity):
def __init__(self, tasmota_entity) -> None:
"""Initialize."""
self._state = None
self._tasmota_entity = tasmota_entity
self._unique_id = tasmota_entity.unique_id
async def async_added_to_hass(self):
"""Subscribe to MQTT events."""
self._tasmota_entity.set_on_state_callback(self.state_updated)
await self._subscribe_topics()
async def async_will_remove_from_hass(self):
"""Unsubscribe when removed."""
await self._tasmota_entity.unsubscribe_topics()
await super().async_will_remove_from_hass()
async def discovery_update(self, update):
"""Handle updated discovery message."""
self._tasmota_entity.config_update(update)
await self._subscribe_topics()
self.async_write_ha_state()
async def _subscribe_topics(self):
"""(Re)Subscribe to topics."""
await self._tasmota_entity.subscribe_topics()
@callback
def state_updated(self, state, **kwargs):
"""Handle new MQTT state messages."""
self._state = state
self.async_write_ha_state()
@property
def device_info(self):
"""Return a device description for device registry."""
return {"connections": {(CONNECTION_NETWORK_MAC, self._tasmota_entity.mac)}}
@property
def name(self):
"""Return the name of the binary sensor."""
return self._tasmota_entity.name
@property
def should_poll(self):
"""Return the polling state."""
return False
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
class TasmotaAvailability(TasmotaEntity):
@@ -38,11 +87,11 @@ class TasmotaAvailability(TasmotaEntity):
async def async_added_to_hass(self) -> None:
"""Subscribe MQTT events."""
await super().async_added_to_hass()
self._tasmota_entity.set_on_availability_callback(self.availability_updated)
self.async_on_remove(
async_subscribe_connection_status(self.hass, self.async_mqtt_connected)
)
await super().async_added_to_hass()
@callback
def availability_updated(self, available: bool) -> None:
@@ -76,8 +125,8 @@ class TasmotaDiscoveryUpdate(TasmotaEntity):
async def async_added_to_hass(self) -> None:
"""Subscribe to discovery updates."""
await super().async_added_to_hass()
self._removed_from_hass = False
await super().async_added_to_hass()
async def discovery_callback(config):
"""Handle discovery update."""
@@ -109,3 +158,4 @@ class TasmotaDiscoveryUpdate(TasmotaEntity):
if not self._removed_from_hass:
clear_discovery_hash(self.hass, self._discovery_hash)
self._removed_from_hass = True
await super().async_will_remove_from_hass()
+1 -53
View File
@@ -4,7 +4,6 @@ import logging
from homeassistant.components import switch
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DOMAIN as TASMOTA_DOMAIN
@@ -42,71 +41,20 @@ class TasmotaSwitch(
):
"""Representation of a Tasmota switch."""
def __init__(self, tasmota_entity, **kwds):
def __init__(self, **kwds):
"""Initialize the Tasmota switch."""
self._state = False
self._sub_state = None
self._unique_id = tasmota_entity.unique_id
super().__init__(
discovery_update=self.discovery_update,
tasmota_entity=tasmota_entity,
**kwds,
)
async def async_added_to_hass(self):
"""Subscribe to MQTT events."""
await super().async_added_to_hass()
self._tasmota_entity.set_on_state_callback(self.state_updated)
await self._subscribe_topics()
async def discovery_update(self, update):
"""Handle updated discovery message."""
self._tasmota_entity.config_update(update)
await self._subscribe_topics()
self.async_write_ha_state()
@callback
def state_updated(self, state):
"""Handle new MQTT state messages."""
self._state = state
self.async_write_ha_state()
async def _subscribe_topics(self):
"""(Re)Subscribe to topics."""
await self._tasmota_entity.subscribe_topics()
async def async_will_remove_from_hass(self):
"""Unsubscribe when removed."""
await self._tasmota_entity.unsubscribe_topics()
await super().async_will_remove_from_hass()
@property
def should_poll(self):
"""Return the polling state."""
return False
@property
def name(self):
"""Return the name of the switch."""
return self._tasmota_entity.name
@property
def is_on(self):
"""Return true if device is on."""
return self._state
@property
def unique_id(self):
"""Return a unique ID."""
return self._tasmota_entity.unique_id
@property
def device_info(self):
"""Return a device description for device registry."""
return {"connections": {(CONNECTION_NETWORK_MAC, self._tasmota_entity.mac)}}
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
self._tasmota_entity.set_state(True)