forked from mac-zhou/midea-ac-py
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an input to the discovery flow to select a country for cloud cred…
…ential selection (#258) * Add an input to the discovery flow to select a country for determining the cloud credentials * Add South Korea as a region option
- Loading branch information
Showing
3 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
"""Config flow for Midea Smart AC.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any, Optional | ||
from typing import Any, Optional, cast | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
import voluptuous as vol | ||
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow | ||
from homeassistant.const import CONF_HOST, CONF_ID, CONF_PORT, CONF_TOKEN | ||
from homeassistant.const import (CONF_COUNTRY_CODE, CONF_HOST, CONF_ID, | ||
CONF_PORT, CONF_TOKEN) | ||
from homeassistant.core import callback | ||
from homeassistant.data_entry_flow import FlowResult | ||
from homeassistant.helpers.selector import (CountrySelector, | ||
CountrySelectorConfig) | ||
from msmart.const import DeviceType | ||
from msmart.device import AirConditioner as AC | ||
from msmart.discover import Discover | ||
from msmart.lan import AuthenticationError | ||
|
||
from .const import (CONF_ADDITIONAL_OPERATION_MODES, CONF_BEEP, | ||
CONF_CLOUD_COUNTRY_CODES, CONF_DEFAULT_CLOUD_COUNTRY, | ||
CONF_FAN_SPEED_STEP, CONF_KEY, | ||
CONF_MAX_CONNECTION_LIFETIME, CONF_SHOW_ALL_PRESETS, | ||
CONF_TEMP_STEP, CONF_USE_ALTERNATE_ENERGY_FORMAT, | ||
|
@@ -31,6 +35,11 @@ | |
CONF_USE_ALTERNATE_ENERGY_FORMAT: False, | ||
} | ||
|
||
_CLOUD_CREDENTIALS = { | ||
"DE": ("[email protected]", "das_ist_passwort1"), | ||
"KR": ("[email protected]", "password_for_sea1") | ||
} | ||
|
||
|
||
class MideaConfigFlow(ConfigFlow, domain=DOMAIN): | ||
"""Config flow for Midea Smart AC.""" | ||
|
@@ -52,12 +61,22 @@ async def async_step_discover( | |
errors = {} | ||
|
||
if user_input is not None: | ||
country_code = cast(str, user_input.get(CONF_COUNTRY_CODE)) | ||
|
||
# If host was not provided, discover all devices | ||
if not (host := user_input.get(CONF_HOST)): | ||
return await self.async_step_pick_device() | ||
return await self.async_step_pick_device(country_code=country_code) | ||
|
||
# Get credentials for region | ||
account, password = _CLOUD_CREDENTIALS.get( | ||
country_code, (None, None)) | ||
|
||
# Attempt to find specified device | ||
device = await Discover.discover_single(host, auto_connect=False, timeout=2) | ||
device = await Discover.discover_single(host, | ||
auto_connect=False, | ||
timeout=2, | ||
account=account, | ||
password=password) | ||
|
||
if device is None: | ||
errors["base"] = "device_not_found" | ||
|
@@ -76,14 +95,22 @@ async def async_step_discover( | |
return self.async_abort(reason="cannot_connect") | ||
|
||
data_schema = vol.Schema({ | ||
vol.Optional(CONF_HOST, default=""): str | ||
vol.Optional(CONF_HOST, default=""): str, | ||
vol.Optional( | ||
CONF_COUNTRY_CODE, default=CONF_DEFAULT_CLOUD_COUNTRY | ||
): CountrySelector( | ||
CountrySelectorConfig( | ||
countries=CONF_CLOUD_COUNTRY_CODES) | ||
), | ||
}) | ||
|
||
return self.async_show_form(step_id="discover", | ||
data_schema=data_schema, errors=errors) | ||
|
||
async def async_step_pick_device( | ||
self, user_input: dict[str, Any] | None = None | ||
self, user_input: dict[str, Any] | None = None, | ||
*, | ||
country_code: str = CONF_DEFAULT_CLOUD_COUNTRY | ||
) -> FlowResult: | ||
"""Handle the pick device step of config flow.""" | ||
|
||
|
@@ -110,8 +137,15 @@ async def async_step_pick_device( | |
entry.unique_id for entry in self._async_current_entries() | ||
} | ||
|
||
# Get credentials for region | ||
account, password = _CLOUD_CREDENTIALS.get(country_code, (None, None)) | ||
|
||
# Discover all devices | ||
self._discovered_devices = await Discover.discover(auto_connect=False, timeout=2) | ||
self._discovered_devices = await Discover.discover( | ||
auto_connect=False, | ||
timeout=2, | ||
account=account, | ||
password=password) | ||
|
||
# Create dict of device ID to friendly name | ||
devices_name = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters