|
| 1 | +"""Platform for the Daikin AC.""" |
| 2 | +import asyncio |
| 3 | +import datetime |
| 4 | +import logging |
| 5 | +import voluptuous as vol |
| 6 | + |
| 7 | +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry |
| 8 | +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, SERVICE_RELOAD |
| 9 | +from homeassistant.helpers.typing import HomeAssistantType |
| 10 | + |
| 11 | +from .const import DOMAIN, DAIKIN_API, DAIKIN_DEVICES, CONF_TOKENSET |
| 12 | + |
| 13 | +from .daikin_api import DaikinApi |
| 14 | + |
| 15 | +_LOGGER = logging.getLogger(__name__) |
| 16 | + |
| 17 | +ENTRY_IS_SETUP = "daikin_entry_is_setup" |
| 18 | +_LOGGER.debug("DAMIANO daikin_entry_is_setup") |
| 19 | + |
| 20 | +PARALLEL_UPDATES = 0 |
| 21 | + |
| 22 | +SERVICE_FORCE_UPDATE = "force_update" |
| 23 | +SERVICE_PULL_DEVICES = "pull_devices" |
| 24 | + |
| 25 | +SIGNAL_DELETE_ENTITY = "daikin_delete" |
| 26 | +SIGNAL_UPDATE_ENTITY = "daikin_update" |
| 27 | + |
| 28 | +TOKENSET_FILE = "altherma_tokenset.json" |
| 29 | + |
| 30 | +MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(seconds=2) |
| 31 | + |
| 32 | +COMPONENT_TYPES = ["climate", "sensor", "switch"] |
| 33 | + |
| 34 | + |
| 35 | +CONFIG_SCHEMA = vol.Schema( |
| 36 | + vol.All( |
| 37 | + { |
| 38 | + DOMAIN: vol.Schema( |
| 39 | + {vol.Required(CONF_EMAIL): str, vol.Required(CONF_PASSWORD): str} |
| 40 | + ) |
| 41 | + } |
| 42 | + ), |
| 43 | + extra=vol.ALLOW_EXTRA, |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +async def async_setup(hass, config): |
| 48 | + """Setup the Daikin Residential component.""" |
| 49 | + |
| 50 | + async def _handle_reload(service): |
| 51 | + """Handle reload service call.""" |
| 52 | + _LOGGER.info("Reloading integration: retrieving new TokenSet.") |
| 53 | + try: |
| 54 | + daikin_api = hass.data[DOMAIN][DAIKIN_API] |
| 55 | + data = daikin_api._config_entry.data.copy() |
| 56 | + await daikin_api.retrieveAccessToken(data[CONF_EMAIL], data[CONF_PASSWORD]) |
| 57 | + data[CONF_TOKENSET] = daikin_api.tokenSet |
| 58 | + hass.config_entries.async_update_entry( |
| 59 | + entry=daikin_api._config_entry, data=data |
| 60 | + ) |
| 61 | + except Exception as e: |
| 62 | + _LOGGER.error("Failed to reload integration: %s", e) |
| 63 | + |
| 64 | + hass.helpers.service.async_register_admin_service( |
| 65 | + DOMAIN, SERVICE_RELOAD, _handle_reload |
| 66 | + ) |
| 67 | + |
| 68 | + if DOMAIN not in config: |
| 69 | + return True |
| 70 | + |
| 71 | + conf = config.get(DOMAIN) |
| 72 | + if conf is not None: |
| 73 | + hass.async_create_task( |
| 74 | + hass.config_entries.flow.async_init( |
| 75 | + DOMAIN, context={"source": SOURCE_IMPORT}, data=conf |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + return True |
| 80 | + |
| 81 | + |
| 82 | +async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): |
| 83 | + """Establish connection with Daikin.""" |
| 84 | + |
| 85 | + daikin_api = DaikinApi(hass, entry) |
| 86 | + await daikin_api.getCloudDeviceDetails() |
| 87 | + |
| 88 | + devices = await daikin_api.getCloudDevices() |
| 89 | + hass.data[DOMAIN] = {DAIKIN_API: daikin_api, DAIKIN_DEVICES: devices} |
| 90 | + |
| 91 | + for component in COMPONENT_TYPES: |
| 92 | + #print("DAMIANO component %S", component) |
| 93 | + hass.async_create_task( |
| 94 | + hass.config_entries.async_forward_entry_setup(entry, component) |
| 95 | + ) |
| 96 | + return True |
| 97 | + |
| 98 | + |
| 99 | +async def async_unload_entry(hass, config_entry): |
| 100 | + """Unload a config entry.""" |
| 101 | + _LOGGER.debug("Unloading integration...") |
| 102 | + await asyncio.wait( |
| 103 | + [ |
| 104 | + hass.config_entries.async_forward_entry_unload(config_entry, component) |
| 105 | + for component in COMPONENT_TYPES |
| 106 | + ] |
| 107 | + ) |
| 108 | + hass.data[DOMAIN].clear() |
| 109 | + if not hass.data[DOMAIN]: |
| 110 | + hass.data.pop(DOMAIN) |
| 111 | + return True |
| 112 | + |
| 113 | + |
| 114 | +async def daikin_api_setup(hass, host, key, uuid, password): |
| 115 | + """Create a Daikin instance only once.""" |
| 116 | + return |
0 commit comments