mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Use attr in mqtt binary sensor and switch (#81403)
This commit is contained in:
parent
7a930d7e79
commit
a255546e9d
@ -10,7 +10,6 @@ import voluptuous as vol
|
|||||||
from homeassistant.components import binary_sensor
|
from homeassistant.components import binary_sensor
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASSES_SCHEMA,
|
DEVICE_CLASSES_SCHEMA,
|
||||||
BinarySensorDeviceClass,
|
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -126,7 +125,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
|
|
||||||
def __init__(self, hass, config, config_entry, discovery_data):
|
def __init__(self, hass, config, config_entry, discovery_data):
|
||||||
"""Initialize the MQTT binary sensor."""
|
"""Initialize the MQTT binary sensor."""
|
||||||
self._state: bool | None = None
|
|
||||||
self._expiration_trigger = None
|
self._expiration_trigger = None
|
||||||
self._delay_listener = None
|
self._delay_listener = None
|
||||||
expire_after = config.get(CONF_EXPIRE_AFTER)
|
expire_after = config.get(CONF_EXPIRE_AFTER)
|
||||||
@ -154,7 +152,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
|
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
|
||||||
return
|
return
|
||||||
self._expired = False
|
self._expired = False
|
||||||
self._state = last_state.state == STATE_ON
|
self._attr_is_on = last_state.state == STATE_ON
|
||||||
|
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
self._expiration_trigger = async_track_point_in_utc_time(
|
||||||
self.hass, self._value_is_expired, expiration_at
|
self.hass, self._value_is_expired, expiration_at
|
||||||
@ -180,8 +178,10 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
"""Return the config schema."""
|
"""Return the config schema."""
|
||||||
return DISCOVERY_SCHEMA
|
return DISCOVERY_SCHEMA
|
||||||
|
|
||||||
def _setup_from_config(self, config):
|
def _setup_from_config(self, config: ConfigType) -> None:
|
||||||
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||||
self._attr_force_update = config[CONF_FORCE_UPDATE]
|
self._attr_force_update = config[CONF_FORCE_UPDATE]
|
||||||
|
|
||||||
self._value_template = MqttValueTemplate(
|
self._value_template = MqttValueTemplate(
|
||||||
self._config.get(CONF_VALUE_TEMPLATE),
|
self._config.get(CONF_VALUE_TEMPLATE),
|
||||||
entity=self,
|
entity=self,
|
||||||
@ -194,7 +194,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
def off_delay_listener(now):
|
def off_delay_listener(now):
|
||||||
"""Switch device off after a delay."""
|
"""Switch device off after a delay."""
|
||||||
self._delay_listener = None
|
self._delay_listener = None
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -233,11 +233,11 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if payload == self._config[CONF_PAYLOAD_ON]:
|
if payload == self._config[CONF_PAYLOAD_ON]:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif payload == self._config[CONF_PAYLOAD_OFF]:
|
elif payload == self._config[CONF_PAYLOAD_OFF]:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
elif payload == PAYLOAD_NONE:
|
elif payload == PAYLOAD_NONE:
|
||||||
self._state = None
|
self._attr_is_on = None
|
||||||
else: # Payload is not for this entity
|
else: # Payload is not for this entity
|
||||||
template_info = ""
|
template_info = ""
|
||||||
if self._config.get(CONF_VALUE_TEMPLATE) is not None:
|
if self._config.get(CONF_VALUE_TEMPLATE) is not None:
|
||||||
@ -256,7 +256,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
self._delay_listener = None
|
self._delay_listener = None
|
||||||
|
|
||||||
off_delay = self._config.get(CONF_OFF_DELAY)
|
off_delay = self._config.get(CONF_OFF_DELAY)
|
||||||
if self._state and off_delay is not None:
|
if self._attr_is_on and off_delay is not None:
|
||||||
self._delay_listener = evt.async_call_later(
|
self._delay_listener = evt.async_call_later(
|
||||||
self.hass, off_delay, off_delay_listener
|
self.hass, off_delay, off_delay_listener
|
||||||
)
|
)
|
||||||
@ -288,16 +288,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool | None:
|
|
||||||
"""Return true if the binary sensor is on."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> BinarySensorDeviceClass | None:
|
|
||||||
"""Return the class of this sensor."""
|
|
||||||
return self._config.get(CONF_DEVICE_CLASS)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return true if the device is available and value has not expired."""
|
"""Return true if the device is available and value has not expired."""
|
||||||
|
@ -7,11 +7,7 @@ from typing import Any
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import switch
|
from homeassistant.components import switch
|
||||||
from homeassistant.components.switch import (
|
from homeassistant.components.switch import DEVICE_CLASSES_SCHEMA, SwitchEntity
|
||||||
DEVICE_CLASSES_SCHEMA,
|
|
||||||
SwitchDeviceClass,
|
|
||||||
SwitchEntity,
|
|
||||||
)
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_DEVICE_CLASS,
|
CONF_DEVICE_CLASS,
|
||||||
@ -125,8 +121,6 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||||||
|
|
||||||
def __init__(self, hass, config, config_entry, discovery_data):
|
def __init__(self, hass, config, config_entry, discovery_data):
|
||||||
"""Initialize the MQTT switch."""
|
"""Initialize the MQTT switch."""
|
||||||
self._state = None
|
|
||||||
|
|
||||||
self._state_on = None
|
self._state_on = None
|
||||||
self._state_off = None
|
self._state_off = None
|
||||||
self._optimistic = None
|
self._optimistic = None
|
||||||
@ -138,8 +132,10 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||||||
"""Return the config schema."""
|
"""Return the config schema."""
|
||||||
return DISCOVERY_SCHEMA
|
return DISCOVERY_SCHEMA
|
||||||
|
|
||||||
def _setup_from_config(self, config):
|
def _setup_from_config(self, config: ConfigType) -> None:
|
||||||
"""(Re)Setup the entity."""
|
"""(Re)Setup the entity."""
|
||||||
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||||
|
|
||||||
state_on = config.get(CONF_STATE_ON)
|
state_on = config.get(CONF_STATE_ON)
|
||||||
self._state_on = state_on if state_on else config[CONF_PAYLOAD_ON]
|
self._state_on = state_on if state_on else config[CONF_PAYLOAD_ON]
|
||||||
|
|
||||||
@ -163,11 +159,11 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||||||
"""Handle new MQTT state messages."""
|
"""Handle new MQTT state messages."""
|
||||||
payload = self._value_template(msg.payload)
|
payload = self._value_template(msg.payload)
|
||||||
if payload == self._state_on:
|
if payload == self._state_on:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif payload == self._state_off:
|
elif payload == self._state_off:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
elif payload == PAYLOAD_NONE:
|
elif payload == PAYLOAD_NONE:
|
||||||
self._state = None
|
self._attr_is_on = None
|
||||||
|
|
||||||
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
|
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
|
||||||
|
|
||||||
@ -193,23 +189,13 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||||
|
|
||||||
if self._optimistic and (last_state := await self.async_get_last_state()):
|
if self._optimistic and (last_state := await self.async_get_last_state()):
|
||||||
self._state = last_state.state == STATE_ON
|
self._attr_is_on = last_state.state == STATE_ON
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool | None:
|
|
||||||
"""Return true if device is on."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def assumed_state(self) -> bool:
|
def assumed_state(self) -> bool:
|
||||||
"""Return true if we do optimistic updates."""
|
"""Return true if we do optimistic updates."""
|
||||||
return self._optimistic
|
return self._optimistic
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> SwitchDeviceClass | None:
|
|
||||||
"""Return the device class of the sensor."""
|
|
||||||
return self._config.get(CONF_DEVICE_CLASS)
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on.
|
"""Turn the device on.
|
||||||
|
|
||||||
@ -224,7 +210,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||||||
)
|
)
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
# Optimistically assume that switch has changed state.
|
# Optimistically assume that switch has changed state.
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
@ -241,5 +227,5 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||||||
)
|
)
|
||||||
if self._optimistic:
|
if self._optimistic:
|
||||||
# Optimistically assume that switch has changed state.
|
# Optimistically assume that switch has changed state.
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user