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

添加 camera 设备 #310

Open
wants to merge 20 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
151 changes: 151 additions & 0 deletions custom_components/xiaomi_miot_raw/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""Platform for camera integration."""
import asyncio
import logging
from functools import partial
import collections

from datetime import timedelta
import json
from collections import OrderedDict
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.components.camera import (
SUPPORT_ON_OFF, SUPPORT_STREAM,
PLATFORM_SCHEMA,
Camera
)
from homeassistant.const import *
from homeassistant.exceptions import PlatformNotReady
from homeassistant.util import color
from miio.exceptions import DeviceException
from .deps.miio_new import MiotDevice
import miio
import copy

from .basic_dev_class import (
GenericMiotDevice,
ToggleableMiotDevice,
MiotSubDevice,
MiotSubToggleableDevice,
MiotIRDevice,
)
from . import async_generic_setup_platform
from .deps.const import (
DOMAIN,
ATTR_SYSSTATUS,
CONF_UPDATE_INSTANT,
CONF_MAPPING,
CONF_CONTROL_PARAMS,
CONF_CLOUD,
CONF_MODEL,
ATTR_STATE_VALUE,
ATTR_MODEL,
ATTR_FIRMWARE_VERSION,
ATTR_HARDWARE_VERSION,
SCHEMA,
MAP,
DUMMY_IP,
DUMMY_TOKEN,
)

TYPE = 'camera'

_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=10)
DEFAULT_NAME = "Generic MIoT " + TYPE
DATA_KEY = TYPE + '.' + DOMAIN

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
SCHEMA
)

# pylint: disable=unused-argument
@asyncio.coroutine
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
await async_generic_setup_platform(
hass,
config,
async_add_devices,
discovery_info,
TYPE,
{'default': MiotCamera}
)


async def async_setup_entry(hass, config_entry, async_add_entities):
config = copy.copy(hass.data[DOMAIN]['configs'].get(config_entry.entry_id, dict(config_entry.data)))
await async_setup_platform(hass, config, async_add_entities)


class MiotCamera(GenericMiotDevice, Camera):
def enable_motion_detection(self) -> None:
pass

def disable_motion_detection(self) -> None:
pass

def __init__(self, device, config, device_info, hass, main_mi_type):
self._device: miio.chuangmi_camera.ChuangmiCamera = None # Just for type hint
self._state: bool = None
GenericMiotDevice.__init__(self, device, config, device_info, hass, main_mi_type)
Camera.__init__(self)
self.register_callback(self.update_callback)

@property
def should_poll(self):
return True

def update_callback(self):
device_status = self.get_devicestatus()
# sleep will return [{'sysstatus': 'sleep'}]
# otherwise will return all other status
self._state = (len(device_status) > 1)
_LOGGER.debug(f"camera.update_callback result: {self._state}")

def get_devicestatus(self):
return self._device.send('get_devicestatus', {
'alarmsensitivity': "",
'cameraprompt': "",
'flip': "",
'infraredlight': "",
'ledstatus': "",
'recordtype': "",
'wakeuplevel': "",
})

@property
def supported_features(self) -> int:
return SUPPORT_ON_OFF

@property
def is_recording(self) -> bool:
return self._state

@property
def is_streaming(self) -> bool:
return False

@property
def is_on(self) -> bool:
return True

async def async_turn_on(self) -> None:
await self.async_do_turn_on(True)

async def async_turn_off(self) -> None:
await self.async_do_turn_on(False)

async def async_do_turn_on(self, new_status) -> None:
_LOGGER.info(f"Start camera.async_do_turn_on( {new_status} )")

if new_status:
cmd = "normal"
else:
cmd = "sleep"
result = self._device.send("set_" + ATTR_SYSSTATUS, [cmd])
if result != ['ok']:
_LOGGER.warning("result for send {}, {}".format(cmd, result))
return

self._state = new_status
self.schedule_update_ha_state()
5 changes: 5 additions & 0 deletions custom_components/xiaomi_miot_raw/deps/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
ATTR_MODEL = "model"
ATTR_FIRMWARE_VERSION = "firmware_version"
ATTR_HARDWARE_VERSION = "hardware_version"
ATTR_SYSSTATUS = "sysstatus"

DOMAIN = 'xiaomi_miot_raw'
SUPPORTED_DOMAINS = [
"camera",
"sensor",
"switch",
"light",
Expand Down Expand Up @@ -85,6 +87,9 @@
}

MAP = {
"camera": {
"camera",
},
"sensor": {
"air_fryer",
"air_monitor",
Expand Down
5 changes: 5 additions & 0 deletions custom_components/xiaomi_miot_raw/deps/special_devices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
SPECIAL_DEVICES={
"chuangmi.camera.xiaobai":{
"device_type": ['camera'],
"mapping": {"camera": {"switch_status": {"siid": 1, "piid": 1}}},
"params": {"camera": {"switch_status": {"power_on": True, "power_off": False}, "main": True}}
},
"chuangmi.plug.212a01":{
"device_type": ['switch','sensor'],
"mapping": {"switch": {"switch_status": {"siid": 2, "piid": 1}, "temperature": {"siid": 2, "piid": 6}, "working_time": {"siid": 2, "piid": 7}}, "power_consumption": {"power_consumption": {"siid": 5, "piid": 1}, "electric_current": {"siid": 5, "piid": 2}, "voltage": {"siid": 5, "piid": 3}, "electric_power": {"siid": 5, "piid": 6}}},
Expand Down