Bump aioshelly library to 0.4.0 (#41905)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/42163/head
Maciej Bieniek 2020-10-21 13:37:17 +02:00 committed by GitHub
parent abeff01626
commit f855ff8751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 39 deletions

View File

@ -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

View File

@ -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"]}

View File

@ -1,3 +1,5 @@
"""Constants for the Shelly integration."""
COAP_CONTEXT = "coap_context"
DATA_CONFIG_ENTRY = "config_entry"
DOMAIN = "shelly"

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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"]
}

View File

@ -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 (

View File

@ -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

View File

@ -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