Skip to content

Commit

Permalink
Add a new mobile_app webhook command to get config (home-assistant#22813
Browse files Browse the repository at this point in the history
)

* Add a new mobile_app webhook command to get config

* Limit fields returned
  • Loading branch information
robbiet480 authored and awarecan committed Apr 7, 2019
1 parent c8eebb6 commit 3ce6be6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/mobile_app/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

WEBHOOK_TYPE_CALL_SERVICE = 'call_service'
WEBHOOK_TYPE_FIRE_EVENT = 'fire_event'
WEBHOOK_TYPE_GET_CONFIG = 'get_config'
WEBHOOK_TYPE_GET_ZONES = 'get_zones'
WEBHOOK_TYPE_REGISTER_SENSOR = 'register_sensor'
WEBHOOK_TYPE_RENDER_TEMPLATE = 'render_template'
Expand All @@ -75,8 +76,9 @@
WEBHOOK_TYPE_UPDATE_SENSOR_STATES = 'update_sensor_states'

WEBHOOK_TYPES = [WEBHOOK_TYPE_CALL_SERVICE, WEBHOOK_TYPE_FIRE_EVENT,
WEBHOOK_TYPE_GET_ZONES, WEBHOOK_TYPE_REGISTER_SENSOR,
WEBHOOK_TYPE_RENDER_TEMPLATE, WEBHOOK_TYPE_UPDATE_LOCATION,
WEBHOOK_TYPE_GET_CONFIG, WEBHOOK_TYPE_GET_ZONES,
WEBHOOK_TYPE_REGISTER_SENSOR, WEBHOOK_TYPE_RENDER_TEMPLATE,
WEBHOOK_TYPE_UPDATE_LOCATION,
WEBHOOK_TYPE_UPDATE_REGISTRATION,
WEBHOOK_TYPE_UPDATE_SENSOR_STATES]

Expand Down
24 changes: 20 additions & 4 deletions homeassistant/components/mobile_app/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ATTR_DEV_ID,
DOMAIN as DT_DOMAIN,
SERVICE_SEE as DT_SEE)

from homeassistant.components.frontend import MANIFEST_JSON
from homeassistant.components.zone.const import DOMAIN as ZONE_DOMAIN

from homeassistant.const import (ATTR_DOMAIN, ATTR_SERVICE, ATTR_SERVICE_DATA,
Expand Down Expand Up @@ -36,9 +36,9 @@
ERR_SENSOR_DUPLICATE_UNIQUE_ID, ERR_SENSOR_NOT_REGISTERED,
SIGNAL_SENSOR_UPDATE, WEBHOOK_PAYLOAD_SCHEMA,
WEBHOOK_SCHEMAS, WEBHOOK_TYPES, WEBHOOK_TYPE_CALL_SERVICE,
WEBHOOK_TYPE_FIRE_EVENT, WEBHOOK_TYPE_GET_ZONES,
WEBHOOK_TYPE_REGISTER_SENSOR, WEBHOOK_TYPE_RENDER_TEMPLATE,
WEBHOOK_TYPE_UPDATE_LOCATION,
WEBHOOK_TYPE_FIRE_EVENT, WEBHOOK_TYPE_GET_CONFIG,
WEBHOOK_TYPE_GET_ZONES, WEBHOOK_TYPE_REGISTER_SENSOR,
WEBHOOK_TYPE_RENDER_TEMPLATE, WEBHOOK_TYPE_UPDATE_LOCATION,
WEBHOOK_TYPE_UPDATE_REGISTRATION,
WEBHOOK_TYPE_UPDATE_SENSOR_STATES)

Expand Down Expand Up @@ -273,3 +273,19 @@ async def handle_webhook(hass: HomeAssistantType, webhook_id: str,
in sorted(hass.states.async_entity_ids(ZONE_DOMAIN)))
return webhook_response(list(zones), registration=registration,
headers=headers)

if webhook_type == WEBHOOK_TYPE_GET_CONFIG:

hass_config = hass.config.as_dict()

return webhook_response({
'latitude': hass_config['latitude'],
'longitude': hass_config['longitude'],
'elevation': hass_config['elevation'],
'unit_system': hass_config['unit_system'],
'location_name': hass_config['location_name'],
'time_zone': hass_config['time_zone'],
'components': hass_config['components'],
'version': hass_config['version'],
'theme_color': MANIFEST_JSON['theme_color'],
}, registration=registration, headers=headers)
34 changes: 34 additions & 0 deletions tests/components/mobile_app/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,40 @@ async def test_webhook_handle_get_zones(hass, create_registrations, # noqa: F40
assert json[0]['entity_id'] == 'zone.home'


async def test_webhook_handle_get_config(hass, create_registrations, # noqa: F401, F811, E501
webhook_client): # noqa: F811
"""Test that we can get config properly."""
resp = await webhook_client.post(
'/api/webhook/{}'.format(create_registrations[1]['webhook_id']),
json={'type': 'get_config'}
)

assert resp.status == 200

json = await resp.json()
if 'components' in json:
json['components'] = set(json['components'])
if 'whitelist_external_dirs' in json:
json['whitelist_external_dirs'] = \
set(json['whitelist_external_dirs'])

hass_config = hass.config.as_dict()

expected_dict = {
'latitude': hass_config['latitude'],
'longitude': hass_config['longitude'],
'elevation': hass_config['elevation'],
'unit_system': hass_config['unit_system'],
'location_name': hass_config['location_name'],
'time_zone': hass_config['time_zone'],
'components': hass_config['components'],
'version': hass_config['version'],
'theme_color': '#03A9F4', # Default frontend theme color
}

assert expected_dict == json


async def test_webhook_returns_error_incorrect_json(webhook_client, # noqa: F401, F811, E501
create_registrations, # noqa: F401, F811, E501
caplog): # noqa: E501 F811
Expand Down

0 comments on commit 3ce6be6

Please sign in to comment.