From 948b34b045a2364f14abba4dd3669829c8f7672f Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Thu, 24 Aug 2023 20:09:14 +0200 Subject: [PATCH] Do not force update mqtt device_tracker (#98838) --- .../components/mqtt/device_tracker.py | 5 +++++ tests/components/mqtt/test_device_tracker.py | 22 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mqtt/device_tracker.py b/homeassistant/components/mqtt/device_tracker.py index dd4eca9878a..67355d9bca5 100644 --- a/homeassistant/components/mqtt/device_tracker.py +++ b/homeassistant/components/mqtt/device_tracker.py @@ -165,6 +165,11 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity): }, ) + @property + def force_update(self) -> bool: + """Do not force updates if the state is the same.""" + return False + async def _subscribe_topics(self) -> None: """(Re)Subscribe to topics.""" await subscription.async_subscribe_topics(self.hass, self._sub_state) diff --git a/tests/components/mqtt/test_device_tracker.py b/tests/components/mqtt/test_device_tracker.py index ddce53bfca0..8485e5578fe 100644 --- a/tests/components/mqtt/test_device_tracker.py +++ b/tests/components/mqtt/test_device_tracker.py @@ -1,6 +1,8 @@ """The tests for the MQTT device_tracker platform.""" +from datetime import UTC, datetime from unittest.mock import patch +from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components import device_tracker, mqtt @@ -199,9 +201,10 @@ async def test_duplicate_device_tracker_removal( async def test_device_tracker_discovery_update( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, + freezer: FrozenDateTimeFactory, ) -> None: """Test for a discovery update event.""" + freezer.move_to("2023-08-22 19:15:00+00:00") await mqtt_mock_entry() async_fire_mqtt_message( hass, @@ -213,7 +216,9 @@ async def test_device_tracker_discovery_update( state = hass.states.get("device_tracker.beer") assert state is not None assert state.name == "Beer" + assert state.last_updated == datetime(2023, 8, 22, 19, 15, tzinfo=UTC) + freezer.move_to("2023-08-22 19:16:00+00:00") async_fire_mqtt_message( hass, "homeassistant/device_tracker/bla/config", @@ -224,6 +229,21 @@ async def test_device_tracker_discovery_update( state = hass.states.get("device_tracker.beer") assert state is not None assert state.name == "Cider" + assert state.last_updated == datetime(2023, 8, 22, 19, 16, tzinfo=UTC) + + freezer.move_to("2023-08-22 19:20:00+00:00") + async_fire_mqtt_message( + hass, + "homeassistant/device_tracker/bla/config", + '{ "name": "Cider", "state_topic": "test-topic" }', + ) + await hass.async_block_till_done() + + state = hass.states.get("device_tracker.beer") + assert state is not None + assert state.name == "Cider" + # Entity was not updated as the state was not changed + assert state.last_updated == datetime(2023, 8, 22, 19, 16, tzinfo=UTC) async def test_cleanup_device_tracker(