Add calendar panel, add tests (#14973)
This commit is contained in:
parent
3cd4cb741c
commit
f744a29d9d
4 changed files with 75 additions and 35 deletions
|
@ -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']))
|
||||
|
|
|
@ -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',
|
||||
}),
|
||||
])
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'}
|
||||
]
|
||||
|
|
Loading…
Add table
Reference in a new issue