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."""

View File

@ -7,11 +7,7 @@ from typing import Any
import voluptuous as vol
from homeassistant.components import switch
from homeassistant.components.switch import (
DEVICE_CLASSES_SCHEMA,
SwitchDeviceClass,
SwitchEntity,
)
from homeassistant.components.switch import DEVICE_CLASSES_SCHEMA, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DEVICE_CLASS,
@ -125,8 +121,6 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
def __init__(self, hass, config, config_entry, discovery_data):
"""Initialize the MQTT switch."""
self._state = None
self._state_on = None
self._state_off = None
self._optimistic = None
@ -138,8 +132,10 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
"""Return the config schema."""
return DISCOVERY_SCHEMA
def _setup_from_config(self, config):
def _setup_from_config(self, config: ConfigType) -> None:
"""(Re)Setup the entity."""
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
state_on = config.get(CONF_STATE_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."""
payload = self._value_template(msg.payload)
if payload == self._state_on:
self._state = True
self._attr_is_on = True
elif payload == self._state_off:
self._state = False
self._attr_is_on = False
elif payload == PAYLOAD_NONE:
self._state = None
self._attr_is_on = None
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)
if self._optimistic and (last_state := await self.async_get_last_state()):
self._state = last_state.state == STATE_ON
@property
def is_on(self) -> bool | None:
"""Return true if device is on."""
return self._state
self._attr_is_on = last_state.state == STATE_ON
@property
def assumed_state(self) -> bool:
"""Return true if we do optimistic updates."""
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:
"""Turn the device on.
@ -224,7 +210,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
)
if self._optimistic:
# Optimistically assume that switch has changed state.
self._state = True
self._attr_is_on = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
@ -241,5 +227,5 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
)
if self._optimistic:
# Optimistically assume that switch has changed state.
self._state = False
self._attr_is_on = False
self.async_write_ha_state()