From f10f74a65a14582b5c3229ee23f7fda25dc01243 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Fri, 28 Oct 2022 10:49:01 +0200 Subject: [PATCH] Add default `source_type` for MQTT device_tracker (#81128) Make sure the source_type is set - default to gps --- .../mqtt/device_tracker/schema_discovery.py | 15 +++++++++++---- .../mqtt/test_device_tracker_discovery.py | 8 ++++++-- tests/components/mqtt/test_diagnostics.py | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/mqtt/device_tracker/schema_discovery.py b/homeassistant/components/mqtt/device_tracker/schema_discovery.py index 1d3f9d109f6d..673a9cb04b78 100644 --- a/homeassistant/components/mqtt/device_tracker/schema_discovery.py +++ b/homeassistant/components/mqtt/device_tracker/schema_discovery.py @@ -7,7 +7,10 @@ import voluptuous as vol from homeassistant.components import device_tracker from homeassistant.components.device_tracker import SOURCE_TYPES -from homeassistant.components.device_tracker.config_entry import TrackerEntity +from homeassistant.components.device_tracker.config_entry import ( + SourceType, + TrackerEntity, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_GPS_ACCURACY, @@ -35,12 +38,16 @@ CONF_PAYLOAD_HOME = "payload_home" CONF_PAYLOAD_NOT_HOME = "payload_not_home" CONF_SOURCE_TYPE = "source_type" +DEFAULT_SOURCE_TYPE = SourceType.GPS + PLATFORM_SCHEMA_MODERN = MQTT_RO_SCHEMA.extend( { vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_PAYLOAD_HOME, default=STATE_HOME): cv.string, vol.Optional(CONF_PAYLOAD_NOT_HOME, default=STATE_NOT_HOME): cv.string, - vol.Optional(CONF_SOURCE_TYPE): vol.In(SOURCE_TYPES), + vol.Optional(CONF_SOURCE_TYPE, default=DEFAULT_SOURCE_TYPE): vol.In( + SOURCE_TYPES + ), } ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) @@ -161,6 +168,6 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity): return self._location_name @property - def source_type(self): + def source_type(self) -> SourceType | str: """Return the source type, eg gps or router, of the device.""" - return self._config.get(CONF_SOURCE_TYPE) + return self._config[CONF_SOURCE_TYPE] diff --git a/tests/components/mqtt/test_device_tracker_discovery.py b/tests/components/mqtt/test_device_tracker_discovery.py index a39b9b696c53..6324afeb3efa 100644 --- a/tests/components/mqtt/test_device_tracker_discovery.py +++ b/tests/components/mqtt/test_device_tracker_discovery.py @@ -356,11 +356,12 @@ async def test_setting_device_tracker_location_via_mqtt_message( async_fire_mqtt_message( hass, "homeassistant/device_tracker/bla/config", - '{ "name": "test", "state_topic": "test-topic" }', + '{ "name": "test", "state_topic": "test-topic", "source_type": "router" }', ) await hass.async_block_till_done() state = hass.states.get("device_tracker.test") + assert state.attributes["source_type"] == "router" assert state.state == STATE_UNKNOWN @@ -386,6 +387,7 @@ async def test_setting_device_tracker_location_via_lat_lon_message( await hass.async_block_till_done() state = hass.states.get("device_tracker.test") + assert state.attributes["source_type"] == "gps" assert state.state == STATE_UNKNOWN @@ -395,12 +397,14 @@ async def test_setting_device_tracker_location_via_lat_lon_message( async_fire_mqtt_message( hass, "attributes-topic", - '{"latitude":32.87336,"longitude": -117.22743, "gps_accuracy":1.5}', + '{"latitude":32.87336,"longitude": -117.22743, "gps_accuracy":1.5, "source_type": "router"}', ) state = hass.states.get("device_tracker.test") assert state.attributes["latitude"] == 32.87336 assert state.attributes["longitude"] == -117.22743 assert state.attributes["gps_accuracy"] == 1.5 + # assert source_type is overridden by discovery + assert state.attributes["source_type"] == "router" assert state.state == STATE_HOME async_fire_mqtt_message( diff --git a/tests/components/mqtt/test_diagnostics.py b/tests/components/mqtt/test_diagnostics.py index 7c486f9af74b..49d3fe0b4b6a 100644 --- a/tests/components/mqtt/test_diagnostics.py +++ b/tests/components/mqtt/test_diagnostics.py @@ -248,7 +248,7 @@ async def test_redact_diagnostics( "gps_accuracy": 1.5, "latitude": "**REDACTED**", "longitude": "**REDACTED**", - "source_type": None, + "source_type": "gps", }, "entity_id": "device_tracker.mqtt_unique", "last_changed": ANY,