8000 Add asyncio locks to screenlogic api access points by dieselrabbit · Pull Request #48457 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add asyncio locks to screenlogic api access points #48457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions homeassistant/components/screenlogic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
_LOGGER.error("Error while connecting to the gateway %s: %s", connect_info, ex)
raise ConfigEntryNotReady from ex

# The api library uses a shared socket connection and does not handle concurrent
# requests very well.
api_lock = asyncio.Lock()

coordinator = ScreenlogicDataUpdateCoordinator(
hass, config_entry=entry, gateway=gateway
hass, config_entry=entry, gateway=gateway, api_lock=api_lock
)

device_data = defaultdict(list)
Expand Down Expand Up @@ -127,10 +131,11 @@ async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
class ScreenlogicDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage the data update for the Screenlogic component."""

def __init__(self, hass, *, config_entry, gateway):
def __init__(self, hass, *, config_entry, gateway, api_lock):
"""Initialize the Screenlogic Data Update Coordinator."""
self.config_entry = config_entry
self.gateway = gateway
self.api_lock = api_lock
self.screenlogic_data = {}
interval = timedelta(
seconds=config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
Expand All @@ -145,7 +150,8 @@ def __init__(self, hass, *, config_entry, gateway):
async def _async_update_data(self):
"""Fetch data from the Screenlogic gateway."""
try:
await self.hass.async_add_executor_job(self.gateway.update)
async with self.api_lock:
await self.hass.async_add_executor_job(self.gateway.update)
except ScreenLogicError as error:
raise UpdateFailed(error) from error
return self.gateway.get_data()
Expand Down
29 changes: 20 additions & 9 deletions homeassistant/components/screenlogic/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ async def async_set_temperature(self, **kwargs) -> None:
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
raise ValueError(f"Expected attribute {ATTR_TEMPERATURE}")

if await self.hass.async_add_executor_job(
self.gateway.set_heat_temp, int(self._data_key), int(temperature)
):
async with self.coordinator.api_lock:
success = await self.hass.async_add_executor_job(
self.gateway.set_heat_temp, int(self._data_key), int(temperature)
)

if success:
await self.coordinator.async_request_refresh()
else:
raise HomeAssistantError(
Expand All @@ -153,9 +156,13 @@ async def async_set_hvac_mode(self, hvac_mode) -> None:
mode = HEAT_MODE.OFF
else:
mode = HEAT_MODE.NUM_FOR_NAME[self.preset_mode]
if await self.hass.async_add_executor_job(
self.gateway.set_heat_mode, int(self._data_key), int(mode)
):

async with self.coordinator.api_lock:
success = await self.hass.async_add_executor_job(
self.gateway.set_heat_mode, int(self._data_key), int(mode)
)

if success:
await self.coordinator.async_request_refresh()
else:
raise HomeAssistantError(
Expand All @@ -168,9 +175,13 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
self._last_preset = mode = HEAT_MODE.NUM_FOR_NAME[preset_mode]
if self.hvac_mode == HVAC_MODE_OFF:
return
if await self.hass.async_add_executor_job(
self.gateway.set_heat_mode, int(self._data_key), int(mode)
):

async with self.coordinator.api_lock:
success = await self.hass.async_add_executor_job(
self.gateway.set_heat_mode, int(self._data_key), int(mode)
)

if success:
await self.coordinator.async_request_refresh()
else:
raise HomeAssistantError(
Expand Down
9 changes: 6 additions & 3 deletions homeassistant/components/screenlogic/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ async def async_turn_off(self, **kwargs) -> None:
return await self._async_set_circuit(ON_OFF.OFF)

async def _async_set_circuit(self, circuit_value) -> None:
if await self.hass.async_add_executor_job(
self.gateway.set_circuit, self._data_key, circuit_value
):
async with self.coordinator.api_lock:
success = await self.hass.async_add_executor_job(
self.gateway.set_circuit, self._data_key, circuit_value
)

if success:
_LOGGER.debug("Turn %s %s", self._data_key, circuit_value)
await self.coordinator.async_request_refresh()
else:
Expand Down
0