From f855ff8751c8dbc869be019b744ecd831fee6412 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Wed, 21 Oct 2020 13:37:17 +0200 Subject: [PATCH] Bump aioshelly library to 0.4.0 (#41905) Co-authored-by: Paulus Schoutsen --- homeassistant/components/shelly/__init__.py | 47 ++++++++----------- .../components/shelly/config_flow.py | 5 +- homeassistant/components/shelly/const.py | 2 + homeassistant/components/shelly/cover.py | 4 +- homeassistant/components/shelly/entity.py | 6 ++- homeassistant/components/shelly/light.py | 4 +- homeassistant/components/shelly/manifest.json | 2 +- homeassistant/components/shelly/switch.py | 4 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index c63858f0652..628ee5a53cc 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -3,7 +3,7 @@ import asyncio from datetime import timedelta import logging -from aiocoap import error as aiocoap_error +import aiocoap import aioshelly import async_timeout @@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import aiohttp_client, device_registry, update_coordinator -from .const import DOMAIN +from .const import COAP_CONTEXT, DATA_CONFIG_ENTRY, DOMAIN PLATFORMS = ["binary_sensor", "cover", "light", "sensor", "switch"] _LOGGER = logging.getLogger(__name__) @@ -26,7 +26,15 @@ _LOGGER = logging.getLogger(__name__) async def async_setup(hass: HomeAssistant, config: dict): """Set up the Shelly component.""" - hass.data[DOMAIN] = {} + hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}} + hass.data[DOMAIN][COAP_CONTEXT] = await aiocoap.Context.create_client_context() + + async def shutdown_listener(*_): + """Home Assistant shutdown listener.""" + await hass.data[DOMAIN][COAP_CONTEXT].shutdown() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_listener) + return True @@ -39,18 +47,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): entry.data.get(CONF_PASSWORD), temperature_unit, ) + + coap_context = hass.data[DOMAIN][COAP_CONTEXT] + try: async with async_timeout.timeout(10): device = await aioshelly.Device.create( aiohttp_client.async_get_clientsession(hass), + coap_context, options, ) except (asyncio.TimeoutError, OSError) as err: raise ConfigEntryNotReady from err - wrapper = hass.data[DOMAIN][entry.entry_id] = ShellyDeviceWrapper( - hass, entry, device - ) + wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][ + entry.entry_id + ] = ShellyDeviceWrapper(hass, entry, device) await wrapper.async_setup() for component in PLATFORMS: @@ -75,18 +87,14 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): self.hass = hass self.entry = entry self.device = device - self._unsub_stop = None async def _async_update_data(self): """Fetch data.""" - # Race condition on shutdown. Stop all the fetches. - if self._unsub_stop is None: - return None try: async with async_timeout.timeout(5): return await self.device.update() - except (aiocoap_error.Error, OSError) as err: + except (aiocoap.error.Error, OSError) as err: raise update_coordinator.UpdateFailed("Error fetching data") from err @property @@ -101,9 +109,6 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): async def async_setup(self): """Set up the wrapper.""" - self._unsub_stop = self.hass.bus.async_listen( - EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop - ) dev_reg = await device_registry.async_get_registry(self.hass) model_type = self.device.settings["device"]["type"] dev_reg.async_get_or_create( @@ -117,18 +122,6 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): sw_version=self.device.settings["fw"], ) - async def shutdown(self): - """Shutdown the device wrapper.""" - if self._unsub_stop: - self._unsub_stop() - self._unsub_stop = None - await self.device.shutdown() - - async def _handle_ha_stop(self, _): - """Handle Home Assistant stopping.""" - self._unsub_stop = None - await self.shutdown() - async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" @@ -141,6 +134,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): ) ) if unload_ok: - await hass.data[DOMAIN].pop(entry.entry_id).shutdown() + hass.data[DOMAIN][DATA_CONFIG_ENTRY].pop(entry.entry_id) return unload_ok diff --git a/homeassistant/components/shelly/config_flow.py b/homeassistant/components/shelly/config_flow.py index b13c4090a10..6fe1c28a2ef 100644 --- a/homeassistant/components/shelly/config_flow.py +++ b/homeassistant/components/shelly/config_flow.py @@ -2,6 +2,7 @@ import asyncio import logging +import aiocoap import aiohttp import aioshelly import async_timeout @@ -33,13 +34,15 @@ async def validate_input(hass: core.HomeAssistant, host, data): options = aioshelly.ConnectionOptions( host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD) ) + coap_context = await aiocoap.Context.create_client_context() async with async_timeout.timeout(5): device = await aioshelly.Device.create( aiohttp_client.async_get_clientsession(hass), + coap_context, options, ) - await device.shutdown() + await coap_context.shutdown() # Return info that you want to store in the config entry. return {"title": device.settings["name"], "mac": device.settings["device"]["mac"]} diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index 5c9a55915fe..b40ada04b30 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -1,3 +1,5 @@ """Constants for the Shelly integration.""" +COAP_CONTEXT = "coap_context" +DATA_CONFIG_ENTRY = "config_entry" DOMAIN = "shelly" diff --git a/homeassistant/components/shelly/cover.py b/homeassistant/components/shelly/cover.py index ec1933ba420..a65ab5a05af 100644 --- a/homeassistant/components/shelly/cover.py +++ b/homeassistant/components/shelly/cover.py @@ -12,13 +12,13 @@ from homeassistant.components.cover import ( from homeassistant.core import callback from . import ShellyDeviceWrapper -from .const import DOMAIN +from .const import DATA_CONFIG_ENTRY, DOMAIN from .entity import ShellyBlockEntity async def async_setup_entry(hass, config_entry, async_add_entities): """Set up cover for device.""" - wrapper = hass.data[DOMAIN][config_entry.entry_id] + wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] blocks = [block for block in wrapper.device.blocks if block.type == "roller"] if not blocks: diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 7ffd1aee052..8af95c0fb33 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -10,7 +10,7 @@ from homeassistant.core import callback from homeassistant.helpers import device_registry, entity from . import ShellyDeviceWrapper -from .const import DOMAIN +from .const import DATA_CONFIG_ENTRY, DOMAIN def temperature_unit(block_info: dict) -> str: @@ -64,7 +64,9 @@ async def async_setup_entry_attribute_entities( hass, config_entry, async_add_entities, sensors, sensor_class ): """Set up entities for block attributes.""" - wrapper: ShellyDeviceWrapper = hass.data[DOMAIN][config_entry.entry_id] + wrapper: ShellyDeviceWrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][ + config_entry.entry_id + ] blocks = [] for block in wrapper.device.blocks: diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index 8025fd11205..85763a51f2a 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -17,13 +17,13 @@ from homeassistant.util.color import ( ) from . import ShellyDeviceWrapper -from .const import DOMAIN +from .const import DATA_CONFIG_ENTRY, DOMAIN from .entity import ShellyBlockEntity async def async_setup_entry(hass, config_entry, async_add_entities): """Set up lights for device.""" - wrapper = hass.data[DOMAIN][config_entry.entry_id] + wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] blocks = [block for block in wrapper.device.blocks if block.type == "light"] if not blocks: diff --git a/homeassistant/components/shelly/manifest.json b/homeassistant/components/shelly/manifest.json index 3816d31222e..98dcbfda568 100644 --- a/homeassistant/components/shelly/manifest.json +++ b/homeassistant/components/shelly/manifest.json @@ -3,7 +3,7 @@ "name": "Shelly", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/shelly", - "requirements": ["aioshelly==0.3.4"], + "requirements": ["aioshelly==0.4.0"], "zeroconf": [{ "type": "_http._tcp.local.", "name": "shelly*" }], "codeowners": ["@balloob", "@bieniu"] } diff --git a/homeassistant/components/shelly/switch.py b/homeassistant/components/shelly/switch.py index 0dcefb51cf9..7d140528858 100644 --- a/homeassistant/components/shelly/switch.py +++ b/homeassistant/components/shelly/switch.py @@ -5,13 +5,13 @@ from homeassistant.components.switch import SwitchEntity from homeassistant.core import callback from . import ShellyDeviceWrapper -from .const import DOMAIN +from .const import DATA_CONFIG_ENTRY, DOMAIN from .entity import ShellyBlockEntity async def async_setup_entry(hass, config_entry, async_add_entities): """Set up switches for device.""" - wrapper = hass.data[DOMAIN][config_entry.entry_id] + wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] # In roller mode the relay blocks exist but do not contain required info if ( diff --git a/requirements_all.txt b/requirements_all.txt index 56844727cd3..3d4362638b3 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -221,7 +221,7 @@ aiopvpc==2.0.2 aiopylgtv==0.3.3 # homeassistant.components.shelly -aioshelly==0.3.4 +aioshelly==0.4.0 # homeassistant.components.switcher_kis aioswitcher==1.2.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index c4781bf38b2..ed49775d89e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -137,7 +137,7 @@ aiopvpc==2.0.2 aiopylgtv==0.3.3 # homeassistant.components.shelly -aioshelly==0.3.4 +aioshelly==0.4.0 # homeassistant.components.switcher_kis aioswitcher==1.2.1