-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.3.0 Enable multi-device selections during setup UI
- Loading branch information
Showing
9 changed files
with
152 additions
and
88 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,51 +1,35 @@ | ||
from homeassistant import config_entries, core | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from .const import DOMAIN | ||
from .pyhisenseapi import HiSenseLogin, HiSenseAC | ||
from .pyhisenseapi import HiSenseAC | ||
|
||
|
||
async def async_setup(hass: core.HomeAssistant, config: dict): | ||
# Initialize integration data structure | ||
hass.data[DOMAIN] = {} | ||
return True | ||
|
||
async def async_setup_entry(hass: core.HomeAssistant, entry: config_entries.ConfigEntry): | ||
# Setup the API client for a device | ||
hass.data.setdefault(DOMAIN, {}) | ||
hass.data[DOMAIN][entry.entry_id] = {} | ||
|
||
session = async_get_clientsession(hass) | ||
hisense_login = HiSenseLogin( | ||
session=session | ||
) | ||
# Setup devices based on the selected devices from the config flow | ||
for device_info in entry.data["devices"]: | ||
device_id = device_info["device_id"] | ||
wifi_id = device_info["wifi_id"] | ||
refresh_token = device_info["refresh_token"] | ||
hass.data[DOMAIN][entry.entry_id][device_id] = HiSenseAC( | ||
wifi_id=wifi_id, | ||
device_id=device_id, | ||
refresh_token=refresh_token, | ||
session=session | ||
) | ||
|
||
access_token, refresh_token = await hisense_login.login(entry.data["username"], entry.data["password"]) | ||
home_id_list = await hisense_login.get_home_id_list(access_token) | ||
# TODO let the user to select home_id | ||
home_id = home_id_list[0] | ||
device_id_list, wifi_id_list = await hisense_login.get_device_id_list(access_token, home_id) | ||
# TODO let the user to select device | ||
device_id = device_id_list[0] | ||
wifi_id = wifi_id_list[0] | ||
|
||
hass.data[DOMAIN][entry.entry_id] = HiSenseAC( | ||
wifi_id=wifi_id, | ||
device_id=device_id, | ||
refresh_token=refresh_token, | ||
session=session | ||
) | ||
# Forward the setup to the climate platform | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(entry, "climate")) | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(entry, "switch")) | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(entry, "button")) | ||
return True | ||
for platform in ("climate", "switch", "button"): | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(entry, platform) | ||
) | ||
|
||
return True | ||
|
||
async def async_unload_entry(hass: core.HomeAssistant, entry: config_entries.ConfigEntry): | ||
# Unload both climate and switch config entries | ||
unload_climate = await hass.config_entries.async_forward_entry_unload(entry, "climate") | ||
unload_switch = await hass.config_entries.async_forward_entry_unload(entry, "switch") | ||
unload_button = await hass.config_entries.async_forward_entry_unload(entry, "button") | ||
if unload_climate and unload_switch and unload_button: | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
return unload_climate and unload_switch and unload_button | ||
return all( | ||
await hass.config_entries.async_forward_entry_unload(entry, platform) | ||
for platform in ("climate", "switch", "button") | ||
) |
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
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,31 +1,93 @@ | ||
import voluptuous as vol | ||
from homeassistant import config_entries | ||
|
||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from homeassistant.helpers import config_validation as cv | ||
from .const import DOMAIN, CONF_USERNAME, CONF_PASSWORD | ||
|
||
from .pyhisenseapi import HiSenseLogin | ||
|
||
class HisenseACConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL | ||
|
||
def __init__(self): | ||
self._home_id_list = None | ||
self._access_token = None | ||
self._refresh_token = None | ||
|
||
async def async_step_user(self, user_input=None): | ||
errors = {} | ||
|
||
if user_input is not None: | ||
# Here you could add code to validate the input, such as attempting to connect to the AC | ||
# For simplicity, we'll assume the input is valid | ||
return self.async_create_entry(title="Hisense Smart Control", data=user_input) | ||
session = async_get_clientsession(self.hass) | ||
hisense_login = HiSenseLogin(session=session) | ||
|
||
try: | ||
access_token, refresh_token = await hisense_login.login( | ||
user_input[CONF_USERNAME], user_input[CONF_PASSWORD] | ||
) | ||
self._home_id_list = await hisense_login.get_home_id_list(access_token) | ||
self._access_token = access_token | ||
self._refresh_token = refresh_token | ||
return await self.async_step_home() | ||
except Exception: | ||
errors["base"] = "invalid_auth" | ||
|
||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema({ | ||
vol.Required(CONF_USERNAME): str, | ||
vol.Required(CONF_PASSWORD, ): str, | ||
}), | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_USERNAME): str, | ||
vol.Required(CONF_PASSWORD): str, | ||
} | ||
), | ||
description_placeholders={ | ||
"username_hint": "app login username", | ||
"password_hint": "app login password", | ||
}, | ||
errors=errors, | ||
) | ||
|
||
|
||
async def async_step_home(self, user_input=None): | ||
errors = {} | ||
|
||
if user_input is not None: | ||
home_id = user_input["home_id"] | ||
session = async_get_clientsession(self.hass) | ||
hisense_login = HiSenseLogin(session=session) | ||
self._device_wifi_id_dict = await hisense_login.get_device_wifi_id_dict( | ||
self._access_token, home_id | ||
) | ||
|
||
return await self.async_step_device() | ||
|
||
return self.async_show_form( | ||
step_id="home", | ||
data_schema=vol.Schema( | ||
{vol.Required("home_id"): vol.In(self._home_id_list)} | ||
), | ||
errors=errors, | ||
) | ||
|
||
async def async_step_device(self, user_input=None): | ||
if user_input is not None: | ||
device_ids = user_input["device_ids"] | ||
devices = [ | ||
{"device_id": device_id, | ||
"wifi_id": self._device_wifi_id_dict[device_id], | ||
"refresh_token": self._refresh_token, | ||
} | ||
for device_id in device_ids | ||
] | ||
return self.async_create_entry( | ||
title="Hisense Smart Control", | ||
data={"devices": devices} | ||
) | ||
|
||
data_schema = vol.Schema( | ||
{ | ||
vol.Required("device_ids"): cv.multi_select( | ||
list(self._device_wifi_id_dict.keys()) | ||
), | ||
} | ||
) | ||
return self.async_show_form(step_id="device", data_schema=data_schema) |
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
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
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