Skip to content

Commit 53cbe9c

Browse files
committed
Add function docs
1 parent 7c24752 commit 53cbe9c

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

hackspaceapi/events.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime
22
from enum import Enum
3-
from typing import List, Literal
3+
from typing import Iterable, Literal
44

55
import arrow
66
from fastapi import APIRouter, Response
@@ -19,7 +19,10 @@ class CalendarType(str, Enum):
1919
members = settings.hackspace_member_calendar
2020

2121

22-
def get_calendar_events(start: datetime, end: datetime, calendar: str) -> List:
22+
def get_calendar_events(start: datetime, end: datetime, calendar: str) -> Iterable:
23+
"""
24+
Get a list of calendar events from Home Assistant via the API.
25+
"""
2326
data = call_homeassistant(
2427
"/api/calendars/{0}".format(calendar.value), start=start, end=end
2528
)

hackspaceapi/services/homeassistant.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@call_homeassistant_metrics.time()
2727
def call_homeassistant(endpoint: str, **params) -> Optional[Iterable]:
2828
"""
29-
Call a Homeassistant API endpoint and return the JSON if successful
29+
Call a Home Assistant API endpoint and return the JSON if successful
3030
"""
3131
url = urljoin(settings.homeassistant_instance, endpoint)
3232
try:
@@ -40,4 +40,7 @@ def call_homeassistant(endpoint: str, **params) -> Optional[Iterable]:
4040

4141

4242
def get_entity_state(entity_id: str) -> Optional[Iterable]:
43+
"""
44+
Call the configured Home Assistant API and retrieve the current state of a entity
45+
"""
4346
return call_homeassistant("/api/states/{0}".format(entity_id))

hackspaceapi/services/prometheus.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111

1212
session = get_requests_session()
1313

14-
prometheus_metric_summary = Summary('hackspaceapi_prometheus_api_time', 'Summary of calls to the Prometheus API')
14+
prometheus_metric_summary = Summary(
15+
"hackspaceapi_prometheus_api_time", "Summary of calls to the Prometheus API"
16+
)
17+
1518

1619
@ttl_cache(ttl=60)
1720
@prometheus_metric_summary.time()
1821
def get_prometheus_metric(query: str) -> Optional[Dict]:
22+
"""
23+
Call the configured Prometheus endpoint with a query, and return the
24+
resulting data if successful.
25+
"""
1926
url = urljoin(settings.prometheus_instance, "/api/v1/query")
2027
try:
2128
resp = session.get(url, params={"query": query})

hackspaceapi/services/website.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
@ttl_cache(ttl=1800)
1818
@website_metric_summary.time()
1919
def get_membership_data() -> Optional[Iterable]:
20+
"""
21+
Pull the JSON formatted membership plan data from the Leigh Hackspace
22+
website.
23+
"""
2024
url = "https://web-test.leighhack.org/membership/index.json"
2125
try:
2226
resp = session.get(url)

hackspaceapi/spaceapi.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
spaceapi = APIRouter()
1414

15-
# Homeassistant Sensors to export to the Space API
15+
# Home Assistant Sensors to export to the Space API
1616
# entity_id, override_name
1717
HOMEASSISTANT_SENSORS = (
1818
("sensor.gw_dhcp_leases_online", "WiFi Clients"),
@@ -32,21 +32,28 @@
3232

3333

3434
def get_state() -> dict:
35+
"""
36+
Return the current open state of the Hackspace.
37+
"""
3538
data = get_entity_state(settings.hackspace_open_entity)
3639
if data:
3740
return {
3841
"open": data["state"] == "on",
3942
"lastchange": int(arrow.get(data["last_changed"]).timestamp()),
4043
}
4144

42-
# We didn't get a valid response from Homeassistant, assume we're closed
45+
# We didn't get a valid response from Home Assistant, assume we're closed
4346
return {
4447
"open": False,
4548
}
4649

4750

4851
@ttl_cache(ttl=60)
4952
def get_sensors() -> dict:
53+
"""
54+
Query Home Assistant and Prometheus to build the 'sensors' section of the
55+
SpaceAPI response.
56+
"""
5057
results = {}
5158
for sensor, override_name in HOMEASSISTANT_SENSORS:
5259
data = get_entity_state(sensor)
@@ -202,6 +209,9 @@ def get_sensors() -> dict:
202209

203210

204211
def get_links() -> list:
212+
"""
213+
Return a list of links to add to the 'links' value of the SpaceAPI response.
214+
"""
205215
return [
206216
{"name": "Github", "url": "https://github.com/leigh-hackspace"},
207217
{
@@ -214,6 +224,9 @@ def get_links() -> list:
214224

215225

216226
def get_membership_plans() -> list:
227+
"""
228+
Format the website membership plan data into a SpaceAPI compatible format.
229+
"""
217230
data = get_membership_data()
218231
if not data:
219232
return []

0 commit comments

Comments
 (0)