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

chuangmi_camera: Improve home monitoring support #751

Merged
Merged
Changes from 5 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
187 changes: 186 additions & 1 deletion miio/chuangmi_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,76 @@
_LOGGER = logging.getLogger(__name__)


CONST_HIGH_SENSITIVITY = [
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already defined as an enum below, so better simply use something like [enumvalue] * times to create the array when needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice, fixed it.

CONST_LOW_SENSITIVITY = [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
]


class Direction(enum.Enum):
"""Rotation direction."""

Expand All @@ -21,6 +91,46 @@ class Direction(enum.Enum):
Down = 4


class MotionDetectionSensitivity(enum.Enum):
"""Motion detection sensitivity."""

High = 3
Low = 1


class HomeMonitoringMode(enum.IntEnum):
"""Home monitoring mode."""

Off = 0
AllDay = 1
Custom = 2


class NASState(enum.IntEnum):
"""NAS state."""

Off = 2
On = 3


class NASSyncInterval(enum.IntEnum):
"""NAS sync interval."""

Realtime = 300
Hour = 3600
Day = 86400


class NASVideoRetentionTime(enum.IntEnum):
"""NAS video retention time."""

Week = 604800
Month = 2592000
Quarter = 7776000
HalfYear = 15552000
Year = 31104000


class CameraStatus:
"""Container for status reports from the Xiaomi Chuangmi Camera."""

Expand Down Expand Up @@ -283,7 +393,7 @@ def night_mode_on(self):
return self.send("set_night_mode", [2])

@command(
click.argument("mode", type=EnumType(Direction, False)),
click.argument("direction", type=EnumType(Direction, False)),
default_output=format_output("Rotating to direction '{direction.name}'"),
)
def rotate(self, direction: Direction):
Expand All @@ -294,3 +404,78 @@ def rotate(self, direction: Direction):
def alarm(self):
"""Sound a loud alarm for 10 seconds."""
return self.send("alarm_sound")

@command(
click.argument("sensitivity", type=EnumType(MotionDetectionSensitivity, False)),
default_output=format_output("Setting motion sensitivity '{sensitivity.name}'"),
)
def set_motion_sensitivity(self, sensitivity: MotionDetectionSensitivity):
"""Set motion sensitivity (high, low)."""
return self.send(
"set_motion_region",
CONST_HIGH_SENSITIVITY
if sensitivity == MotionDetectionSensitivity.High
else CONST_LOW_SENSITIVITY,
)

@command(
click.argument("mode", type=EnumType(HomeMonitoringMode, False)),
click.argument("start-hour", default=10),
click.argument("start-minute", default=0),
click.argument("end-hour", default=17),
click.argument("end-minute", default=0),
click.argument("notify", default=1),
click.argument("interval", default=5),
default_output=format_output("Setting alarm config to '{mode.name}'"),
)
def set_home_monitoring_config(
self,
mode: HomeMonitoringMode = HomeMonitoringMode.AllDay,
start_hour: int = 10,
start_minute: int = 0,
end_hour: int = 17,
end_minute: int = 0,
notify: int = 1,
interval: int = 5,
):
"""Set home monitoring configuration"""
return self.send(
"setAlarmConfig",
[mode, start_hour, start_minute, end_hour, end_minute, notify, interval],
)

@command(default_output=format_output("Clearing NAS directory"),)
def clear_nas_dir(self):
"""Clear NAS directory"""
return self.send("nas_clear_dir", [[]],)

@command(default_output=format_output("Getting NAS config info"),)
def get_nas_config(self):
"""Get NAS config info"""
return self.send("nas_get_config", {},)

@command(
click.argument("state", type=EnumType(NASState, False)),
click.argument("share"),
click.argument("sync-interval", type=EnumType(NASSyncInterval, False)),
click.argument(
"video-retention-time", type=EnumType(NASVideoRetentionTime, False)
),
default_output=format_output("Setting NAS config to '{state.name}'"),
)
def set_nas_config(
self,
state: NASState,
share={},
sync_interval: NASSyncInterval = NASSyncInterval.Realtime,
video_retention_time: NASVideoRetentionTime = NASVideoRetentionTime.Week,
):
"""Set NAS configuration"""
return self.send(
"nas_set_config",
{
"state": state,
"sync_interval": sync_interval,
"video_retention_time": video_retention_time,
},
)