def connect_gateway(self): """Connect the gateway in a way that can called by async_add_executor_job.""" try: self._gateway_device = gateway.Gateway(self._host, self._token) # get the gateway info self._gateway_info = self._gateway_device.info() except DeviceException as error: if isinstance(error.__cause__, ChecksumError): raise ConfigEntryAuthFailed(error) from error _LOGGER.error( "DeviceException during setup of xiaomi gateway with host %s: %s", self._host, error, ) return False # get the connected sub devices use_cloud = self._use_cloud or self._gateway_info.model == GATEWAY_MODEL_EU if not use_cloud: # use local query (not supported by all gateway types) try: self._gateway_device.discover_devices() except DeviceException as error: _LOGGER.info( "DeviceException during getting subdevices of xiaomi gateway" " with host %s, trying cloud to obtain subdevices: %s", self._host, error, ) use_cloud = True if use_cloud: # use miio-cloud if (self._cloud_username is None or self._cloud_password is None or self._cloud_country is None): raise ConfigEntryAuthFailed( "Missing cloud credentials in Xiaomi Miio configuration") try: miio_cloud = MiCloud(self._cloud_username, self._cloud_password) if not miio_cloud.login(): raise ConfigEntryAuthFailed( "Could not login to Xiaomi Miio Cloud, check the credentials" ) devices_raw = miio_cloud.get_devices(self._cloud_country) self._gateway_device.get_devices_from_dict(devices_raw) except DeviceException as error: _LOGGER.error( "DeviceException during setup of xiaomi gateway with host %s: %s", self._host, error, ) return False return True
def test_get_devices(self): mc = MiCloud(os.getenv("USERNAME"), os.getenv("PASSWORD")) self.assertTrue(mc.login()) self.assertIsNotNone(mc.get_token()) res = mc.get_devices(save=True) self.assertIsNotNone(res) self.assertTrue(type(res) == list)
def thread_get_tokens(self): self._device_list = [] mc = MiCloud(self._username, self._password) if mc.login(): for country in self._country: dev_list = mc.get_devices(country=country) for dev in dev_list: dev["country"] = country self._device_list.append(dev) else: raise Exception("Login failed, please check username/password!")
def connect_gateway(self): """Connect the gateway in a way that can called by async_add_executor_job.""" try: self._gateway_device = gateway.Gateway(self._host, self._token) # get the gateway info self._gateway_device.info() # get the connected sub devices if self._use_cloud or self._gateway_info.model == GATEWAY_MODEL_EU: if (self._cloud_username is None or self._cloud_password is None or self._cloud_country is None): raise ConfigEntryAuthFailed( "Missing cloud credentials in Xiaomi Miio configuration" ) # use miio-cloud miio_cloud = MiCloud(self._cloud_username, self._cloud_password) if not miio_cloud.login(): raise ConfigEntryAuthFailed( "Could not login to Xioami Miio Cloud, check the credentials" ) devices_raw = miio_cloud.get_devices(self._cloud_country) self._gateway_device.get_devices_from_dict(devices_raw) else: # use local query (not supported by all gateway types) self._gateway_device.discover_devices() except DeviceException: _LOGGER.error( "DeviceException during setup of xiaomi gateway with host %s", self._host, ) return False return True
import miio from micloud import MiCloud import time vacuum_ip = "YOUR_VACUUM_CLEANER_IP" vacuum_token = "YOUR_TOKEN" cloud_user_id = "YOUR_MI_ACCOUNT_ID" cloud_user_password = "******" cloud_country = "ru" map_link = "retry" if not (vacuum_ip == "" or vacuum_token == ""): vacuum = miio.Vacuum(vacuum_ip, vacuum_token) counter = 10 while map_link == "retry" and counter > 0: print("Invalid map: " + map_link) time.sleep(0.2) map_link = vacuum.map()[0] counter = counter - 1 if map_link == "retry": raise ValueError('Can not obtain the map link') print("Retrieved map: " + map_link) mc = MiCloud(cloud_user_id, cloud_user_password) mc.login() cloud_token = mc.get_token() device_list = mc.get_devices() mc.download_vacuum_map(country=cloud_country, map_id=map_link)
def test_login_access_denied(self): mc = MiCloud('DEFFNOTAUSER', 'DEFFWRONGPW') self.assertRaises(MiCloudAccessDenied, mc.login)
async def async_step_cloud(self, user_input=None): """Configure a xiaomi miio device through the Miio Cloud.""" errors = {} if user_input is not None: if user_input[CONF_MANUAL]: return await self.async_step_manual() cloud_username = user_input.get(CONF_CLOUD_USERNAME) cloud_password = user_input.get(CONF_CLOUD_PASSWORD) cloud_country = user_input.get(CONF_CLOUD_COUNTRY) if not cloud_username or not cloud_password or not cloud_country: errors["base"] = "cloud_credentials_incomplete" return self.async_show_form(step_id="cloud", data_schema=DEVICE_CLOUD_CONFIG, errors=errors) miio_cloud = MiCloud(cloud_username, cloud_password) try: if not await self.hass.async_add_executor_job(miio_cloud.login ): errors["base"] = "cloud_login_error" except MiCloudAccessDenied: errors["base"] = "cloud_login_error" if errors: return self.async_show_form(step_id="cloud", data_schema=DEVICE_CLOUD_CONFIG, errors=errors) devices_raw = await self.hass.async_add_executor_job( miio_cloud.get_devices, cloud_country) if not devices_raw: errors["base"] = "cloud_no_devices" return self.async_show_form(step_id="cloud", data_schema=DEVICE_CLOUD_CONFIG, errors=errors) self.cloud_devices = {} for device in devices_raw: if not device.get("parent_id"): name = device["name"] model = device["model"] list_name = f"{name} - {model}" self.cloud_devices[list_name] = device self.cloud_username = cloud_username self.cloud_password = cloud_password self.cloud_country = cloud_country if self.host is not None: for device in self.cloud_devices.values(): cloud_host = device.get("localip") if cloud_host == self.host: self.extract_cloud_info(device) return await self.async_step_connect() if len(self.cloud_devices) == 1: self.extract_cloud_info(list(self.cloud_devices.values())[0]) return await self.async_step_connect() return await self.async_step_select() return self.async_show_form(step_id="cloud", data_schema=DEVICE_CLOUD_CONFIG, errors=errors)