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
|
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
|
2022-12-19 11:16:30 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_UUID,
|
|
|
|
Platform,
|
|
|
|
)
|
2023-07-05 12:12:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-04-10 07:44:00 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2023-07-05 12:12:18 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
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
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
|
2018-01-04 10:05:27 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2022-12-19 11:16:30 +00:00
|
|
|
from .const import 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
|
|
|
|
2023-07-05 12:12:18 +00:00
|
|
|
await async_migrate_unique_id(hass, entry, daikin_api)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2023-07-05 12:12:18 +00:00
|
|
|
async def daikin_api_setup(hass: HomeAssistant, 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:
|
2023-08-15 13:30:20 +00:00
|
|
|
async with asyncio.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("_", "."),
|
|
|
|
)
|
2023-07-05 12:12:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_migrate_unique_id(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, api: DaikinApi
|
|
|
|
) -> None:
|
|
|
|
"""Migrate old entry."""
|
|
|
|
dev_reg = dr.async_get(hass)
|
|
|
|
old_unique_id = config_entry.unique_id
|
|
|
|
new_unique_id = api.device.mac
|
2023-07-15 12:17:02 +00:00
|
|
|
new_name = api.device.values.get("name")
|
2023-07-05 12:12:18 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] | None:
|
|
|
|
"""Update unique ID of entity entry."""
|
|
|
|
return update_unique_id(entity_entry, new_unique_id)
|
|
|
|
|
|
|
|
if new_unique_id == old_unique_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Migrate devices
|
|
|
|
for device_entry in dr.async_entries_for_config_entry(
|
|
|
|
dev_reg, config_entry.entry_id
|
|
|
|
):
|
|
|
|
for connection in device_entry.connections:
|
|
|
|
if connection[1] == old_unique_id:
|
|
|
|
new_connections = {
|
|
|
|
(CONNECTION_NETWORK_MAC, dr.format_mac(new_unique_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Migrating device %s connections to %s",
|
|
|
|
device_entry.name,
|
|
|
|
new_connections,
|
|
|
|
)
|
|
|
|
dev_reg.async_update_device(
|
|
|
|
device_entry.id,
|
|
|
|
merge_connections=new_connections,
|
|
|
|
)
|
|
|
|
|
|
|
|
if device_entry.name is None:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Migrating device name to %s",
|
|
|
|
new_name,
|
|
|
|
)
|
|
|
|
dev_reg.async_update_device(
|
|
|
|
device_entry.id,
|
|
|
|
name=new_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Migrate entities
|
|
|
|
await er.async_migrate_entries(hass, config_entry.entry_id, _update_unique_id)
|
|
|
|
|
|
|
|
new_data = {**config_entry.data, KEY_MAC: dr.format_mac(new_unique_id)}
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
config_entry, unique_id=new_unique_id, data=new_data
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_unique_id(
|
|
|
|
entity_entry: er.RegistryEntry, unique_id: str
|
|
|
|
) -> dict[str, str] | None:
|
|
|
|
"""Update unique ID of entity entry."""
|
|
|
|
if entity_entry.unique_id.startswith(unique_id):
|
|
|
|
# Already correct, nothing to do
|
|
|
|
return None
|
|
|
|
|
|
|
|
unique_id_parts = entity_entry.unique_id.split("-")
|
|
|
|
unique_id_parts[0] = unique_id
|
|
|
|
entity_new_unique_id = "-".join(unique_id_parts)
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Migrating entity %s from %s to new id %s",
|
|
|
|
entity_entry.entity_id,
|
|
|
|
entity_entry.unique_id,
|
|
|
|
entity_new_unique_id,
|
|
|
|
)
|
|
|
|
return {"new_unique_id": entity_new_unique_id}
|