-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Xiaomi Cameras - multiple models #14244
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
Changes from 9 commits
89b2dd9
7599e77
7e6fcec
9319778
31c6f67
564daa0
bdab2e0
3c40007
79567f4
a1cc751
696b4c1
6e5ae6d
8573391
9315d7a
d789ea5
bc2c874
2fccf01
aba4dfd
bc00d3f
d4be965
fa91354
bd22fe1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| """ | ||
| This component provides support for Xiaomi Cameras. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/camera.xiaomi/ | ||
| """ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.camera import Camera, PLATFORM_SCHEMA | ||
| from homeassistant.components.ffmpeg import DATA_FFMPEG | ||
| from homeassistant.const import (CONF_HOST, CONF_NAME, CONF_PATH, | ||
| CONF_PASSWORD, CONF_PORT, CONF_USERNAME) | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream | ||
|
|
||
| DEPENDENCIES = ['ffmpeg'] | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_BRAND = 'Xiaomi Home Camera' | ||
| DEFAULT_PASSWORD = '' | ||
| DEFAULT_PATH = '/media/mmcblk0p1/record' | ||
| DEFAULT_PORT = 21 | ||
| DEFAULT_USERNAME = 'root' | ||
|
|
||
| CONF_FFMPEG_ARGUMENTS = 'ffmpeg_arguments' | ||
| CONF_MODEL = 'model' | ||
|
|
||
| MODEL_YI = 'yi' | ||
| MODEL_XIAOFANG = 'xiaofang' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_NAME): cv.string, | ||
| vol.Required(CONF_HOST): cv.string, | ||
| vol.Required(CONF_MODEL): vol.Any(MODEL_YI, | ||
| MODEL_XIAOFANG), | ||
| vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.string, | ||
| vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string, | ||
| vol.Optional(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Optional(CONF_FFMPEG_ARGUMENTS): cv.string | ||
| }) | ||
|
|
||
|
|
||
| async def async_setup_platform(hass, | ||
| config, | ||
| async_add_devices, | ||
| discovery_info=None): | ||
| """Set up a Xiaomi Camera.""" | ||
| _LOGGER.debug('Received configuration: %s', config) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't log the config. |
||
| async_add_devices([XiaomiCamera(hass, config)], True) | ||
|
|
||
|
|
||
| class XiaomiCamera(Camera): | ||
| """Define an implementation of a Xiaomi Camera.""" | ||
|
|
||
| def __init__(self, hass, config): | ||
| """Initialize.""" | ||
| super().__init__() | ||
| self._extra_arguments = config.get(CONF_FFMPEG_ARGUMENTS) | ||
| self._last_image = None | ||
| self._last_url = None | ||
| self._manager = hass.data[DATA_FFMPEG] | ||
| self._name = config[CONF_NAME] | ||
| self.host = config[CONF_HOST] | ||
| self._model = config[CONF_MODEL] | ||
| self.port = config[CONF_PORT] | ||
| self.path = config[CONF_PATH] | ||
| self.user = config[CONF_USERNAME] | ||
| self.passwd = config[CONF_PASSWORD] | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of this camera.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def brand(self): | ||
| """Camera brand.""" | ||
| return DEFAULT_BRAND | ||
|
|
||
| @property | ||
| def model(self): | ||
| """Return the camera model.""" | ||
| return self._model | ||
|
|
||
| def get_latest_video_url(self): | ||
| """Retrieve the latest video file from the Xiaomi Camera FTP server.""" | ||
| from ftplib import FTP, error_perm | ||
|
|
||
| ftp = FTP(self.host) | ||
| try: | ||
| ftp.login(self.user, self.passwd) | ||
| except error_perm as exc: | ||
| _LOGGER.error('There was an error while logging into the camera') | ||
| _LOGGER.debug(exc) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still the double logging issue... just merge the two
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though double logging is already there in some of the official released codes, am fixing it |
||
| return False | ||
|
|
||
| try: | ||
| ftp.cwd(self.path) | ||
| except error_perm as exc: | ||
| _LOGGER.error('Unable to find path: %s', self.path) | ||
| _LOGGER.debug(exc) | ||
| return False | ||
|
|
||
| if self._model == MODEL_YI: | ||
| dirs = [d for d in ftp.nlst() if '.' not in d] | ||
| if not dirs: | ||
| _LOGGER.warning("There don't appear to be any uploaded videos") | ||
| return False | ||
|
|
||
| if self._model == MODEL_XIAOFANG: | ||
| dirs = [d for d in ftp.nlst() if '.' not in d] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same for both models. Can it be moved up? |
||
| if not dirs: | ||
| _LOGGER.warning("There don't appear to be any folders") | ||
| return False | ||
|
|
||
| first_dir = dirs[-1] | ||
| try: | ||
| ftp.cwd(first_dir) | ||
| except error_perm as exc: | ||
| _LOGGER.error('Unable to find path: %s', first_dir) | ||
| _LOGGER.debug(exc) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't double log
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think both logging are different, 1st one is level 1 and next is level 2.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yet both will log the same issue, just leave one of them
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but 1st will report if 20180503 directory exists and 2nd will report if 15 directory exists, there may be cases where the 2nd level will not be there when the camera option was set to only record when there is an motion detection.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they will both appear when log level is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do have both models and tested both and worked fine.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have a look at existing tests: create a new file for your platform
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to create a test?. I don't see it was created for yi already. And it is just a few modification over yi which was already released.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yi camera had no options. I would mock the FTP server, and validade the existence of the image file in a different path depending on the configured model.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This camera has same options like Yi camera, and am not sure how to do testing, as I am not an expert in python. The code of this one is same as Yi Camera, except one directory above.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should also implement:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
| return False | ||
|
|
||
| dirs = [d for d in ftp.nlst() if '.' not in d] | ||
| if not dirs: | ||
| _LOGGER.warning("There don't appear to be any uploaded videos") | ||
| return False | ||
|
|
||
| latest_dir = dirs[-1] | ||
| ftp.cwd(latest_dir) | ||
| videos = [v for v in ftp.nlst() if '.tmp' not in v] | ||
| if not videos: | ||
| _LOGGER.info('Video folder "%s" is empty; delaying', latest_dir) | ||
| return False | ||
|
|
||
| if self._model == 'xiaofang': | ||
| video = videos[-2] | ||
| else: | ||
| video = videos[-1] | ||
|
|
||
| return 'ftp://{0}:{1}@{2}:{3}{4}/{5}'.format( | ||
| self.user, self.passwd, self.host, self.port, ftp.pwd(), video) | ||
|
|
||
| async def async_camera_image(self): | ||
| """Return a still image response from the camera.""" | ||
| from haffmpeg import ImageFrame, IMAGE_JPEG | ||
|
|
||
| url = await self.hass.async_add_job(self.get_latest_video_url) | ||
| if url != self._last_url: | ||
| ffmpeg = ImageFrame(self._manager.binary, loop=self.hass.loop) | ||
| self._last_image = await asyncio.shield(ffmpeg.get_image( | ||
| url, output_format=IMAGE_JPEG, | ||
| extra_cmd=self._extra_arguments), loop=self.hass.loop) | ||
| self._last_url = url | ||
|
|
||
| return self._last_image | ||
|
|
||
| async def handle_async_mjpeg_stream(self, request): | ||
| """Generate an HTTP MJPEG stream from the camera.""" | ||
| from haffmpeg import CameraMjpeg | ||
|
|
||
| stream = CameraMjpeg(self._manager.binary, loop=self.hass.loop) | ||
| await stream.open_camera( | ||
| self._last_url, extra_cmd=self._extra_arguments) | ||
|
|
||
| await async_aiohttp_proxy_stream( | ||
| self.hass, request, stream, | ||
| 'multipart/x-mixed-replace;boundary=ffserver') | ||
| await stream.close() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't used.