From 38f69bce58dbae9f206a842ec65e810cd231a9fd Mon Sep 17 00:00:00 2001 From: msp1974 Date: Thu, 30 Apr 2020 22:54:21 +0100 Subject: [PATCH] Additional IO improvements --- custom_components/wiser/__init__.py | 24 ++++++++++++++++-------- custom_components/wiser/climate.py | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/custom_components/wiser/__init__.py b/custom_components/wiser/__init__.py index daab868..1d75334 100755 --- a/custom_components/wiser/__init__.py +++ b/custom_components/wiser/__init__.py @@ -304,22 +304,26 @@ async def async_update_device_registry(self): async def set_away_mode(self, away, away_temperature): mode = "AWAY" if away else "HOME" if self.wiserhub is None: - self.wiserhub = wiserHub(self.ip, self.secret) + self.wiserhub = await self.async_connect() _LOGGER.debug( "Setting away mode to {} with temp {}.".format(mode, away_temperature) ) try: - self.wiserhub.setHomeAwayMode(mode, away_temperature) + await self._hass.async_add_executor_job( + partial(self.wiserhub.setHomeAwayMode, mode, away_temperature) + ) await self.async_update(no_throttle=True) except BaseException as e: _LOGGER.debug("Error setting away mode! {}".format(str(e))) async def set_system_switch(self, switch, mode): if self.wiserhub is None: - self.wiserhub = wiserHub(self.ip, self.secret) + self.wiserhub = await self.async_connect() _LOGGER.debug("Setting {} system switch to {}.".format(switch, mode)) try: - self.wiserhub.setSystemSwitch(switch, mode) + await self._hass.async_add_executor_job( + partial(self.wiserhub.setSystemSwitch, switch, mode) + ) await self.async_update(no_throttle=True) except BaseException as e: _LOGGER.debug("Error setting {} system switch! {}".format(switch, str(e))) @@ -332,11 +336,13 @@ async def set_smart_plug_state(self, plug_id, state): :return: """ if self.wiserhub is None: - self.wiserhub = wiserHub(self.ip, self.secret) + self.wiserhub = await self.async_connect() _LOGGER.info("Setting SmartPlug {} to {} ".format(plug_id, state)) try: - self.wiserhub.setSmartPlugState(plug_id, state) + await self._hass.async_add_executor_job( + partial(self.wiserhub.setSmartPlugState, plug_id, state) + ) # Add small delay to allow hub to update status before refreshing await asyncio.sleep(0.5) await self.async_update(no_throttle=True) @@ -353,14 +359,16 @@ async def set_hotwater_mode(self, hotwater_mode): """ if self.wiserhub is None: - self.wiserhub = wiserHub(self.ip, self.secret) + self.wiserhub = await self.async_connect() _LOGGER.info("Setting Hotwater to {} ".format(hotwater_mode)) # Add small delay to allow hub to update status before refreshing await asyncio.sleep(0.5) await self.async_update(no_throttle=True) try: - self.wiserhub.setHotwaterMode(hotwater_mode) + await self._hass.async_add_executor_job( + partial(self.wiserhub.setHotwaterMode, hotwater_mode) + ) except BaseException as e: _LOGGER.debug( diff --git a/custom_components/wiser/climate.py b/custom_components/wiser/climate.py index f720db2..542d089 100755 --- a/custom_components/wiser/climate.py +++ b/custom_components/wiser/climate.py @@ -10,6 +10,7 @@ import voluptuous as vol +from functools import partial from homeassistant.components.climate import ClimateDevice from homeassistant.core import callback @@ -477,7 +478,11 @@ async def async_set_temperature(self, **kwargs): _LOGGER.debug( "Setting temperature for {} to {}".format(self.name, target_temperature) ) - self.data.wiserhub.setRoomTemperature(self.room_id, target_temperature) + await self.hass.async_add_executor_job( + partial( + self.data.wiserhub.setRoomTemperature, self.room_id, target_temperature + ) + ) self._force_update = True await self.async_update_ha_state(True) @@ -490,7 +495,11 @@ async def set_room_mode(self, room_id, mode, boost_temp=None, boost_time=None): _LOGGER.debug( "Setting Room Mode to {} for roomId {}".format(mode, self.room_id) ) - self.data.wiserhub.setRoomMode(room_id, mode, boost_temp, boost_time) + await self.hass.async_add_executor_job( + partial( + self.data.wiserhub.setRoomMode, room_id, mode, boost_temp, boost_time + ) + ) self._force_update = True await self.async_update_ha_state(True) return True @@ -498,7 +507,9 @@ async def set_room_mode(self, room_id, mode, boost_temp=None, boost_time=None): async def set_room_schedule(self, room_id, scheduleData): if scheduleData != None: scheduleData = convert_to_wiser_schedule(scheduleData) - self.data.wiserhub.setRoomSchedule(room_id, scheduleData) + await self.hass.async_add_executor_job( + partial(self.data.wiserhub.setRoomSchedule, room_id, scheduleData) + ) _LOGGER.debug("Set room schedule for {}".format(self.name)) self._force_update = True await self.async_update_ha_state(True) @@ -507,7 +518,9 @@ async def set_room_schedule(self, room_id, scheduleData): return False async def copy_room_schedule(self, room_id, to_room_id): - self.data.wiserhub.copyRoomSchedule(room_id, to_room_id) + await self.hass.async_add_executor_job( + partial(self.data.wiserhub.copyRoomSchedule, room_id, to_room_id) + ) _LOGGER.debug( "Copied room schedule from {} to {}".format( self.name, self.data.wiserhub.getRoom(to_room_id).get("Name")