Skip to content

Commit

Permalink
Add smb share feature for Chuangmi Camera (#1482)
Browse files Browse the repository at this point in the history
The Xiaomi App have limitation in setting NAS location, it has to be in
the local network. It discover the NAS then list them then let me
choose. I have a NAS across the VPN and actually it's accessible, but
not able to be discovered and set.
This function can help people config NAS outside the local network.

Example:
```shell
miiocli chuangmicamera --ip 192.168.60.11 --token xxxxxxxxxx set_nas_config on "smb://user:pass@192.168.40.2/camera" realtime quarter
```

If the `share` param left empty, It will keep the last config.

Tested on `chuangmi.camera.ipc013`

Co-authored-by: Teemu R. <tpr@iki.fi>
  • Loading branch information
0x5e and rytilahti committed Oct 10, 2022
1 parent aa27188 commit dc55eba
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions miio/chuangmi_camera.py
@@ -1,8 +1,11 @@
"""Xiaomi Chuangmi camera (chuangmi.camera.ipc009, ipc013, ipc019, 038a2) support."""

import enum
import ipaddress
import logging
import socket
from typing import Any, Dict
from urllib.parse import urlparse

import click

Expand Down Expand Up @@ -362,7 +365,7 @@ def get_nas_config(self):

@command(
click.argument("state", type=EnumType(NASState)),
click.argument("share"),
click.argument("share", type=str),
click.argument("sync-interval", type=EnumType(NASSyncInterval)),
click.argument("video-retention-time", type=EnumType(NASVideoRetentionTime)),
default_output=format_output("Setting NAS config to '{state.name}'"),
Expand All @@ -375,13 +378,27 @@ def set_nas_config(
video_retention_time: NASVideoRetentionTime = NASVideoRetentionTime.Week,
):
"""Set NAS configuration."""
if share is None:
share = {}
return self.send(
"nas_set_config",
{
"state": state,
"sync_interval": sync_interval,
"video_retention_time": video_retention_time,
},
)

params: Dict[str, Any] = {
"state": state,
"sync_interval": sync_interval,
"video_retention_time": video_retention_time,
}

share = urlparse(share)
if share.scheme == "smb":
ip = socket.gethostbyname(share.hostname)
reversed_ip = ".".join(reversed(ip.split(".")))
addr = int(ipaddress.ip_address(reversed_ip))

params["share"] = {
"type": 1,
"name": share.hostname,
"addr": addr,
"dir": share.path.lstrip("/"),
"group": "WORKGROUP",
"user": share.username,
"pass": share.password,
}

return self.send("nas_set_config", params)

0 comments on commit dc55eba

Please sign in to comment.