diff --git a/custom_components/ninebot/api.py b/custom_components/ninebot/api.py index 86d26f1..529f209 100644 --- a/custom_components/ninebot/api.py +++ b/custom_components/ninebot/api.py @@ -197,10 +197,14 @@ def _normalize_status(status: dict[str, Any]) -> dict[str, Any]: locked = loc.get("lock") lat = _coerce_float(loc.get("lat")) lon = _coerce_float(loc.get("lon")) + accuracy = _coerce_float(loc.get("acc")) if locked is not None: state["lock"] = _coerce_int(locked) if lat is not None and lon is not None: - state["location"] = {"latitude": lat, "longitude": lon} + location = {"latitude": lat, "longitude": lon} + if accuracy is not None: + location["accuracy"] = accuracy + state["location"] = location elif "lock_status" in status: state["lock"] = status.get("lock_status") diff --git a/custom_components/ninebot/device_tracker.py b/custom_components/ninebot/device_tracker.py index 1bd371c..37f207d 100644 --- a/custom_components/ninebot/device_tracker.py +++ b/custom_components/ninebot/device_tracker.py @@ -57,22 +57,27 @@ def entity_picture(self) -> str | None: picture = self.device_profile.get("image_url") return picture if isinstance(picture, str) and picture else None - @property - def available(self) -> bool: - return super().available and self.latitude is not None and self.longitude is not None + @callback + def _on_updated(self) -> None: + """Handle updated data.""" + self._attr_latitude = None + self._attr_longitude = None + self._attr_location_accuracy = None - @property - def latitude(self) -> float | None: location = self.device_state.get("location") if not isinstance(location, dict): - return None + return + latitude = location.get("latitude") - return latitude if isinstance(latitude, int | float) else None + longitude = location.get("longitude") + if isinstance(latitude, int | float) and isinstance(longitude, int | float): + self._attr_latitude = latitude + self._attr_longitude = longitude + + accuracy = location.get("accuracy") + if isinstance(accuracy, int | float): + self._attr_location_accuracy = accuracy @property - def longitude(self) -> float | None: - location = self.device_state.get("location") - if not isinstance(location, dict): - return None - longitude = location.get("longitude") - return longitude if isinstance(longitude, int | float) else None + def available(self) -> bool: + return super().available and self.latitude is not None and self.longitude is not None