1 """Support for the Foursquare (Swarm) API."""
3 from http
import HTTPStatus
6 from aiohttp
import web
8 import voluptuous
as vol
16 _LOGGER = logging.getLogger(__name__)
18 CONF_PUSH_SECRET =
"push_secret"
22 EVENT_CHECKIN =
"foursquare.checkin"
23 EVENT_PUSH =
"foursquare.push"
25 SERVICE_CHECKIN =
"checkin"
27 CHECKIN_SERVICE_SCHEMA = vol.Schema(
29 vol.Optional(
"alt"): cv.string,
30 vol.Optional(
"altAcc"): cv.string,
31 vol.Optional(
"broadcast"): cv.string,
32 vol.Optional(
"eventId"): cv.string,
33 vol.Optional(
"ll"): cv.string,
34 vol.Optional(
"llAcc"): cv.string,
35 vol.Optional(
"mentions"): cv.string,
36 vol.Optional(
"shout"): cv.string,
37 vol.Required(
"venueId"): cv.string,
41 CONFIG_SCHEMA = vol.Schema(
45 vol.Required(CONF_ACCESS_TOKEN): cv.string,
46 vol.Required(CONF_PUSH_SECRET): cv.string,
50 extra=vol.ALLOW_EXTRA,
54 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
55 """Set up the Foursquare component."""
56 config = config[DOMAIN]
58 def checkin_user(call: ServiceCall) ->
None:
59 """Check a user in on Swarm."""
60 url = f
"https://api.foursquare.com/v2/checkins/add?oauth_token={config[CONF_ACCESS_TOKEN]}&v=20160802&m=swarm"
61 response = requests.post(url, data=call.data, timeout=10)
63 if response.status_code
not in (HTTPStatus.OK, HTTPStatus.CREATED):
65 "Error checking in user. Response %d: %s:",
70 hass.bus.fire(EVENT_CHECKIN, {
"text": response.text})
73 hass.services.register(
74 DOMAIN,
"checkin", checkin_user, schema=CHECKIN_SERVICE_SCHEMA
83 """Handle pushes from the Foursquare API."""
86 url =
"/api/foursquare"
90 """Initialize the OAuth callback view."""
93 async
def post(self, request: web.Request) -> web.Response |
None:
94 """Accept the POST from Foursquare."""
96 data = await request.json()
98 return self.json_message(
"Invalid JSON", HTTPStatus.BAD_REQUEST)
100 secret = data.pop(
"secret",
None)
102 _LOGGER.debug(
"Received Foursquare push: %s", data)
106 "Received Foursquare push with invalid push secret: %s", secret
108 return self.json_message(
"Incorrect secret", HTTPStatus.BAD_REQUEST)
110 request.app[KEY_HASS].bus.async_fire(EVENT_PUSH, data)
web.Response|None post(self, web.Request request)
None __init__(self, str push_secret)
bool setup(HomeAssistant hass, ConfigType config)