Fix socket bug with Yi in 0.72 (#15109)

* Fixes BrokenPipeError exceptions with Yi (#15108)

* Make sure to close the socket
pull/14146/merge
Aaron Bach 2018-06-23 13:22:48 -06:00 committed by GitHub
parent ee31f89049
commit 91962e2681
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 14 deletions

View File

@ -53,7 +53,6 @@ class YiCamera(Camera):
"""Initialize."""
super().__init__()
self._extra_arguments = config.get(CONF_FFMPEG_ARGUMENTS)
self._ftp = None
self._last_image = None
self._last_url = None
self._manager = hass.data[DATA_FFMPEG]
@ -64,8 +63,6 @@ class YiCamera(Camera):
self.user = config[CONF_USERNAME]
self.passwd = config[CONF_PASSWORD]
hass.async_add_job(self._connect_to_client)
@property
def brand(self):
"""Camera brand."""
@ -76,38 +73,35 @@ class YiCamera(Camera):
"""Return the name of this camera."""
return self._name
async def _connect_to_client(self):
"""Attempt to establish a connection via FTP."""
async def _get_latest_video_url(self):
"""Retrieve the latest video file from the customized Yi FTP server."""
from aioftp import Client, StatusCodeError
ftp = Client()
try:
await ftp.connect(self.host)
await ftp.login(self.user, self.passwd)
self._ftp = ftp
except StatusCodeError as err:
raise PlatformNotReady(err)
async def _get_latest_video_url(self):
"""Retrieve the latest video file from the customized Yi FTP server."""
from aioftp import StatusCodeError
try:
await self._ftp.change_directory(self.path)
await ftp.change_directory(self.path)
dirs = []
for path, attrs in await self._ftp.list():
for path, attrs in await ftp.list():
if attrs['type'] == 'dir' and '.' not in str(path):
dirs.append(path)
latest_dir = dirs[-1]
await self._ftp.change_directory(latest_dir)
await ftp.change_directory(latest_dir)
videos = []
for path, _ in await self._ftp.list():
for path, _ in await ftp.list():
videos.append(path)
if not videos:
_LOGGER.info('Video folder "%s" empty; delaying', latest_dir)
return None
await ftp.quit()
return 'ftp://{0}:{1}@{2}:{3}{4}/{5}/{6}'.format(
self.user, self.passwd, self.host, self.port, self.path,
latest_dir, videos[-1])