Merge branch 'dev' into jbouwh-mqtt-device-discovery

This commit is contained in:
J. Nick Koston
2024-05-26 10:42:37 -10:00
committed by GitHub
2 changed files with 75 additions and 80 deletions

View File

@@ -210,10 +210,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
).async_render_with_possible_json_value ).async_render_with_possible_json_value
@callback @callback
def _state_message_received(self, msg: ReceiveMessage) -> None: def _update_state(self, msg: ReceiveMessage) -> None:
"""Handle new MQTT state messages."""
def _update_state(msg: ReceiveMessage) -> None:
# auto-expire enabled? # auto-expire enabled?
if self._expire_after is not None and self._expire_after > 0: if self._expire_after is not None and self._expire_after > 0:
# When self._expire_after is set, and we receive a message, assume # When self._expire_after is set, and we receive a message, assume
@@ -261,7 +258,8 @@ class MqttSensor(MqttEntity, RestoreSensor):
return return
self._attr_native_value = payload_datetime self._attr_native_value = payload_datetime
def _update_last_reset(msg: ReceiveMessage) -> None: @callback
def _update_last_reset(self, msg: ReceiveMessage) -> None:
payload = self._last_reset_template(msg.payload) payload = self._last_reset_template(msg.payload)
if not payload: if not payload:
@@ -277,9 +275,12 @@ class MqttSensor(MqttEntity, RestoreSensor):
"Invalid last_reset message '%s' from '%s'", msg.payload, msg.topic "Invalid last_reset message '%s' from '%s'", msg.payload, msg.topic
) )
_update_state(msg) @callback
def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle new MQTT state messages."""
self._update_state(msg)
if CONF_LAST_RESET_VALUE_TEMPLATE in self._config: if CONF_LAST_RESET_VALUE_TEMPLATE in self._config:
_update_last_reset(msg) self._update_last_reset(msg)
@callback @callback
def _prepare_subscribe_topics(self) -> None: def _prepare_subscribe_topics(self) -> None:

View File

@@ -90,18 +90,17 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
def _setup_from_config(self, config: ConfigType) -> None: 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) self._attr_device_class = config.get(CONF_DEVICE_CLASS)
state_on: str | None = config.get(CONF_STATE_ON) state_on: str | None = config.get(CONF_STATE_ON)
self._state_on = state_on if state_on else config[CONF_PAYLOAD_ON]
state_off: str | None = config.get(CONF_STATE_OFF) state_off: str | None = config.get(CONF_STATE_OFF)
self._state_off = state_off if state_off else config[CONF_PAYLOAD_OFF] self._is_on_map = {
state_on if state_on else config[CONF_PAYLOAD_ON]: True,
state_off if state_off else config[CONF_PAYLOAD_OFF]: False,
PAYLOAD_NONE: None,
}
self._optimistic = ( self._optimistic = (
config[CONF_OPTIMISTIC] or config.get(CONF_STATE_TOPIC) is None config[CONF_OPTIMISTIC] or config.get(CONF_STATE_TOPIC) is None
) )
self._attr_assumed_state = bool(self._optimistic) self._attr_assumed_state = bool(self._optimistic)
self._value_template = MqttValueTemplate( self._value_template = MqttValueTemplate(
self._config.get(CONF_VALUE_TEMPLATE), entity=self self._config.get(CONF_VALUE_TEMPLATE), entity=self
).async_render_with_possible_json_value ).async_render_with_possible_json_value
@@ -109,13 +108,8 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
@callback @callback
def _state_message_received(self, msg: ReceiveMessage) -> None: def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle new MQTT state messages.""" """Handle new MQTT state messages."""
payload = self._value_template(msg.payload) if (payload := self._value_template(msg.payload)) in self._is_on_map:
if payload == self._state_on: self._attr_is_on = self._is_on_map[payload]
self._attr_is_on = True
elif payload == self._state_off:
self._attr_is_on = False
elif payload == PAYLOAD_NONE:
self._attr_is_on = None
@callback @callback
def _prepare_subscribe_topics(self) -> None: def _prepare_subscribe_topics(self) -> None: