Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all mqtt messages report state to core #140001

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions homeassistant/components/mqtt/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ def _state_message_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(
CONF_STATE_TOPIC, self._state_message_received, {"_attr_alarm_state"}
)
self.add_subscription(CONF_STATE_TOPIC, self._state_message_received)

async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/mqtt/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ def _state_message_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(
CONF_STATE_TOPIC, self._state_message_received, {"_attr_is_on", "_expired"}
)
self.add_subscription(CONF_STATE_TOPIC, self._state_message_received)

async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/mqtt/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def _image_received(self, msg: ReceiveMessage) -> None:
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""

self.add_subscription(
CONF_TOPIC, self._image_received, None, disable_encoding=True
)
self.add_subscription(CONF_TOPIC, self._image_received, disable_encoding=True)

async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
Expand Down
12 changes: 0 additions & 12 deletions homeassistant/components/mqtt/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ def prepare_subscribe_topics(
CONF_CURRENT_TEMP_TEMPLATE,
"_attr_current_temperature",
),
{"_attr_current_temperature"},
)
self.add_subscription(
CONF_TEMP_STATE_TOPIC,
Expand All @@ -450,7 +449,6 @@ def prepare_subscribe_topics(
CONF_TEMP_STATE_TEMPLATE,
"_attr_target_temperature",
),
{"_attr_target_temperature"},
)
self.add_subscription(
CONF_TEMP_LOW_STATE_TOPIC,
Expand All @@ -459,7 +457,6 @@ def prepare_subscribe_topics(
CONF_TEMP_LOW_STATE_TEMPLATE,
"_attr_target_temperature_low",
),
{"_attr_target_temperature_low"},
)
self.add_subscription(
CONF_TEMP_HIGH_STATE_TOPIC,
Expand All @@ -468,7 +465,6 @@ def prepare_subscribe_topics(
CONF_TEMP_HIGH_STATE_TEMPLATE,
"_attr_target_temperature_high",
),
{"_attr_target_temperature_high"},
)

async def _subscribe_topics(self) -> None:
Expand Down Expand Up @@ -737,7 +733,6 @@ def _prepare_subscribe_topics(self) -> None:
self.add_subscription(
CONF_ACTION_TOPIC,
self._handle_action_received,
{"_attr_hvac_action"},
)
self.add_subscription(
CONF_CURRENT_HUMIDITY_TOPIC,
Expand All @@ -746,7 +741,6 @@ def _prepare_subscribe_topics(self) -> None:
CONF_CURRENT_HUMIDITY_TEMPLATE,
"_attr_current_humidity",
),
{"_attr_current_humidity"},
)
self.add_subscription(
CONF_HUMIDITY_STATE_TOPIC,
Expand All @@ -755,7 +749,6 @@ def _prepare_subscribe_topics(self) -> None:
CONF_HUMIDITY_STATE_TEMPLATE,
"_attr_target_humidity",
),
{"_attr_target_humidity"},
)
self.add_subscription(
CONF_MODE_STATE_TOPIC,
Expand All @@ -765,7 +758,6 @@ def _prepare_subscribe_topics(self) -> None:
"_attr_hvac_mode",
CONF_MODE_LIST,
),
{"_attr_hvac_mode"},
)
self.add_subscription(
CONF_FAN_MODE_STATE_TOPIC,
Expand All @@ -775,7 +767,6 @@ def _prepare_subscribe_topics(self) -> None:
"_attr_fan_mode",
CONF_FAN_MODE_LIST,
),
{"_attr_fan_mode"},
)
self.add_subscription(
CONF_SWING_HORIZONTAL_MODE_STATE_TOPIC,
Expand All @@ -785,7 +776,6 @@ def _prepare_subscribe_topics(self) -> None:
"_attr_swing_horizontal_mode",
CONF_SWING_HORIZONTAL_MODE_LIST,
),
{"_attr_swing_horizontal_mode"},
)
self.add_subscription(
CONF_SWING_MODE_STATE_TOPIC,
Expand All @@ -795,12 +785,10 @@ def _prepare_subscribe_topics(self) -> None:
"_attr_swing_mode",
CONF_SWING_MODE_LIST,
),
{"_attr_swing_mode"},
)
self.add_subscription(
CONF_PRESET_MODE_STATE_TOPIC,
self._handle_preset_mode_received,
{"_attr_preset_mode"},
)
# add subscriptions for MqttTemperatureControlEntity
self.prepare_subscribe_topics()
Expand Down
9 changes: 0 additions & 9 deletions homeassistant/components/mqtt/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,14 @@ def _prepare_subscribe_topics(self) -> None:
self.add_subscription(
CONF_GET_POSITION_TOPIC,
self._position_message_received,
{
"_attr_current_cover_position",
"_attr_current_cover_tilt_position",
"_attr_is_closed",
"_attr_is_closing",
"_attr_is_opening",
},
)
self.add_subscription(
CONF_STATE_TOPIC,
self._state_message_received,
{"_attr_is_closed", "_attr_is_closing", "_attr_is_opening"},
)
self.add_subscription(
CONF_TILT_STATUS_TOPIC,
self._tilt_message_received,
{"_attr_current_cover_tilt_position"},
)

async def _subscribe_topics(self) -> None:
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/mqtt/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ def _tracker_message_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(
CONF_STATE_TOPIC, self._tracker_message_received, {"_location_name"}
)
self.add_subscription(CONF_STATE_TOPIC, self._tracker_message_received)

@property
def force_update(self) -> bool:
Expand Down
18 changes: 7 additions & 11 deletions homeassistant/components/mqtt/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ def _attributes_prepare_subscribe_topics(self) -> None:
"msg_callback": partial(
self._message_callback, # type: ignore[attr-defined]
self._attributes_message_received,
{"_attr_extra_state_attributes"},
),
"entity_id": self.entity_id,
"qos": self._attributes_config.get(CONF_QOS),
Expand Down Expand Up @@ -513,7 +512,6 @@ def _availability_prepare_subscribe_topics(self) -> None:
"msg_callback": partial(
self._message_callback, # type: ignore[attr-defined]
self._availability_message_received,
{"available"},
),
"entity_id": self.entity_id,
"qos": self._avail_config[CONF_QOS],
Expand Down Expand Up @@ -1450,15 +1448,11 @@ def _attrs_have_changed(
def _message_callback(
self,
msg_callback: MessageCallbackType,
attributes: set[str] | None,
msg: ReceiveMessage,
*,
disable_write_state: bool = False,
) -> None:
"""Process the message callback."""
if attributes is not None:
attrs_snapshot: tuple[tuple[str, Any | UndefinedType], ...] = tuple(
(attribute, getattr(self, attribute, UNDEFINED))
for attribute in attributes
)
mqtt_data = self.hass.data[DATA_MQTT]
messages = mqtt_data.debug_info_entities[self.entity_id]["subscriptions"][
msg.subscribed_topic
Expand All @@ -1472,14 +1466,14 @@ def _message_callback(
_LOGGER.warning(exc)
return

if attributes is not None and self._attrs_have_changed(attrs_snapshot):
if not disable_write_state:
mqtt_data.state_write_requests.write_state_request(self)

def add_subscription(
self,
state_topic_config_key: str,
msg_callback: Callable[[ReceiveMessage], None],
tracked_attributes: set[str] | None,
disable_write_state: bool = False,
disable_encoding: bool = False,
) -> bool:
"""Add a subscription."""
Expand All @@ -1494,7 +1488,9 @@ def add_subscription(
self._subscriptions[state_topic_config_key] = {
"topic": self._config[state_topic_config_key],
"msg_callback": partial(
self._message_callback, msg_callback, tracked_attributes
self._message_callback,
msg_callback,
disable_write_state=disable_write_state,
),
"entity_id": self.entity_id,
"qos": qos,
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/mqtt/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def _event_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(CONF_STATE_TOPIC, self._event_received, None)
self.add_subscription(
CONF_STATE_TOPIC, self._event_received, disable_write_state=True
)

async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
Expand Down
9 changes: 2 additions & 7 deletions homeassistant/components/mqtt/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,25 +432,20 @@ def _direction_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(CONF_STATE_TOPIC, self._state_received, {"_attr_is_on"})
self.add_subscription(
CONF_PERCENTAGE_STATE_TOPIC, self._percentage_received, {"_attr_percentage"}
)
self.add_subscription(CONF_STATE_TOPIC, self._state_received)
self.add_subscription(CONF_PERCENTAGE_STATE_TOPIC, self._percentage_received)
self.add_subscription(
CONF_PRESET_MODE_STATE_TOPIC,
self._preset_mode_received,
{"_attr_preset_mode"},
)
if self.add_subscription(
CONF_OSCILLATION_STATE_TOPIC,
self._oscillation_received,
{"_attr_oscillating"},
):
self._attr_oscillating = False
self.add_subscription(
CONF_DIRECTION_STATE_TOPIC,
self._direction_received,
{"_attr_current_direction"},
)

async def _subscribe_topics(self) -> None:
Expand Down
12 changes: 3 additions & 9 deletions homeassistant/components/mqtt/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,23 +396,17 @@ def _mode_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(CONF_STATE_TOPIC, self._state_received, {"_attr_is_on"})
self.add_subscription(
CONF_ACTION_TOPIC, self._action_received, {"_attr_action"}
)
self.add_subscription(CONF_STATE_TOPIC, self._state_received)
self.add_subscription(CONF_ACTION_TOPIC, self._action_received)
self.add_subscription(
CONF_CURRENT_HUMIDITY_TOPIC,
self._current_humidity_received,
{"_attr_current_humidity"},
)
self.add_subscription(
CONF_TARGET_HUMIDITY_STATE_TOPIC,
self._target_humidity_received,
{"_attr_target_humidity"},
)
self.add_subscription(
CONF_MODE_STATE_TOPIC, self._mode_received, {"_attr_mode"}
)
self.add_subscription(CONF_MODE_STATE_TOPIC, self._mode_received)

async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/mqtt/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,9 @@ def _image_from_url_request_received(self, msg: ReceiveMessage) -> None:
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(
CONF_IMAGE_TOPIC, self._image_data_received, None, disable_encoding=True
)
self.add_subscription(
CONF_URL_TOPIC, self._image_from_url_request_received, None
CONF_IMAGE_TOPIC, self._image_data_received, disable_encoding=True
)
self.add_subscription(CONF_URL_TOPIC, self._image_from_url_request_received)

async def _subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/mqtt/lawn_mower.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ def _message_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
if not self.add_subscription(
CONF_ACTIVITY_STATE_TOPIC, self._message_received, {"_attr_activity"}
):
if not self.add_subscription(CONF_ACTIVITY_STATE_TOPIC, self._message_received):
# Force into optimistic mode.
self._attr_assumed_state = True
return
Expand Down
20 changes: 4 additions & 16 deletions homeassistant/components/mqtt/light/schema_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,45 +589,33 @@ def _xy_received(self, msg: ReceiveMessage) -> None:
@callback
def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics."""
self.add_subscription(CONF_STATE_TOPIC, self._state_received, {"_attr_is_on"})
self.add_subscription(
CONF_BRIGHTNESS_STATE_TOPIC, self._brightness_received, {"_attr_brightness"}
)
self.add_subscription(CONF_STATE_TOPIC, self._state_received)
self.add_subscription(CONF_BRIGHTNESS_STATE_TOPIC, self._brightness_received)
self.add_subscription(
CONF_RGB_STATE_TOPIC,
self._rgb_received,
{"_attr_brightness", "_attr_color_mode", "_attr_rgb_color"},
)
self.add_subscription(
CONF_RGBW_STATE_TOPIC,
self._rgbw_received,
{"_attr_brightness", "_attr_color_mode", "_attr_rgbw_color"},
)
self.add_subscription(
CONF_RGBWW_STATE_TOPIC,
self._rgbww_received,
{"_attr_brightness", "_attr_color_mode", "_attr_rgbww_color"},
)
self.add_subscription(
CONF_COLOR_MODE_STATE_TOPIC, self._color_mode_received, {"_attr_color_mode"}
)
self.add_subscription(CONF_COLOR_MODE_STATE_TOPIC, self._color_mode_received)
self.add_subscription(
CONF_COLOR_TEMP_STATE_TOPIC,
self._color_temp_received,
{"_attr_color_mode", "_attr_color_temp_kelvin"},
)
self.add_subscription(
CONF_EFFECT_STATE_TOPIC, self._effect_received, {"_attr_effect"}
)
self.add_subscription(CONF_EFFECT_STATE_TOPIC, self._effect_received)
self.add_subscription(
CONF_HS_STATE_TOPIC,
self._hs_received,
{"_attr_color_mode", "_attr_hs_color"},
)
self.add_subscription(
CONF_XY_STATE_TOPIC,
self._xy_received,
{"_attr_color_mode", "_attr_xy_color"},
)

async def _subscribe_topics(self) -> None:
Expand Down
12 changes: 0 additions & 12 deletions homeassistant/components/mqtt/light/schema_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,6 @@ def _prepare_subscribe_topics(self) -> None:
self.add_subscription(
CONF_STATE_TOPIC,
self._state_received,
{
"_attr_brightness",
"_attr_color_temp_kelvin",
"_attr_effect",
"_attr_hs_color",
"_attr_is_on",
"_attr_rgb_color",
"_attr_rgbw_color",
"_attr_rgbww_color",
"_attr_xy_color",
"color_mode",
},
)

async def _subscribe_topics(self) -> None:
Expand Down
8 changes: 0 additions & 8 deletions homeassistant/components/mqtt/light/schema_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,6 @@ def _prepare_subscribe_topics(self) -> None:
self.add_subscription(
CONF_STATE_TOPIC,
self._state_received,
{
"_attr_brightness",
"_attr_color_mode",
"_attr_color_temp_kelvin",
"_attr_effect",
"_attr_hs_color",
"_attr_is_on",
},
)

async def _subscribe_topics(self) -> None:
Expand Down
Loading
Loading