mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Bump aioshelly library to 0.4.0 (#41905)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
abeff01626
commit
f855ff8751
@ -3,7 +3,7 @@ import asyncio
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiocoap import error as aiocoap_error
|
import aiocoap
|
||||||
import aioshelly
|
import aioshelly
|
||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import aiohttp_client, device_registry, update_coordinator
|
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"]
|
PLATFORMS = ["binary_sensor", "cover", "light", "sensor", "switch"]
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -26,7 +26,15 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: dict):
|
async def async_setup(hass: HomeAssistant, config: dict):
|
||||||
"""Set up the Shelly component."""
|
"""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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -39,18 +47,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
entry.data.get(CONF_PASSWORD),
|
entry.data.get(CONF_PASSWORD),
|
||||||
temperature_unit,
|
temperature_unit,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
coap_context = hass.data[DOMAIN][COAP_CONTEXT]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(10):
|
async with async_timeout.timeout(10):
|
||||||
device = await aioshelly.Device.create(
|
device = await aioshelly.Device.create(
|
||||||
aiohttp_client.async_get_clientsession(hass),
|
aiohttp_client.async_get_clientsession(hass),
|
||||||
|
coap_context,
|
||||||
options,
|
options,
|
||||||
)
|
)
|
||||||
except (asyncio.TimeoutError, OSError) as err:
|
except (asyncio.TimeoutError, OSError) as err:
|
||||||
raise ConfigEntryNotReady from err
|
raise ConfigEntryNotReady from err
|
||||||
|
|
||||||
wrapper = hass.data[DOMAIN][entry.entry_id] = ShellyDeviceWrapper(
|
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
|
||||||
hass, entry, device
|
entry.entry_id
|
||||||
)
|
] = ShellyDeviceWrapper(hass, entry, device)
|
||||||
await wrapper.async_setup()
|
await wrapper.async_setup()
|
||||||
|
|
||||||
for component in PLATFORMS:
|
for component in PLATFORMS:
|
||||||
@ -75,18 +87,14 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator):
|
|||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.entry = entry
|
self.entry = entry
|
||||||
self.device = device
|
self.device = device
|
||||||
self._unsub_stop = None
|
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
"""Fetch data."""
|
"""Fetch data."""
|
||||||
# Race condition on shutdown. Stop all the fetches.
|
|
||||||
if self._unsub_stop is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(5):
|
async with async_timeout.timeout(5):
|
||||||
return await self.device.update()
|
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
|
raise update_coordinator.UpdateFailed("Error fetching data") from err
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -101,9 +109,6 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator):
|
|||||||
|
|
||||||
async def async_setup(self):
|
async def async_setup(self):
|
||||||
"""Set up the wrapper."""
|
"""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)
|
dev_reg = await device_registry.async_get_registry(self.hass)
|
||||||
model_type = self.device.settings["device"]["type"]
|
model_type = self.device.settings["device"]["type"]
|
||||||
dev_reg.async_get_or_create(
|
dev_reg.async_get_or_create(
|
||||||
@ -117,18 +122,6 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator):
|
|||||||
sw_version=self.device.settings["fw"],
|
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):
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
@ -141,6 +134,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
if unload_ok:
|
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
|
return unload_ok
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import aiocoap
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import aioshelly
|
import aioshelly
|
||||||
import async_timeout
|
import async_timeout
|
||||||
@ -33,13 +34,15 @@ async def validate_input(hass: core.HomeAssistant, host, data):
|
|||||||
options = aioshelly.ConnectionOptions(
|
options = aioshelly.ConnectionOptions(
|
||||||
host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)
|
host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)
|
||||||
)
|
)
|
||||||
|
coap_context = await aiocoap.Context.create_client_context()
|
||||||
async with async_timeout.timeout(5):
|
async with async_timeout.timeout(5):
|
||||||
device = await aioshelly.Device.create(
|
device = await aioshelly.Device.create(
|
||||||
aiohttp_client.async_get_clientsession(hass),
|
aiohttp_client.async_get_clientsession(hass),
|
||||||
|
coap_context,
|
||||||
options,
|
options,
|
||||||
)
|
)
|
||||||
|
|
||||||
await device.shutdown()
|
await coap_context.shutdown()
|
||||||
|
|
||||||
# Return info that you want to store in the config entry.
|
# Return info that you want to store in the config entry.
|
||||||
return {"title": device.settings["name"], "mac": device.settings["device"]["mac"]}
|
return {"title": device.settings["name"], "mac": device.settings["device"]["mac"]}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
"""Constants for the Shelly integration."""
|
"""Constants for the Shelly integration."""
|
||||||
|
|
||||||
|
COAP_CONTEXT = "coap_context"
|
||||||
|
DATA_CONFIG_ENTRY = "config_entry"
|
||||||
DOMAIN = "shelly"
|
DOMAIN = "shelly"
|
||||||
|
@ -12,13 +12,13 @@ from homeassistant.components.cover import (
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
from . import ShellyDeviceWrapper
|
from . import ShellyDeviceWrapper
|
||||||
from .const import DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
from .entity import ShellyBlockEntity
|
from .entity import ShellyBlockEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up cover for device."""
|
"""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"]
|
blocks = [block for block in wrapper.device.blocks if block.type == "roller"]
|
||||||
|
|
||||||
if not blocks:
|
if not blocks:
|
||||||
|
@ -10,7 +10,7 @@ from homeassistant.core import callback
|
|||||||
from homeassistant.helpers import device_registry, entity
|
from homeassistant.helpers import device_registry, entity
|
||||||
|
|
||||||
from . import ShellyDeviceWrapper
|
from . import ShellyDeviceWrapper
|
||||||
from .const import DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
|
|
||||||
|
|
||||||
def temperature_unit(block_info: dict) -> str:
|
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
|
hass, config_entry, async_add_entities, sensors, sensor_class
|
||||||
):
|
):
|
||||||
"""Set up entities for block attributes."""
|
"""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 = []
|
blocks = []
|
||||||
|
|
||||||
for block in wrapper.device.blocks:
|
for block in wrapper.device.blocks:
|
||||||
|
@ -17,13 +17,13 @@ from homeassistant.util.color import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from . import ShellyDeviceWrapper
|
from . import ShellyDeviceWrapper
|
||||||
from .const import DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
from .entity import ShellyBlockEntity
|
from .entity import ShellyBlockEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up lights for device."""
|
"""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"]
|
blocks = [block for block in wrapper.device.blocks if block.type == "light"]
|
||||||
|
|
||||||
if not blocks:
|
if not blocks:
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"name": "Shelly",
|
"name": "Shelly",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/shelly",
|
"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*" }],
|
"zeroconf": [{ "type": "_http._tcp.local.", "name": "shelly*" }],
|
||||||
"codeowners": ["@balloob", "@bieniu"]
|
"codeowners": ["@balloob", "@bieniu"]
|
||||||
}
|
}
|
||||||
|
@ -5,13 +5,13 @@ from homeassistant.components.switch import SwitchEntity
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
from . import ShellyDeviceWrapper
|
from . import ShellyDeviceWrapper
|
||||||
from .const import DOMAIN
|
from .const import DATA_CONFIG_ENTRY, DOMAIN
|
||||||
from .entity import ShellyBlockEntity
|
from .entity import ShellyBlockEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up switches for device."""
|
"""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
|
# In roller mode the relay blocks exist but do not contain required info
|
||||||
if (
|
if (
|
||||||
|
@ -221,7 +221,7 @@ aiopvpc==2.0.2
|
|||||||
aiopylgtv==0.3.3
|
aiopylgtv==0.3.3
|
||||||
|
|
||||||
# homeassistant.components.shelly
|
# homeassistant.components.shelly
|
||||||
aioshelly==0.3.4
|
aioshelly==0.4.0
|
||||||
|
|
||||||
# homeassistant.components.switcher_kis
|
# homeassistant.components.switcher_kis
|
||||||
aioswitcher==1.2.1
|
aioswitcher==1.2.1
|
||||||
|
@ -137,7 +137,7 @@ aiopvpc==2.0.2
|
|||||||
aiopylgtv==0.3.3
|
aiopylgtv==0.3.3
|
||||||
|
|
||||||
# homeassistant.components.shelly
|
# homeassistant.components.shelly
|
||||||
aioshelly==0.3.4
|
aioshelly==0.4.0
|
||||||
|
|
||||||
# homeassistant.components.switcher_kis
|
# homeassistant.components.switcher_kis
|
||||||
aioswitcher==1.2.1
|
aioswitcher==1.2.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user