Improve error mqtt valve error logging (#106129)

* Improve error mqtt valve error logging

* Update homeassistant/components/mqtt/valve.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/valve.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/valve.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Jan Bouwhuis 2023-12-20 21:18:30 +01:00 committed by GitHub
parent 69dcc159ae
commit 65d3f7e1c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 19 deletions

View file

@ -66,12 +66,7 @@ from .mixins import (
async_setup_entity_entry_helper, async_setup_entity_entry_helper,
write_state_on_attr_change, write_state_on_attr_change,
) )
from .models import ( from .models import MqttCommandTemplate, MqttValueTemplate, ReceiveMessage
MqttCommandTemplate,
MqttValueTemplate,
ReceiveMessage,
ReceivePayloadType,
)
from .util import valid_publish_topic, valid_subscribe_topic from .util import valid_publish_topic, valid_subscribe_topic
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -230,7 +225,7 @@ class MqttValve(MqttEntity, ValveEntity):
@callback @callback
def _process_binary_valve_update( def _process_binary_valve_update(
self, payload: ReceivePayloadType, state_payload: str self, msg: ReceiveMessage, state_payload: str
) -> None: ) -> None:
"""Process an update for a valve that does not report the position.""" """Process an update for a valve that does not report the position."""
state: str | None = None state: str | None = None
@ -244,18 +239,21 @@ class MqttValve(MqttEntity, ValveEntity):
state = STATE_CLOSED state = STATE_CLOSED
if state is None: if state is None:
_LOGGER.warning( _LOGGER.warning(
"Payload is not one of [open, closed, opening, closing], got: %s", "Payload received on topic '%s' is not one of "
payload, "[open, closed, opening, closing], got: %s",
msg.topic,
state_payload,
) )
return return
self._update_state(state) self._update_state(state)
@callback @callback
def _process_position_valve_update( def _process_position_valve_update(
self, payload: ReceivePayloadType, position_payload: str, state_payload: str self, msg: ReceiveMessage, position_payload: str, state_payload: str
) -> None: ) -> None:
"""Process an update for a valve that reports the position.""" """Process an update for a valve that reports the position."""
state: str | None = None state: str | None = None
position_set: bool = False
if state_payload == self._config[CONF_STATE_OPENING]: if state_payload == self._config[CONF_STATE_OPENING]:
state = STATE_OPENING state = STATE_OPENING
elif state_payload == self._config[CONF_STATE_CLOSING]: elif state_payload == self._config[CONF_STATE_CLOSING]:
@ -266,16 +264,24 @@ class MqttValve(MqttEntity, ValveEntity):
self._range, float(position_payload) self._range, float(position_payload)
) )
except ValueError: except ValueError:
_LOGGER.warning("Payload '%s' is not numeric", position_payload) _LOGGER.warning(
return "Ignoring non numeric payload '%s' received on topic '%s'",
position_payload,
self._attr_current_valve_position = min(max(percentage_payload, 0), 100) msg.topic,
if state is None: )
else:
self._attr_current_valve_position = min(max(percentage_payload, 0), 100)
position_set = True
if state_payload and state is None and not position_set:
_LOGGER.warning( _LOGGER.warning(
"Payload is not one of [opening, closing], got: %s", "Payload received on topic '%s' is not one of "
payload, "[opening, closing], got: %s",
msg.topic,
state_payload,
) )
return return
if state is None:
return
self._update_state(state) self._update_state(state)
def _prepare_subscribe_topics(self) -> None: def _prepare_subscribe_topics(self) -> None:
@ -315,10 +321,10 @@ class MqttValve(MqttEntity, ValveEntity):
if self._config[CONF_REPORTS_POSITION]: if self._config[CONF_REPORTS_POSITION]:
self._process_position_valve_update( self._process_position_valve_update(
payload, position_payload, state_payload msg, position_payload, state_payload
) )
else: else:
self._process_binary_valve_update(payload, state_payload) self._process_binary_valve_update(msg, state_payload)
if self._config.get(CONF_STATE_TOPIC): if self._config.get(CONF_STATE_TOPIC):
topics["state_topic"] = { topics["state_topic"] = {

View file

@ -245,6 +245,7 @@ async def test_state_via_state_topic_with_position_template(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("message", "asserted_state", "valve_position"), ("message", "asserted_state", "valve_position"),
[ [
("invalid", STATE_UNKNOWN, None),
("0", STATE_CLOSED, 0), ("0", STATE_CLOSED, 0),
("opening", STATE_OPENING, None), ("opening", STATE_OPENING, None),
("50", STATE_OPEN, 50), ("50", STATE_OPEN, 50),