From 4e84e8a15e18d24c84f4f4465c082f217c3d1afa Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Thu, 14 Mar 2019 18:33:43 +0100 Subject: [PATCH] Async support for Daikin (#21638) * asyncio support for Daikin * version bump pydaikin * pass session to pydaikin.Appliance * all entities should have update --- homeassistant/components/daikin/__init__.py | 25 ++++++++++++++------- homeassistant/components/daikin/climate.py | 24 ++++++++++---------- homeassistant/components/daikin/sensor.py | 4 ++-- requirements_all.txt | 2 +- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/daikin/__init__.py b/homeassistant/components/daikin/__init__.py index ce4a58162c7..04cf8a584bf 100644 --- a/homeassistant/components/daikin/__init__.py +++ b/homeassistant/components/daikin/__init__.py @@ -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): diff --git a/homeassistant/components/daikin/climate.py b/homeassistant/components/daikin/climate.py index 775e4a216e5..869c38869cb 100644 --- a/homeassistant/components/daikin/climate.py +++ b/homeassistant/components/daikin/climate.py @@ -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): diff --git a/homeassistant/components/daikin/sensor.py b/homeassistant/components/daikin/sensor.py index 6065a182274..3669dfac280 100644 --- a/homeassistant/components/daikin/sensor.py +++ b/homeassistant/components/daikin/sensor.py @@ -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): diff --git a/requirements_all.txt b/requirements_all.txt index 4b82eed9f81..a4967357d51 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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