From 44b6094f0083303dce8f89499683e1aa567a2969 Mon Sep 17 00:00:00 2001 From: Jan Rieger Date: Sat, 9 Sep 2023 14:54:54 +0200 Subject: [PATCH 1/5] Allow setting the elevation in `set_location` --- homeassistant/components/homeassistant/__init__.py | 13 +++++++++++-- .../components/homeassistant/services.yaml | 5 +++++ homeassistant/components/homeassistant/strings.json | 4 ++++ homeassistant/const.py | 3 +++ tests/components/homeassistant/test_init.py | 8 ++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index 987a4317ba84a..f33018a4b1d7a 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -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, @@ -251,7 +252,9 @@ 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( @@ -259,7 +262,13 @@ async def async_set_location(call: ha.ServiceCall) -> None: 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: diff --git a/homeassistant/components/homeassistant/services.yaml b/homeassistant/components/homeassistant/services.yaml index 899fee357fd52..09a280133f218 100644 --- a/homeassistant/components/homeassistant/services.yaml +++ b/homeassistant/components/homeassistant/services.yaml @@ -13,6 +13,11 @@ set_location: example: 117.22743 selector: text: + elevation: + required: false + example: 120 + selector: + text: stop: toggle: diff --git a/homeassistant/components/homeassistant/strings.json b/homeassistant/components/homeassistant/strings.json index 5404ee4af6496..53510a94f0195 100644 --- a/homeassistant/components/homeassistant/strings.json +++ b/homeassistant/components/homeassistant/strings.json @@ -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." } } }, diff --git a/homeassistant/const.py b/homeassistant/const.py index 70f7827143b12..de968451af9f4 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -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" diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 652fc4a1fdda7..54432ad7c113f 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -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( From ecd317b6f6b830868600b45eea65f318b8c15877 Mon Sep 17 00:00:00 2001 From: Jan Rieger Date: Sat, 9 Sep 2023 17:12:45 +0200 Subject: [PATCH 2/5] Set parameter only if service call data includes elevation --- homeassistant/components/homeassistant/__init__.py | 14 +++++++++++--- tests/components/homeassistant/test_init.py | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index f33018a4b1d7a..1820591c399ed 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -252,11 +252,19 @@ 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], - elevation=call.data.get(ATTR_ELEVATION, None), + latitude=call.data[ATTR_LATITUDE], longitude=call.data[ATTR_LONGITUDE] ) + service_data = { + "latitude": call.data[ATTR_LATITUDE], + "longitude": call.data[ATTR_LONGITUDE], + } + + if call.data.get(ATTR_ELEVATION): + service_data["elevation"] = call.data[ATTR_ELEVATION] + + await hass.config.async_update(**service_data) + async_register_admin_service( hass, ha.DOMAIN, diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 54432ad7c113f..60de790119c48 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -311,7 +311,7 @@ async def test_setting_location(hass: HomeAssistant) -> None: {"latitude": 30, "longitude": 40}, blocking=True, ) - assert len(events) == 1 + assert len(events) == 2 assert hass.config.latitude == 30 assert hass.config.longitude == 40 assert hass.config.elevation != 50 From 636277bfad28fac5ab1947455011314a541d3aaa Mon Sep 17 00:00:00 2001 From: Jan Rieger Date: Sat, 9 Sep 2023 17:40:48 +0200 Subject: [PATCH 3/5] Remove old code --- homeassistant/components/homeassistant/__init__.py | 4 ---- tests/components/homeassistant/test_init.py | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index 1820591c399ed..8f12db80f2841 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -251,10 +251,6 @@ 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], diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 60de790119c48..54432ad7c113f 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -311,7 +311,7 @@ async def test_setting_location(hass: HomeAssistant) -> None: {"latitude": 30, "longitude": 40}, blocking=True, ) - assert len(events) == 2 + assert len(events) == 1 assert hass.config.latitude == 30 assert hass.config.longitude == 40 assert hass.config.elevation != 50 From 4f5fc493e23e548c9df63828cbae04864400bc50 Mon Sep 17 00:00:00 2001 From: Jan Rieger Date: Tue, 12 Sep 2023 16:40:28 +0200 Subject: [PATCH 4/5] Small test improvement --- tests/components/homeassistant/test_init.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 54432ad7c113f..4c5643ae3ca5b 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -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", @@ -314,7 +316,8 @@ 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 + assert hass.config.elevation == elevation + await hass.services.async_call( "homeassistant", "set_location", From 7084693e8f6c2c1c3b9c26ae62bb3f2f520566e6 Mon Sep 17 00:00:00 2001 From: Jan Rieger Date: Tue, 12 Sep 2023 16:49:27 +0200 Subject: [PATCH 5/5] Update homeassistant/components/homeassistant/__init__.py Co-authored-by: G Johansson --- homeassistant/components/homeassistant/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index 8f12db80f2841..e4032ad954d29 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -256,8 +256,8 @@ async def async_set_location(call: ha.ServiceCall) -> None: "longitude": call.data[ATTR_LONGITUDE], } - if call.data.get(ATTR_ELEVATION): - service_data["elevation"] = call.data[ATTR_ELEVATION] + if elevation := call.data.get(ATTR_ELEVATION): + service_data["elevation"] = elevation await hass.config.async_update(**service_data)