-
-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
89b2dd9
Added support for Xiaofang Camera
vaidyasr 7599e77
Added entry for Xiaofang 1080p camera
vaidyasr 7e6fcec
Code fix
vaidyasr 9319778
Minor comment fix
vaidyasr 31c6f67
Updated coveragerc for Xiaomi cameras
vaidyasr 564daa0
Added Xiaomi Camera
vaidyasr bdab2e0
Minor code fix
vaidyasr 3c40007
Minor code fix
vaidyasr 79567f4
Added model property
vaidyasr a1cc751
Update xiaomi.py
vaidyasr 696b4c1
Minor code fix
vaidyasr 6e5ae6d
Update xiaomi.py
vaidyasr 8573391
Update xiaomi.py
vaidyasr 9315d7a
Minor code fix
vaidyasr d789ea5
Package requirement fix due to Version conflict
vaidyasr bc2c874
To fix conflicts
vaidyasr 2fccf01
Update package_constraints.txt
vaidyasr aba4dfd
Minor fix
vaidyasr bc00d3f
Update xiaomi.py
vaidyasr d4be965
Update xiaomi.py
vaidyasr fa91354
Update xiaomi.py
vaidyasr bd22fe1
Don't update on add.
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| """ | ||
| This component provides support for Xiaofang Cameras | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/camera.xiaofang/ | ||
| """ | ||
| 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 = 'Xiaofang Home Camera' | ||
| DEFAULT_PASSWORD = '' | ||
| DEFAULT_PATH = '/media/mmcblk0p1/record' | ||
| DEFAULT_PORT = 21 | ||
| DEFAULT_USERNAME = 'root' | ||
|
|
||
| CONF_FFMPEG_ARGUMENTS = 'ffmpeg_arguments' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_NAME): cv.string, | ||
| vol.Required(CONF_HOST): cv.string, | ||
| 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 Xiaofang Camera.""" | ||
| _LOGGER.debug('Received configuration: %s', config) | ||
| async_add_devices([XiaofangCamera(hass, config)], True) | ||
|
|
||
|
|
||
| class XiaofangCamera(Camera): | ||
| """Define an implementation of a Xiaofang 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.get(CONF_NAME) | ||
| self.host = config.get(CONF_HOST) | ||
| self.port = config.get(CONF_PORT) | ||
| self.path = config.get(CONF_PATH) | ||
| self.user = config.get(CONF_USERNAME) | ||
| self.passwd = config.get(CONF_PASSWORD) | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of this camera.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def brand(self): | ||
| """Camera brand.""" | ||
| return DEFAULT_BRAND | ||
|
|
||
| def get_latest_video_url(self): | ||
| """Retrieve the latest video file from the customized Xiaofang 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) | ||
| 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 | ||
|
|
||
| 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 | ||
|
|
||
| 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) | ||
| 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 = ftp.nlst() | ||
| if not videos: | ||
| _LOGGER.info('Video folder "%s" is empty; delaying', latest_dir) | ||
| return False | ||
|
|
||
| return 'ftp://{0}:{1}@{2}:{3}{4}/{5}/{6}/{7}'.format( | ||
| self.user, self.passwd, self.host, self.port, self.path, | ||
| first_dir,latest_dir, videos[-2]) | ||
|
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. missing whitespace after ',' |
||
|
|
||
| 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() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
line too long (85 > 79 characters)