Skip to content

Commit

Permalink
Allow setting the elevation in set_location (home-assistant#99978)
Browse files Browse the repository at this point in the history
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
  • Loading branch information
jrieger and gjohansson-ST authored Sep 13, 2023
1 parent f6b094d commit ee65aa9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
21 changes: 17 additions & 4 deletions homeassistant/components/homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.components import persistent_notification
import homeassistant.config as conf_util
from homeassistant.const import (
ATTR_ELEVATION,
ATTR_ENTITY_ID,
ATTR_LATITUDE,
ATTR_LONGITUDE,
Expand Down Expand Up @@ -250,16 +251,28 @@ async def async_handle_reload_config(call: ha.ServiceCall) -> None:

async def async_set_location(call: ha.ServiceCall) -> None:
"""Service handler to set location."""
await hass.config.async_update(
latitude=call.data[ATTR_LATITUDE], longitude=call.data[ATTR_LONGITUDE]
)
service_data = {
"latitude": call.data[ATTR_LATITUDE],
"longitude": call.data[ATTR_LONGITUDE],
}

if elevation := call.data.get(ATTR_ELEVATION):
service_data["elevation"] = elevation

await hass.config.async_update(**service_data)

async_register_admin_service(
hass,
ha.DOMAIN,
SERVICE_SET_LOCATION,
async_set_location,
vol.Schema({ATTR_LATITUDE: cv.latitude, ATTR_LONGITUDE: cv.longitude}),
vol.Schema(
{
vol.Required(ATTR_LATITUDE): cv.latitude,
vol.Required(ATTR_LONGITUDE): cv.longitude,
vol.Optional(ATTR_ELEVATION): int,
}
),
)

async def async_handle_reload_templates(call: ha.ServiceCall) -> None:
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/homeassistant/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ set_location:
example: 117.22743
selector:
text:
elevation:
required: false
example: 120
selector:
text:

stop:
toggle:
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/homeassistant/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
"longitude": {
"name": "[%key:common::config_flow::data::longitude%]",
"description": "Longitude of your location."
},
"elevation": {
"name": "[%key:common::config_flow::data::elevation%]",
"description": "Elevation of your location."
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,9 @@ class Platform(StrEnum):
ATTR_LATITUDE: Final = "latitude"
ATTR_LONGITUDE: Final = "longitude"

# Elevation of the entity
ATTR_ELEVATION: Final = "elevation"

# Accuracy of location in meters
ATTR_GPS_ACCURACY: Final = "gps_accuracy"

Expand Down
11 changes: 11 additions & 0 deletions tests/components/homeassistant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ async def test_setting_location(hass: HomeAssistant) -> None:
# Just to make sure that we are updating values.
assert hass.config.latitude != 30
assert hass.config.longitude != 40
elevation = hass.config.elevation
assert elevation != 50
await hass.services.async_call(
"homeassistant",
"set_location",
Expand All @@ -314,6 +316,15 @@ async def test_setting_location(hass: HomeAssistant) -> None:
assert len(events) == 1
assert hass.config.latitude == 30
assert hass.config.longitude == 40
assert hass.config.elevation == elevation

await hass.services.async_call(
"homeassistant",
"set_location",
{"latitude": 30, "longitude": 40, "elevation": 50},
blocking=True,
)
assert hass.config.elevation == 50


async def test_require_admin(
Expand Down

0 comments on commit ee65aa9

Please sign in to comment.