Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EU Issues #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/device-miio.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const EventEmitter = require('events');
const fetch = require('node-fetch');
const miioProtocol = require('./protocol-miio');
const miCloudProtocol = require('./protocol-micloud');
const debug = require('debug')('mihome:miio');

const sleep = time => {
return new Promise(resolve => {
Expand All @@ -14,7 +15,7 @@ const sleep = time => {
module.exports = class MiioDevice extends EventEmitter {

constructor({
id, address, token, protocol, refresh,
id, address, token, protocol, refresh, country,
}) {
super();
this.id = String(id);
Expand All @@ -32,6 +33,7 @@ module.exports = class MiioDevice extends EventEmitter {
id,
token,
});
miCloudProtocol.setDefaultCountry(country);
}

get properties() {
Expand Down Expand Up @@ -87,21 +89,18 @@ module.exports = class MiioDevice extends EventEmitter {
propsChunks.push(props.slice(i, i + chunkSize));
}

let result = [];
for (const propChunk of propsChunks) {
const resultChunk = await this.getProperties(propChunk);
if (!resultChunk) {
throw new Error('Properties is empty');
}
if (resultChunk.length !== propChunk.length) {
throw new Error(`Result ${JSON.stringify(resultChunk)} and props ${JSON.stringify(propChunk)} does not match length`);
debug(`Result ${JSON.stringify(resultChunk)} and props ${JSON.stringify(propChunk)} does not match length`);
}
result = result.concat(resultChunk);
propChunk.forEach((prop, i) => {
data[prop] = resultChunk[i];
});
}
props.forEach((prop, i) => {
const value = result[i];
data[prop] = value;
});
this._properties = Object.assign(this._properties, data);
this.emit('available', true);
this.emit('properties', data);
Expand Down
9 changes: 7 additions & 2 deletions lib/protocol-micloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class MiCloudProtocol {

this.countries = ['ru', 'us', 'tw', 'sg', 'cn', 'de'];
this.locale = 'en';
this.defaultCountry = 'cn';
}

setDefaultCountry(country = 'cn') {
this.defaultCountry = country;
}

get isLoggedIn() {
Expand Down Expand Up @@ -62,7 +67,7 @@ class MiCloudProtocol {
this.serviceToken = null;
}

async request(path, data, country = 'cn') {
async request(path, data, country = this.defaultCountry) {
if (!this.isLoggedIn) {
throw new Error('Pls login before make any request');
}
Expand Down Expand Up @@ -138,7 +143,7 @@ class MiCloudProtocol {
return data.result;
}

_getApiUrl(country) {
_getApiUrl(country = this.defaultCountry) {
country = country.trim().toLowerCase();
return `https://${country === 'cn' ? '' : `${country}.`}api.io.mi.com/app`;
}
Expand Down