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

Add service to update core location #24328

Merged
merged 2 commits into from
Jun 7, 2019
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
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