mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Allow float for device_tracker location accuracy (#143604)
This commit is contained in:
parent
088f0c82bd
commit
e389ff2537
@ -218,7 +218,7 @@ class TrackerEntity(
|
||||
|
||||
entity_description: TrackerEntityDescription
|
||||
_attr_latitude: float | None = None
|
||||
_attr_location_accuracy: int = 0
|
||||
_attr_location_accuracy: float = 0
|
||||
_attr_location_name: str | None = None
|
||||
_attr_longitude: float | None = None
|
||||
_attr_source_type: SourceType = SourceType.GPS
|
||||
@ -234,7 +234,7 @@ class TrackerEntity(
|
||||
return not self.should_poll
|
||||
|
||||
@cached_property
|
||||
def location_accuracy(self) -> int:
|
||||
def location_accuracy(self) -> float:
|
||||
"""Return the location accuracy of the device.
|
||||
|
||||
Value in meters.
|
||||
|
@ -162,7 +162,7 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity):
|
||||
):
|
||||
latitude: float | None
|
||||
longitude: float | None
|
||||
gps_accuracy: int
|
||||
gps_accuracy: float
|
||||
# Reset manually set location to allow automatic zone detection
|
||||
self._attr_location_name = None
|
||||
if isinstance(
|
||||
|
@ -64,7 +64,7 @@ class TileDeviceTracker(TileEntity, TrackerEntity):
|
||||
)
|
||||
self._attr_latitude = None if not self._tile.latitude else self._tile.latitude
|
||||
self._attr_location_accuracy = (
|
||||
0 if not self._tile.accuracy else int(self._tile.accuracy)
|
||||
0 if not self._tile.accuracy else self._tile.accuracy
|
||||
)
|
||||
|
||||
self._attr_extra_state_attributes = {
|
||||
|
@ -54,6 +54,6 @@ class TraccarServerDeviceTracker(TraccarServerEntity, TrackerEntity):
|
||||
return self.traccar_position["longitude"]
|
||||
|
||||
@property
|
||||
def location_accuracy(self) -> int:
|
||||
def location_accuracy(self) -> float:
|
||||
"""Return the gps accuracy of the device."""
|
||||
return self.traccar_position["accuracy"]
|
||||
|
@ -49,7 +49,7 @@ class TractiveDeviceTracker(TractiveEntity, TrackerEntity):
|
||||
self._battery_level: int | None = item.hw_info.get("battery_level")
|
||||
self._attr_latitude = item.pos_report["latlong"][0]
|
||||
self._attr_longitude = item.pos_report["latlong"][1]
|
||||
self._attr_location_accuracy: int = item.pos_report["pos_uncertainty"]
|
||||
self._attr_location_accuracy: float = item.pos_report["pos_uncertainty"]
|
||||
self._source_type: str = item.pos_report["sensor_used"]
|
||||
self._attr_unique_id = item.trackable["_id"]
|
||||
|
||||
|
@ -115,7 +115,7 @@ DATA_ZONE_ENTITY_IDS: HassKey[list[str]] = HassKey(ZONE_ENTITY_IDS)
|
||||
|
||||
@bind_hass
|
||||
def async_active_zone(
|
||||
hass: HomeAssistant, latitude: float, longitude: float, radius: int = 0
|
||||
hass: HomeAssistant, latitude: float, longitude: float, radius: float = 0
|
||||
) -> State | None:
|
||||
"""Find the active zone for given latitude, longitude.
|
||||
|
||||
|
@ -1374,7 +1374,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
||||
),
|
||||
TypeHintMatch(
|
||||
function_name="location_accuracy",
|
||||
return_type="int",
|
||||
return_type="float",
|
||||
),
|
||||
TypeHintMatch(
|
||||
function_name="location_name",
|
||||
|
@ -146,12 +146,14 @@ class MockTrackerEntity(TrackerEntity):
|
||||
location_name: str | None = None,
|
||||
latitude: float | None = None,
|
||||
longitude: float | None = None,
|
||||
location_accuracy: float = 0,
|
||||
) -> None:
|
||||
"""Initialize entity."""
|
||||
self._battery_level = battery_level
|
||||
self._location_name = location_name
|
||||
self._latitude = latitude
|
||||
self._longitude = longitude
|
||||
self._location_accuracy = location_accuracy
|
||||
|
||||
@property
|
||||
def battery_level(self) -> int | None:
|
||||
@ -181,6 +183,11 @@ class MockTrackerEntity(TrackerEntity):
|
||||
"""Return longitude value of the device."""
|
||||
return self._longitude
|
||||
|
||||
@property
|
||||
def location_accuracy(self) -> float:
|
||||
"""Return the accuracy of the location in meters."""
|
||||
return self._location_accuracy
|
||||
|
||||
|
||||
@pytest.fixture(name="battery_level")
|
||||
def battery_level_fixture() -> int | None:
|
||||
@ -206,6 +213,12 @@ def longitude_fixture() -> float | None:
|
||||
return None
|
||||
|
||||
|
||||
@pytest.fixture(name="location_accuracy")
|
||||
def accuracy_fixture() -> float:
|
||||
"""Return the location accuracy of the entity for the test."""
|
||||
return 0
|
||||
|
||||
|
||||
@pytest.fixture(name="tracker_entity")
|
||||
def tracker_entity_fixture(
|
||||
entity_id: str,
|
||||
@ -213,6 +226,7 @@ def tracker_entity_fixture(
|
||||
location_name: str | None,
|
||||
latitude: float | None,
|
||||
longitude: float | None,
|
||||
location_accuracy: float = 0,
|
||||
) -> MockTrackerEntity:
|
||||
"""Create a test tracker entity."""
|
||||
entity = MockTrackerEntity(
|
||||
@ -220,6 +234,7 @@ def tracker_entity_fixture(
|
||||
location_name=location_name,
|
||||
latitude=latitude,
|
||||
longitude=longitude,
|
||||
location_accuracy=location_accuracy,
|
||||
)
|
||||
entity.entity_id = entity_id
|
||||
return entity
|
||||
@ -513,6 +528,7 @@ def test_tracker_entity() -> None:
|
||||
assert entity.battery_level is None
|
||||
assert entity.should_poll is False
|
||||
assert entity.force_update is True
|
||||
assert entity.location_accuracy == 0
|
||||
|
||||
class MockEntity(TrackerEntity):
|
||||
"""Mock tracker class."""
|
||||
|
@ -380,7 +380,7 @@ def assert_location_longitude(hass: HomeAssistant, longitude: float) -> None:
|
||||
assert state.attributes.get("longitude") == longitude
|
||||
|
||||
|
||||
def assert_location_accuracy(hass: HomeAssistant, accuracy: int) -> None:
|
||||
def assert_location_accuracy(hass: HomeAssistant, accuracy: float) -> None:
|
||||
"""Test the assertion of a location accuracy."""
|
||||
state = hass.states.get(DEVICE_TRACKER_STATE)
|
||||
assert state.attributes.get("gps_accuracy") == accuracy
|
||||
|
@ -26,6 +26,7 @@ def tile() -> AsyncMock:
|
||||
mock.latitude = 1
|
||||
mock.longitude = 1
|
||||
mock.altitude = 0
|
||||
mock.accuracy = 13.496111
|
||||
mock.lost = False
|
||||
mock.last_timestamp = datetime(2020, 8, 12, 17, 55, 26)
|
||||
mock.lost_timestamp = datetime(1969, 12, 31, 19, 0, 0)
|
||||
@ -42,8 +43,8 @@ def tile() -> AsyncMock:
|
||||
"hardware_version": "02.09",
|
||||
"kind": "TILE",
|
||||
"last_timestamp": datetime(2020, 8, 12, 17, 55, 26),
|
||||
"latitude": 0,
|
||||
"longitude": 0,
|
||||
"latitude": 1,
|
||||
"longitude": 1,
|
||||
"lost": False,
|
||||
"lost_timestamp": datetime(1969, 12, 31, 19, 0, 0),
|
||||
"name": "Wallet",
|
||||
|
@ -38,7 +38,7 @@
|
||||
'attributes': ReadOnlyDict({
|
||||
'altitude': 0,
|
||||
'friendly_name': 'Wallet',
|
||||
'gps_accuracy': 1,
|
||||
'gps_accuracy': 13.496111,
|
||||
'is_lost': False,
|
||||
'last_lost_timestamp': datetime.datetime(1970, 1, 1, 3, 0, tzinfo=datetime.timezone.utc),
|
||||
'last_timestamp': datetime.datetime(2020, 8, 13, 0, 55, 26, tzinfo=datetime.timezone.utc),
|
||||
|
Loading…
x
Reference in New Issue
Block a user