Update to latest Somfy changes (#28207)

* Update to latest Somfy changes

* Update api.py

* Update api.py
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-11-06 10:55:56 -08:00
1 parent 9a0a889492
commit d9edd42532
6 files changed
+16 -45

No files matched your search

+8 -29
View File
@@ -1,15 +1,14 @@
"""API for Somfy bound to HASS OAuth."""
from asyncio import run_coroutine_threadsafe
from functools import partial
from typing import Dict, Union
import requests
from pymfy.api import somfy_api
from homeassistant import core, config_entries
from homeassistant.helpers import config_entry_oauth2_flow
class ConfigEntrySomfyApi(somfy_api.AbstractSomfyApi):
class ConfigEntrySomfyApi(somfy_api.SomfyApi):
"""Provide a Somfy API tied into an OAuth2 based config entry."""
def __init__(
@@ -24,32 +23,12 @@ class ConfigEntrySomfyApi(somfy_api.AbstractSomfyApi):
self.session = config_entry_oauth2_flow.OAuth2Session(
hass, config_entry, implementation
)
super().__init__(None, None, token=self.session.token)
def get(self, path):
"""Fetch a URL from the Somfy API."""
return run_coroutine_threadsafe(
self._request("get", path), self.hass.loop
def refresh_tokens(self,) -> Dict[str, Union[str, int]]:
"""Refresh and return new Somfy tokens using Home Assistant OAuth2 session."""
run_coroutine_threadsafe(
self.session.async_ensure_token_valid(), self.hass.loop
).result()
def post(self, path, *, json):
"""Post data to the Somfy API."""
return run_coroutine_threadsafe(
self._request("post", path, json=json), self.hass.loop
).result()
async def _request(self, method, path, **kwargs):
"""Make a request."""
await self.session.async_ensure_token_valid()
return await self.hass.async_add_executor_job(
partial(
requests.request,
method,
f"{self.base_url}{path}",
**kwargs,
headers={
**kwargs.get("headers", {}),
"authorization": f"Bearer {self.config_entry.data['token']['access_token']}",
},
)
)
return self.session.token