Skip to content

Commit

Permalink
Add custom open telnet command option
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Feb 23, 2021
1 parent b18dd23 commit 321d923
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion custom_components/xiaomi_gateway3/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from . import DOMAIN
from .core import gateway3
from .core.gateway3 import TELNET_CMD
from .core.xiaomi_cloud import MiCloud

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -56,8 +57,8 @@ async def async_step_user(self, user_input=None):
data_schema=vol.Schema({
vol.Required('host', default=device['localip']): str,
vol.Required('token', default=device['token']): str,
vol.Required('telnet_cmd', default=TELNET_CMD): str,
}),
description_placeholders={'error_text': ''}
)

if DOMAIN in self.hass.data and 'devices' in self.hass.data[DOMAIN]:
Expand Down Expand Up @@ -116,6 +117,7 @@ async def async_step_token(self, user_input=None, error=None):
data_schema=vol.Schema({
vol.Required('host'): str,
vol.Required('token'): str,
vol.Required('telnet_cmd', default=TELNET_CMD): str,
}),
errors={'base': error} if error else None
)
Expand Down Expand Up @@ -180,6 +182,7 @@ async def async_step_user(self, user_input=None):

host = self.entry.options['host']
token = self.entry.options['token']
telnet_cmd = self.entry.options.get('telnet_cmd')
ble = self.entry.options.get('ble', True)
stats = self.entry.options.get('stats', False)
debug = self.entry.options.get('debug', [])
Expand All @@ -192,6 +195,7 @@ async def async_step_user(self, user_input=None):
data_schema=vol.Schema({
vol.Required('host', default=host): str,
vol.Required('token', default=token): str,
vol.Optional('telnet_cmd', default=telnet_cmd): str,
vol.Required('ble', default=ble): bool,
vol.Required('stats', default=stats): bool,
vol.Optional('debug', default=debug): cv.multi_select(
Expand Down
8 changes: 7 additions & 1 deletion custom_components/xiaomi_gateway3/core/gateway3.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# MAC reverse
RE_REVERSE = re.compile(r'(..)(..)(..)(..)(..)(..)')

TELNET_CMD = '{"method":"enable_telnet_service","params":""}'


class GatewayV:
"""Handling different firmware versions."""
Expand Down Expand Up @@ -339,6 +341,7 @@ class Gateway3(Thread, GatewayV, GatewayMesh, GatewayStats):
time_offset = 0
pair_model = None
pair_payload = None
telnet_cmd = None

def __init__(self, host: str, token: str, config: dict, **options):
super().__init__(daemon=True)
Expand All @@ -359,6 +362,8 @@ def __init__(self, host: str, token: str, config: dict, **options):
else options['parent'])
self.default_devices = config['devices'] if config else None

self.telnet_cmd = options.get('telnet_cmd') or TELNET_CMD

if 'true' in self._debug:
self.miio.debug = True

Expand Down Expand Up @@ -444,7 +449,8 @@ def _check_port(self, port: int):

def _enable_telnet(self):
"""Enable telnet with miio protocol."""
if self.miio.send("enable_telnet_service") != 'ok':
raw = json.loads(self.telnet_cmd)
if self.miio.send(raw['method'], raw.get('params')) != 'ok':
self.debug(f"Can't enable telnet")
return False
return True
Expand Down
6 changes: 4 additions & 2 deletions custom_components/xiaomi_gateway3/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
}
},
"token": {
"description": "[Obtain](https://github.com/Maxmudjon/com.xiaomi-miio/blob/master/docs/obtain_token.md) Mi Home token.\nNote: Gateway with firmware **1.4.6_0043** higher are supported only after [soldering](https://github.com/AlexxIT/XiaomiGateway3/wiki)",
"description": "You can obtain Mi Home token automatically with [Cloud integration](https://github.com/AlexxIT/XiaomiGateway3#obtain-mi-home-device-token) or [manually](https://github.com/Maxmudjon/com.xiaomi-miio/blob/master/docs/obtain_token.md). Check supported firmwares info in [component docs](https://github.com/AlexxIT/XiaomiGateway3#supported-firmwares).",
"data": {
"host": "Host",
"token": "Token"
"token": "Token",
"telnet_cmd": "Open Telnet command"
}
}
}
Expand All @@ -45,6 +46,7 @@
"data": {
"host": "Host",
"token": "Token",
"telnet_cmd": "Open Telnet command",
"ble": "Support BLE Devices",
"stats": "Zigbee and BLE performance data",
"debug": "Debug",
Expand Down
6 changes: 4 additions & 2 deletions custom_components/xiaomi_gateway3/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
}
},
"token": {
"description": "[Получите](https://github.com/Maxmudjon/com.xiaomi-miio/blob/master/docs/obtain_token.md) Mi Home токен. Шлюз с прошивкой **1.4.6_0043** и выше поддерживается только после [перепрошивки](https://github.com/AlexxIT/XiaomiGateway3/wiki)",
"description": "Вы можете получить Mi Home токен автоматически с помощью [облачной интеграции](https://github.com/AlexxIT/XiaomiGateway3#obtain-mi-home-device-token) или [вручную](https://github.com/Maxmudjon/com.xiaomi-miio/blob/master/docs/obtain_token.md). Ознакомьтесь с поддерживаемыми версиями прошивок в [документации компонента](https://github.com/AlexxIT/XiaomiGateway3#supported-firmwares).",
"data": {
"host": "IP-адрес",
"token": "Токен"
"token": "Токен",
"telnet_cmd": "Команда для открытия Telnet"
}
}
}
Expand All @@ -45,6 +46,7 @@
"data": {
"host": "IP-адрес",
"token": "Токен",
"telnet_cmd": "Команда для открытия Telnet",
"ble": "Поддержка BLE устройств",
"stats": "Детализация работы Zigbee и BLE",
"debug": "Отладка",
Expand Down

0 comments on commit 321d923

Please sign in to comment.