Skip to content

Commit

Permalink
Add service to update core location (#24328)
Browse files Browse the repository at this point in the history
* Add service to update core location

* Update test_init.py
  • Loading branch information
balloob authored and andrewsayre committed Jun 7, 2019
1 parent b71baef commit 7c5da67
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 19 additions & 3 deletions homeassistant/components/homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
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__)
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
})
Expand Down Expand Up @@ -131,7 +132,22 @@ async def async_handle_reload_config(call):
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
20 changes: 18 additions & 2 deletions tests/components/homeassistant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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 (
Expand All @@ -22,7 +22,7 @@

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):
Expand Down Expand Up @@ -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

0 comments on commit 7c5da67

Please sign in to comment.