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

View File

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

View File

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

View File

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