mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Update to latest Somfy changes (#28207)
* Update to latest Somfy changes * Update api.py * Update api.py
This commit is contained in:
parent
9a0a889492
commit
d9edd42532
@ -9,6 +9,7 @@ import logging
|
||||
from datetime import timedelta
|
||||
|
||||
import voluptuous as vol
|
||||
from requests import HTTPError
|
||||
|
||||
from homeassistant.helpers import config_validation as cv, config_entry_oauth2_flow
|
||||
from homeassistant.components.somfy import config_flow
|
||||
@ -156,13 +157,8 @@ class SomfyEntity(Entity):
|
||||
@Throttle(SCAN_INTERVAL)
|
||||
async def update_all_devices(hass):
|
||||
"""Update all the devices."""
|
||||
from requests import HTTPError
|
||||
from oauthlib.oauth2 import TokenExpiredError
|
||||
|
||||
try:
|
||||
data = hass.data[DOMAIN]
|
||||
data[DEVICES] = await hass.async_add_executor_job(data[API].get_devices)
|
||||
except TokenExpiredError:
|
||||
_LOGGER.warning("Cannot update devices due to expired token")
|
||||
except HTTPError:
|
||||
_LOGGER.warning("Cannot update devices")
|
||||
except HTTPError as err:
|
||||
_LOGGER.warning("Cannot update devices: %s", err.response.status_code)
|
||||
|
@ -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
|
||||
|
@ -4,6 +4,8 @@ Support for Somfy Covers.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/cover.somfy/
|
||||
"""
|
||||
from pymfy.api.devices.category import Category
|
||||
from pymfy.api.devices.blind import Blind
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
CoverDevice,
|
||||
@ -18,8 +20,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
|
||||
def get_covers():
|
||||
"""Retrieve covers."""
|
||||
from pymfy.api.devices.category import Category
|
||||
|
||||
categories = {
|
||||
Category.ROLLER_SHUTTER.value,
|
||||
Category.INTERIOR_BLIND.value,
|
||||
@ -51,15 +51,11 @@ class SomfyCover(SomfyEntity, CoverDevice):
|
||||
|
||||
def __init__(self, device, api):
|
||||
"""Initialize the Somfy device."""
|
||||
from pymfy.api.devices.blind import Blind
|
||||
|
||||
super().__init__(device, api)
|
||||
self.cover = Blind(self.device, self.api)
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the device with the latest data."""
|
||||
from pymfy.api.devices.blind import Blind
|
||||
|
||||
await super().async_update()
|
||||
self.cover = Blind(self.device, self.api)
|
||||
|
||||
|
@ -5,5 +5,5 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/somfy",
|
||||
"dependencies": ["http"],
|
||||
"codeowners": ["@tetienne"],
|
||||
"requirements": ["pymfy==0.6.0"]
|
||||
"requirements": ["pymfy==0.6.1"]
|
||||
}
|
||||
|
@ -1313,7 +1313,7 @@ pymailgunner==1.4
|
||||
pymediaroom==0.6.4
|
||||
|
||||
# homeassistant.components.somfy
|
||||
pymfy==0.6.0
|
||||
pymfy==0.6.1
|
||||
|
||||
# homeassistant.components.xiaomi_tv
|
||||
pymitv==1.4.3
|
||||
|
@ -439,7 +439,7 @@ pylitejet==0.1
|
||||
pymailgunner==1.4
|
||||
|
||||
# homeassistant.components.somfy
|
||||
pymfy==0.6.0
|
||||
pymfy==0.6.1
|
||||
|
||||
# homeassistant.components.mochad
|
||||
pymochad==0.2.0
|
||||
|
Loading…
x
Reference in New Issue
Block a user