Skip to content

Commit

Permalink
Update Async methods (#6)
Browse files Browse the repository at this point in the history
* Update pydroid_ipcam.py

* Update setup.py

* Update pydroid_ipcam.py

* Update .travis.yml

* Update pydroid_ipcam.py

* Update tox.ini

* Update pydroid_ipcam.py
  • Loading branch information
pvizeli authored Dec 13, 2018
1 parent fb81d77 commit c9efa1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ sudo: false
matrix:
fast_finish: true
include:
- python: "3.4"
env: TOXENV=lint
- python: "3.5"
env: TOXENV=lint
- python: "3.6"
env: TOXENV=lint
- python: "3.7"
env: TOXENV=lint
dist: xenial
install: pip install -U tox
language: python
script: tox
34 changes: 14 additions & 20 deletions pydroid_ipcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import logging

import aiohttp
import async_timeout
import yarl
from yarl import URL

_LOGGER = logging.getLogger(__name__)

Expand All @@ -13,7 +12,7 @@
]


class PyDroidIPCam(object):
class PyDroidIPCam:
"""The Android device running IP Webcam."""

def __init__(self, loop, websession, host, port, username=None,
Expand Down Expand Up @@ -52,21 +51,18 @@ def available(self):
"""Return True if is available."""
return self._available

@asyncio.coroutine
def _request(self, path):
async def _request(self, path):
"""Make the actual request and return the parsed response."""
url = '{}{}'.format(self.base_url, path)
data = None

try:
with async_timeout.timeout(self._timeout, loop=self.loop):
response = yield from self.websession.get(url, auth=self._auth)

async with self.websession.get(url, auth=self._auth, timeout=self._timeout) as response:
if response.status == 200:
if response.headers['content-type'] == 'application/json':
data = yield from response.json()
data = await response.json()
else:
data = yield from response.text()
data = await response.text()

except (asyncio.TimeoutError, aiohttp.ClientError) as error:
_LOGGER.error('Failed to communicate with IP Webcam: %s', error)
Expand All @@ -76,18 +72,16 @@ def _request(self, path):
self._available = True
if isinstance(data, str):
return data.find("Ok") != -1
else:
return data
return data

@asyncio.coroutine
def update(self):
async def update(self):
"""Fetch the latest data from IP Webcam."""
status_data = yield from self._request('/status.json?show_avail=1')
status_data = await self._request('/status.json?show_avail=1')

if status_data:
self.status_data = status_data

sensor_data = yield from self._request('/sensors.json')
sensor_data = await self._request('/sensors.json')
if sensor_data:
self.sensor_data = sensor_data

Expand All @@ -104,7 +98,7 @@ def current_settings(self):
except ValueError:
val = val

if val == 'on' or val == 'off':
if val in ('on', 'off'):
val = (val == 'on')

settings[key] = val
Expand Down Expand Up @@ -140,7 +134,7 @@ def available_settings(self):
except ValueError:
subval = subval

if subval == 'on' or subval == 'off':
if val in ('on', 'off'):
subval = (subval == 'on')

available[key].append(subval)
Expand Down Expand Up @@ -196,7 +190,7 @@ def record(self, record=True, tag=None):
"""
path = '/startvideo?force=1' if record else '/stopvideo?force=1'
if record and tag is not None:
path = '/startvideo?force=1&tag={}'.format(yarl.quote(tag))
path = '/startvideo?force=1&tag={}'.format(URL(tag).raw_path)

return self._request(path)

Expand Down Expand Up @@ -234,7 +228,7 @@ def set_quality(self, quality=100):
Return a coroutine.
"""
return self.change_setting('quality', quality)

def set_motion_detect(self, activate=True):
"""Set motion detection on/off.
Return a coroutine.
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
with open('README.rst') as f:
long_description = f.read()

VERSION = "0.8"
VERSION = "0.9"

setup(
name='pydroid-ipcam',
Expand All @@ -23,16 +23,15 @@
'Operating System :: OS Independent',
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
keywords=['android', 'ipcam', 'api', 'asyncio'],
zip_safe=False,
platforms='any',
py_modules=['pydroid_ipcam'],
install_requires=[
'async_timeout',
'aiohttp',
'yarl',
],
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ basepython = python3
ignore_errors = True
deps =
pylint
flake8
commands =
flake8 pydroid_ipcam.py
pylint pydroid_ipcam.py

0 comments on commit c9efa1e

Please sign in to comment.