Async support for Daikin (#21638)

* asyncio support for Daikin

* version bump pydaikin

* pass session to pydaikin.Appliance

* all entities should have update
This commit is contained in:
Fredrik Erlandsson 2019-03-14 18:33:43 +01:00 committed by Martin Hjelmare
parent 018a5d5c1f
commit 4e84e8a15e
4 changed files with 32 additions and 23 deletions

View File

@ -17,7 +17,7 @@ from homeassistant.util import Throttle
from . import config_flow # noqa pylint_disable=unused-import
from .const import KEY_HOST
REQUIREMENTS = ['pydaikin==0.9']
REQUIREMENTS = ['pydaikin==1.1.0']
_LOGGER = logging.getLogger(__name__)
@ -87,9 +87,11 @@ async def async_unload_entry(hass, config_entry):
async def daikin_api_setup(hass, host):
"""Create a Daikin instance only once."""
from pydaikin.appliance import Appliance
session = hass.helpers.aiohttp_client.async_get_clientsession()
try:
with async_timeout.timeout(10):
device = await hass.async_add_executor_job(Appliance, host)
device = Appliance(host, session)
await device.init()
except asyncio.TimeoutError:
_LOGGER.error("Connection to Daikin could not be established")
return None
@ -97,8 +99,7 @@ async def daikin_api_setup(hass, host):
_LOGGER.error("Unexpected error creating device")
return None
name = device.values['name']
api = DaikinApi(device, name)
api = DaikinApi(device)
return api
@ -106,21 +107,29 @@ async def daikin_api_setup(hass, host):
class DaikinApi:
"""Keep the Daikin instance in one place and centralize the update."""
def __init__(self, device, name):
def __init__(self, device):
"""Initialize the Daikin Handle."""
self.device = device
self.name = name
self.name = device.values['name']
self.ip_address = device.ip
self._available = True
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self, **kwargs):
async def async_update(self, **kwargs):
"""Pull the latest data from Daikin."""
try:
self.device.update_status()
await self.device.update_status()
self._available = True
except timeout:
_LOGGER.warning(
"Connection failed for %s", self.ip_address
)
self._available = False
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
@property
def mac(self):

View File

@ -146,7 +146,7 @@ class DaikinClimate(ClimateDevice):
return value
def set(self, settings):
async def _set(self, settings):
"""Set device settings using API."""
values = {}
@ -173,7 +173,7 @@ class DaikinClimate(ClimateDevice):
_LOGGER.error("Invalid temperature %s", value)
if values:
self._api.device.set(values)
await self._api.device.set(values)
@property
def supported_features(self):
@ -210,9 +210,9 @@ class DaikinClimate(ClimateDevice):
"""Return the supported step of target temperature."""
return 1
def set_temperature(self, **kwargs):
async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
self.set(kwargs)
await self._set(kwargs)
@property
def current_operation(self):
@ -224,18 +224,18 @@ class DaikinClimate(ClimateDevice):
"""Return the list of available operation modes."""
return self._list.get(ATTR_OPERATION_MODE)
def set_operation_mode(self, operation_mode):
async def async_set_operation_mode(self, operation_mode):
"""Set HVAC mode."""
self.set({ATTR_OPERATION_MODE: operation_mode})
await self._set({ATTR_OPERATION_MODE: operation_mode})
@property
def current_fan_mode(self):
"""Return the fan setting."""
return self.get(ATTR_FAN_MODE)
def set_fan_mode(self, fan_mode):
async def async_set_fan_mode(self, fan_mode):
"""Set fan mode."""
self.set({ATTR_FAN_MODE: fan_mode})
await self._set({ATTR_FAN_MODE: fan_mode})
@property
def fan_list(self):
@ -247,18 +247,18 @@ class DaikinClimate(ClimateDevice):
"""Return the fan setting."""
return self.get(ATTR_SWING_MODE)
def set_swing_mode(self, swing_mode):
async def async_set_swing_mode(self, swing_mode):
"""Set new target temperature."""
self.set({ATTR_SWING_MODE: swing_mode})
await self._set({ATTR_SWING_MODE: swing_mode})
@property
def swing_list(self):
"""List of available swing modes."""
return self._list.get(ATTR_SWING_MODE)
def update(self):
async def async_update(self):
"""Retrieve latest state."""
self._api.update()
await self._api.async_update()
@property
def device_info(self):

View File

@ -96,9 +96,9 @@ class DaikinClimateSensor(Entity):
"""Return the unit of measurement."""
return self._unit_of_measurement
def update(self):
async def async_update(self):
"""Retrieve latest state."""
self._api.update()
await self._api.async_update()
@property
def device_info(self):

View File

@ -998,7 +998,7 @@ pycsspeechtts==1.0.2
# pycups==1.9.73
# homeassistant.components.daikin
pydaikin==0.9
pydaikin==1.1.0
# homeassistant.components.danfoss_air
pydanfossair==0.0.7