Allow setting the elevation in set_location (#99978)

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Jan Rieger 2023-09-13 18:09:12 +02:00 committed by GitHub
parent f6b094dfee
commit ee65aa91e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 4 deletions

View File

@ -9,6 +9,7 @@ from homeassistant.auth.permissions.const import CAT_ENTITIES, POLICY_CONTROL
from homeassistant.components import persistent_notification from homeassistant.components import persistent_notification
import homeassistant.config as conf_util import homeassistant.config as conf_util
from homeassistant.const import ( from homeassistant.const import (
ATTR_ELEVATION,
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_LATITUDE, ATTR_LATITUDE,
ATTR_LONGITUDE, ATTR_LONGITUDE,
@ -250,16 +251,28 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
async def async_set_location(call: ha.ServiceCall) -> None: async def async_set_location(call: ha.ServiceCall) -> None:
"""Service handler to set location.""" """Service handler to set location."""
await hass.config.async_update( service_data = {
latitude=call.data[ATTR_LATITUDE], longitude=call.data[ATTR_LONGITUDE] "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( async_register_admin_service(
hass, hass,
ha.DOMAIN, ha.DOMAIN,
SERVICE_SET_LOCATION, SERVICE_SET_LOCATION,
async_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: async def async_handle_reload_templates(call: ha.ServiceCall) -> None:

View File

@ -13,6 +13,11 @@ set_location:
example: 117.22743 example: 117.22743
selector: selector:
text: text:
elevation:
required: false
example: 120
selector:
text:
stop: stop:
toggle: toggle:

View File

@ -81,6 +81,10 @@
"longitude": { "longitude": {
"name": "[%key:common::config_flow::data::longitude%]", "name": "[%key:common::config_flow::data::longitude%]",
"description": "Longitude of your location." "description": "Longitude of your location."
},
"elevation": {
"name": "[%key:common::config_flow::data::elevation%]",
"description": "Elevation of your location."
} }
} }
}, },

View File

@ -460,6 +460,9 @@ ATTR_HIDDEN: Final = "hidden"
ATTR_LATITUDE: Final = "latitude" ATTR_LATITUDE: Final = "latitude"
ATTR_LONGITUDE: Final = "longitude" ATTR_LONGITUDE: Final = "longitude"
# Elevation of the entity
ATTR_ELEVATION: Final = "elevation"
# Accuracy of location in meters # Accuracy of location in meters
ATTR_GPS_ACCURACY: Final = "gps_accuracy" ATTR_GPS_ACCURACY: Final = "gps_accuracy"

View File

@ -305,6 +305,8 @@ async def test_setting_location(hass: HomeAssistant) -> None:
# Just to make sure that we are updating values. # Just to make sure that we are updating values.
assert hass.config.latitude != 30 assert hass.config.latitude != 30
assert hass.config.longitude != 40 assert hass.config.longitude != 40
elevation = hass.config.elevation
assert elevation != 50
await hass.services.async_call( await hass.services.async_call(
"homeassistant", "homeassistant",
"set_location", "set_location",
@ -314,6 +316,15 @@ async def test_setting_location(hass: HomeAssistant) -> None:
assert len(events) == 1 assert len(events) == 1
assert hass.config.latitude == 30 assert hass.config.latitude == 30
assert hass.config.longitude == 40 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( async def test_require_admin(