Skip to content

Commit

Permalink
Allow setting the elevation in set_location
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieger committed Sep 9, 2023
1 parent f903cd6 commit 44b6094
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 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 @@ -251,15 +252,23 @@ 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]
latitude=call.data[ATTR_LATITUDE],
longitude=call.data[ATTR_LONGITUDE],
elevation=call.data.get(ATTR_ELEVATION, None),
)

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
8 changes: 8 additions & 0 deletions tests/components/homeassistant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ 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 != 50
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 44b6094

Please sign in to comment.