mirror of
https://github.com/home-assistant/core.git
synced 2025-12-25 05:26:47 +00:00
Fix friendly names of zones with mobile_app (#149453)
Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
@@ -8,6 +8,10 @@ from homeassistant.components.device_tracker import (
|
||||
ATTR_LOCATION_NAME,
|
||||
TrackerEntity,
|
||||
)
|
||||
from homeassistant.components.zone import (
|
||||
ENTITY_ID_FORMAT as ZONE_ENTITY_ID_FORMAT,
|
||||
HOME_ZONE,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_BATTERY_LEVEL,
|
||||
@@ -15,6 +19,7 @@ from homeassistant.const import (
|
||||
ATTR_GPS_ACCURACY,
|
||||
ATTR_LATITUDE,
|
||||
ATTR_LONGITUDE,
|
||||
STATE_HOME,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
@@ -99,6 +104,12 @@ class MobileAppEntity(TrackerEntity, RestoreEntity):
|
||||
def location_name(self) -> str | None:
|
||||
"""Return a location name for the current location of the device."""
|
||||
if location_name := self._data.get(ATTR_LOCATION_NAME):
|
||||
if location_name == HOME_ZONE:
|
||||
return STATE_HOME
|
||||
if zone_state := self.hass.states.get(
|
||||
ZONE_ENTITY_ID_FORMAT.format(location_name)
|
||||
):
|
||||
return zone_state.name
|
||||
return location_name
|
||||
return None
|
||||
|
||||
|
||||
@@ -57,12 +57,12 @@ async def setup_zone(hass: HomeAssistant) -> None:
|
||||
(
|
||||
{"gps": [20, 30], "location_name": "office"},
|
||||
{"latitude": 20, "longitude": 30, "gps_accuracy": 30},
|
||||
"office",
|
||||
"Office",
|
||||
),
|
||||
(
|
||||
{"gps": [30, 40], "location_name": "school"},
|
||||
{"latitude": 30, "longitude": 40, "gps_accuracy": 30},
|
||||
"school",
|
||||
"School",
|
||||
),
|
||||
# Send wrong coordinates + location_name: Location name has precedence
|
||||
(
|
||||
@@ -73,17 +73,17 @@ async def setup_zone(hass: HomeAssistant) -> None:
|
||||
(
|
||||
{"gps": [10, 10], "location_name": "office"},
|
||||
{"latitude": 10, "longitude": 10, "gps_accuracy": 30},
|
||||
"office",
|
||||
"Office",
|
||||
),
|
||||
(
|
||||
{"gps": [10, 10], "location_name": "school"},
|
||||
{"latitude": 10, "longitude": 10, "gps_accuracy": 30},
|
||||
"school",
|
||||
"School",
|
||||
),
|
||||
# Send location_name only
|
||||
({"location_name": "home"}, {}, "home"),
|
||||
({"location_name": "office"}, {}, "office"),
|
||||
({"location_name": "school"}, {}, "school"),
|
||||
({"location_name": "office"}, {}, "Office"),
|
||||
({"location_name": "school"}, {}, "School"),
|
||||
# Send coordinates only - location is determined by coordinates
|
||||
(
|
||||
{"gps": [10, 20]},
|
||||
|
||||
Reference in New Issue
Block a user