From f744a29d9d950fcf6e60525fc31ad18f1a700159 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 15 Jun 2018 13:37:46 -0400 Subject: [PATCH] Add calendar panel, add tests (#14973) --- homeassistant/components/calendar/__init__.py | 46 +++++++++++++++---- homeassistant/components/calendar/demo.py | 4 +- tests/components/calendar/test_demo.py | 23 ---------- tests/components/calendar/test_init.py | 37 +++++++++++++++ 4 files changed, 75 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/calendar/__init__.py b/homeassistant/components/calendar/__init__.py index f5e1d581891..65e5e33c7c1 100644 --- a/homeassistant/components/calendar/__init__.py +++ b/homeassistant/components/calendar/__init__.py @@ -4,7 +4,6 @@ Support for Google Calendar event device sensors. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/calendar/ """ -import asyncio import logging from datetime import timedelta import re @@ -34,15 +33,18 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}' SCAN_INTERVAL = timedelta(seconds=60) -@asyncio.coroutine -def async_setup(hass, config): +async def async_setup(hass, config): """Track states and offer events for calendars.""" component = EntityComponent( _LOGGER, DOMAIN, hass, SCAN_INTERVAL, DOMAIN) + hass.http.register_view(CalendarListView(component)) hass.http.register_view(CalendarEventView(component)) - yield from component.async_setup(config) + await hass.components.frontend.async_register_built_in_panel( + 'calendar', 'calendar', 'hass:calendar') + + await component.async_setup(config) return True @@ -196,8 +198,8 @@ class CalendarEventDevice(Entity): class CalendarEventView(http.HomeAssistantView): """View to retrieve calendar content.""" - url = '/api/calendar/{entity_id}' - name = 'api:calendar' + url = '/api/calendars/{entity_id}' + name = 'api:calendars:calendar' def __init__(self, component): """Initialize calendar view.""" @@ -205,7 +207,7 @@ class CalendarEventView(http.HomeAssistantView): async def get(self, request, entity_id): """Return calendar events.""" - entity = self.component.get_entity('calendar.' + entity_id) + entity = self.component.get_entity(entity_id) start = request.query.get('start') end = request.query.get('end') if None in (start, end, entity): @@ -215,7 +217,31 @@ class CalendarEventView(http.HomeAssistantView): end_date = dt.parse_datetime(end) except (ValueError, AttributeError): return web.Response(status=400) - event_list = await entity.async_get_events(request.app['hass'], - start_date, - end_date) + event_list = await entity.async_get_events( + request.app['hass'], start_date, end_date) return self.json(event_list) + + +class CalendarListView(http.HomeAssistantView): + """View to retrieve calendar list.""" + + url = '/api/calendars' + name = "api:calendars" + + def __init__(self, component): + """Initialize calendar view.""" + self.component = component + + async def get(self, request): + """Retrieve calendar list.""" + get_state = request.app['hass'].states.get + calendar_list = [] + + for entity in self.component.entities: + state = get_state(entity.entity_id) + calendar_list.append({ + "name": state.name, + "entity_id": entity.entity_id, + }) + + return self.json(sorted(calendar_list, key=lambda x: x['name'])) diff --git a/homeassistant/components/calendar/demo.py b/homeassistant/components/calendar/demo.py index 5ddd9fe8e3d..53129d3316c 100644 --- a/homeassistant/components/calendar/demo.py +++ b/homeassistant/components/calendar/demo.py @@ -17,12 +17,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): calendar_data_current = DemoGoogleCalendarDataCurrent() add_devices([ DemoGoogleCalendar(hass, calendar_data_future, { - CONF_NAME: 'Future Event', + CONF_NAME: 'Calendar 1', CONF_DEVICE_ID: 'calendar_1', }), DemoGoogleCalendar(hass, calendar_data_current, { - CONF_NAME: 'Current Event', + CONF_NAME: 'Calendar 2', CONF_DEVICE_ID: 'calendar_2', }), ]) diff --git a/tests/components/calendar/test_demo.py b/tests/components/calendar/test_demo.py index 50ac63121b1..09c6a06a54e 100644 --- a/tests/components/calendar/test_demo.py +++ b/tests/components/calendar/test_demo.py @@ -1,24 +1 @@ """The tests for the demo calendar component.""" -from datetime import timedelta - -from homeassistant.bootstrap import async_setup_component -import homeassistant.util.dt as dt_util - - -async def test_api_calendar_demo_view(hass, aiohttp_client): - """Test the calendar demo view.""" - await async_setup_component(hass, 'calendar', - {'calendar': {'platform': 'demo'}}) - client = await aiohttp_client(hass.http.app) - response = await client.get( - '/api/calendar/calendar_2') - assert response.status == 400 - start = dt_util.now() - end = start + timedelta(days=1) - response = await client.get( - '/api/calendar/calendar_1?start={}&end={}'.format(start.isoformat(), - end.isoformat())) - assert response.status == 200 - events = await response.json() - assert events[0]['summary'] == 'Future Event' - assert events[0]['title'] == 'Future Event' diff --git a/tests/components/calendar/test_init.py b/tests/components/calendar/test_init.py index 164c3f57f52..a5f6a751b46 100644 --- a/tests/components/calendar/test_init.py +++ b/tests/components/calendar/test_init.py @@ -1 +1,38 @@ """The tests for the calendar component.""" +from datetime import timedelta + +from homeassistant.bootstrap import async_setup_component +import homeassistant.util.dt as dt_util + + +async def test_events_http_api(hass, aiohttp_client): + """Test the calendar demo view.""" + await async_setup_component(hass, 'calendar', + {'calendar': {'platform': 'demo'}}) + client = await aiohttp_client(hass.http.app) + response = await client.get( + '/api/calendars/calendar.calendar_2') + assert response.status == 400 + start = dt_util.now() + end = start + timedelta(days=1) + response = await client.get( + '/api/calendars/calendar.calendar_1?start={}&end={}'.format( + start.isoformat(), end.isoformat())) + assert response.status == 200 + events = await response.json() + assert events[0]['summary'] == 'Future Event' + assert events[0]['title'] == 'Future Event' + + +async def test_calendars_http_api(hass, aiohttp_client): + """Test the calendar demo view.""" + await async_setup_component(hass, 'calendar', + {'calendar': {'platform': 'demo'}}) + client = await aiohttp_client(hass.http.app) + response = await client.get('/api/calendars') + assert response.status == 200 + data = await response.json() + assert data == [ + {'entity_id': 'calendar.calendar_1', 'name': 'Calendar 1'}, + {'entity_id': 'calendar.calendar_2', 'name': 'Calendar 2'} + ]