Add default `source_type` for MQTT device_tracker (#81128)
Make sure the source_type is set - default to gpspull/81139/head
parent
5eb3473d41
commit
f10f74a65a
|
@ -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]
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue