Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the elevation in set_location #99978

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
jrieger marked this conversation as resolved.
Show resolved Hide resolved

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:
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

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