2019-02-13 20:21:14 +00:00
|
|
|
"""Platform for the Daikin AC."""
|
2018-12-16 15:19:18 +00:00
|
|
|
import asyncio
|
2018-01-04 10:05:27 +00:00
|
|
|
from datetime import timedelta
|
2018-12-16 15:19:18 +00:00
|
|
|
import logging
|
2018-01-04 10:05:27 +00:00
|
|
|
|
2019-04-08 17:08:03 +00:00
|
|
|
from aiohttp import ClientConnectionError
|
|
|
|
from async_timeout import timeout
|
2020-05-06 11:59:07 +00:00
|
|
|
from pydaikin.daikin_base import Appliance
|
2018-01-04 10:05:27 +00:00
|
|
|
|
2021-05-04 21:23:59 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-03 16:51:30 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD, Platform
|
2021-04-17 10:48:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-04-10 07:44:00 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2022-01-11 16:33:50 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2018-01-04 10:05:27 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-06-02 11:24:15 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
2021-10-22 15:00:00 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2018-01-04 10:05:27 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2021-03-20 23:34:46 +00:00
|
|
|
from .const import CONF_UUID, DOMAIN, KEY_MAC, TIMEOUT
|
2018-12-16 15:19:18 +00:00
|
|
|
|
2018-01-04 10:05:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-03-27 20:37:21 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
2018-01-04 10:05:27 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
|
|
|
|
2021-12-03 16:51:30 +00:00
|
|
|
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH]
|
2018-01-04 10:05:27 +00:00
|
|
|
|
2021-12-21 11:46:10 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2018-01-04 10:05:27 +00:00
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-12-16 15:19:18 +00:00
|
|
|
"""Establish connection with Daikin."""
|
|
|
|
conf = entry.data
|
2020-05-20 11:25:42 +00:00
|
|
|
# For backwards compat, set unique ID
|
2021-05-04 21:23:59 +00:00
|
|
|
if entry.unique_id is None or ".local" in entry.unique_id:
|
2020-06-12 15:25:18 +00:00
|
|
|
hass.config_entries.async_update_entry(entry, unique_id=conf[KEY_MAC])
|
2021-05-04 21:23:59 +00:00
|
|
|
|
2020-05-06 11:59:07 +00:00
|
|
|
daikin_api = await daikin_api_setup(
|
|
|
|
hass,
|
|
|
|
conf[CONF_HOST],
|
2020-10-08 19:25:54 +00:00
|
|
|
conf.get(CONF_API_KEY),
|
2020-05-06 11:59:07 +00:00
|
|
|
conf.get(CONF_UUID),
|
|
|
|
conf.get(CONF_PASSWORD),
|
|
|
|
)
|
2018-12-16 15:19:18 +00:00
|
|
|
if not daikin_api:
|
|
|
|
return False
|
2021-05-04 21:23:59 +00:00
|
|
|
|
2018-12-16 15:19:18 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: daikin_api})
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2018-12-16 15:19:18 +00:00
|
|
|
return True
|
2018-01-04 10:05:27 +00:00
|
|
|
|
|
|
|
|
2022-01-02 15:12:46 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-12-16 15:19:18 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
return unload_ok
|
2018-01-04 10:05:27 +00:00
|
|
|
|
|
|
|
|
2020-05-06 11:59:07 +00:00
|
|
|
async def daikin_api_setup(hass, host, key, uuid, password):
|
2018-01-04 10:05:27 +00:00
|
|
|
"""Create a Daikin instance only once."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2022-01-11 16:33:50 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2018-12-16 15:19:18 +00:00
|
|
|
try:
|
2021-11-04 15:07:50 +00:00
|
|
|
async with timeout(TIMEOUT):
|
2020-05-06 11:59:07 +00:00
|
|
|
device = await Appliance.factory(
|
|
|
|
host, session, key=key, uuid=uuid, password=password
|
|
|
|
)
|
2020-08-28 11:50:32 +00:00
|
|
|
except asyncio.TimeoutError as err:
|
2019-04-10 07:44:00 +00:00
|
|
|
_LOGGER.debug("Connection to %s timed out", host)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
except ClientConnectionError as err:
|
2019-04-10 07:44:00 +00:00
|
|
|
_LOGGER.debug("ClientConnectionError to %s", host)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
2018-12-16 15:19:18 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-04-10 07:44:00 +00:00
|
|
|
_LOGGER.error("Unexpected error creating device %s", host)
|
2018-12-16 15:19:18 +00:00
|
|
|
return None
|
|
|
|
|
2019-03-14 17:33:43 +00:00
|
|
|
api = DaikinApi(device)
|
2018-01-04 10:05:27 +00:00
|
|
|
|
|
|
|
return api
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class DaikinApi:
|
2018-01-04 10:05:27 +00:00
|
|
|
"""Keep the Daikin instance in one place and centralize the update."""
|
|
|
|
|
2021-05-20 15:51:39 +00:00
|
|
|
def __init__(self, device: Appliance) -> None:
|
2018-01-04 10:05:27 +00:00
|
|
|
"""Initialize the Daikin Handle."""
|
|
|
|
self.device = device
|
2020-05-06 11:59:07 +00:00
|
|
|
self.name = device.values.get("name", "Daikin AC")
|
|
|
|
self.ip_address = device.device_ip
|
2019-03-14 17:33:43 +00:00
|
|
|
self._available = True
|
2018-01-04 10:05:27 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2019-03-14 17:33:43 +00:00
|
|
|
async def async_update(self, **kwargs):
|
2018-01-04 10:05:27 +00:00
|
|
|
"""Pull the latest data from Daikin."""
|
|
|
|
try:
|
2019-03-14 17:33:43 +00:00
|
|
|
await self.device.update_status()
|
|
|
|
self._available = True
|
2019-04-08 17:08:03 +00:00
|
|
|
except ClientConnectionError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Connection failed for %s", self.ip_address)
|
2019-03-14 17:33:43 +00:00
|
|
|
self._available = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._available
|
2018-11-27 14:36:55 +00:00
|
|
|
|
2018-12-19 07:18:40 +00:00
|
|
|
@property
|
2021-10-22 15:00:00 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2018-12-19 07:18:40 +00:00
|
|
|
"""Return a device description for device registry."""
|
|
|
|
info = self.device.values
|
2021-10-22 15:00:00 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
connections={(CONNECTION_NETWORK_MAC, self.device.mac)},
|
|
|
|
manufacturer="Daikin",
|
|
|
|
model=info.get("model"),
|
|
|
|
name=info.get("name"),
|
|
|
|
sw_version=info.get("ver", "").replace("_", "."),
|
|
|
|
)
|