Use attr in mqtt binary sensor and switch (#81403)

This commit is contained in:
epenet 2022-11-02 13:41:14 +01:00 committed by GitHub
parent 7a930d7e79
commit a255546e9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 43 deletions

View file

@ -10,7 +10,6 @@ import voluptuous as vol
from homeassistant.components import binary_sensor
from homeassistant.components.binary_sensor import (
DEVICE_CLASSES_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
@ -126,7 +125,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
def __init__(self, hass, config, config_entry, discovery_data):
"""Initialize the MQTT binary sensor."""
self._state: bool | None = None
self._expiration_trigger = None
self._delay_listener = None
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)
return
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.hass, self._value_is_expired, expiration_at
@ -180,8 +178,10 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
"""Return the config 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._value_template = MqttValueTemplate(
self._config.get(CONF_VALUE_TEMPLATE),
entity=self,
@ -194,7 +194,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
def off_delay_listener(now):
"""Switch device off after a delay."""
self._delay_listener = None
self._state = False
self._attr_is_on = False
self.async_write_ha_state()
@callback
@ -233,11 +233,11 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
return
if payload == self._config[CONF_PAYLOAD_ON]:
self._state = True
self._attr_is_on = True
elif payload == self._config[CONF_PAYLOAD_OFF]:
self._state = False
self._attr_is_on = False
elif payload == PAYLOAD_NONE:
self._state = None
self._attr_is_on = None
else: # Payload is not for this entity
template_info = ""
if self._config.get(CONF_VALUE_TEMPLATE) is not None:
@ -256,7 +256,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
self._delay_listener = None
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.hass, off_delay, off_delay_listener
)
@ -288,16 +288,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
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
def available(self) -> bool:
"""Return true if the device is available and value has not expired."""