From 7c5da67d74356a7f1d5fcf0437d18e1112664243 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 7 Jun 2019 07:09:26 -0700 Subject: [PATCH] Add service to update core location (#24328) * Add service to update core location * Update test_init.py --- .../components/homeassistant/__init__.py | 22 ++++++++++++++++--- tests/components/homeassistant/test_init.py | 20 +++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index 93a197969ca..2bcacb48bd1 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -14,7 +14,7 @@ from homeassistant.helpers import intent from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE, SERVICE_HOMEASSISTANT_STOP, SERVICE_HOMEASSISTANT_RESTART, - RESTART_EXIT_CODE) + RESTART_EXIT_CODE, ATTR_LATITUDE, ATTR_LONGITUDE) from homeassistant.helpers import config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -22,6 +22,7 @@ DOMAIN = ha.DOMAIN SERVICE_RELOAD_CORE_CONFIG = 'reload_core_config' SERVICE_CHECK_CONFIG = 'check_config' SERVICE_UPDATE_ENTITY = 'update_entity' +SERVICE_SET_LOCATION = 'set_location' SCHEMA_UPDATE_ENTITY = vol.Schema({ ATTR_ENTITY_ID: cv.entity_ids }) @@ -131,7 +132,22 @@ async def async_setup(hass: ha.HomeAssistant, config: dict) -> Awaitable[bool]: await conf_util.async_process_ha_core_config( hass, conf.get(ha.DOMAIN) or {}) - hass.services.async_register( - ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config) + hass.helpers.service.async_register_admin_service( + ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config + ) + + async def async_set_location(call): + """Service handler to set location.""" + await hass.config.async_update( + latitude=call.data[ATTR_LATITUDE], + longitude=call.data[ATTR_LONGITUDE], + ) + + hass.helpers.service.async_register_admin_service( + ha.DOMAIN, SERVICE_SET_LOCATION, async_set_location, vol.Schema({ + ATTR_LATITUDE: cv.latitude, + ATTR_LONGITUDE: cv.longitude, + }) + ) return True diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index b72589e60e3..0eeabd252fd 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -10,7 +10,7 @@ from homeassistant import config from homeassistant.const import ( ATTR_ENTITY_ID, STATE_ON, STATE_OFF, SERVICE_HOMEASSISTANT_RESTART, SERVICE_HOMEASSISTANT_STOP, SERVICE_TURN_ON, SERVICE_TURN_OFF, - SERVICE_TOGGLE) + SERVICE_TOGGLE, EVENT_CORE_CONFIG_UPDATE) import homeassistant.components as comps from homeassistant.setup import async_setup_component from homeassistant.components.homeassistant import ( @@ -22,7 +22,7 @@ from homeassistant.util.async_ import run_coroutine_threadsafe from tests.common import ( get_test_home_assistant, mock_service, patch_yaml_files, mock_coro, - async_mock_service) + async_mock_service, async_capture_events) def turn_on(hass, entity_id=None, **service_data): @@ -371,3 +371,19 @@ async def test_entity_update(hass): assert len(mock_update.mock_calls) == 1 assert mock_update.mock_calls[0][1][1] == 'light.kitchen' + + +async def test_setting_location(hass): + """Test setting the location.""" + await async_setup_component(hass, 'homeassistant', {}) + events = async_capture_events(hass, EVENT_CORE_CONFIG_UPDATE) + # Just to make sure that we are updating values. + assert hass.config.latitude != 30 + assert hass.config.longitude != 40 + await hass.services.async_call('homeassistant', 'set_location', { + 'latitude': 30, + 'longitude': 40, + }, blocking=True) + assert len(events) == 1 + assert hass.config.latitude == 30 + assert hass.config.longitude == 40