Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pyLoad download sensor #10089

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 13 commits into from
Nov 2, 2017
Merged

pyLoad download sensor #10089

merged 13 commits into from
Nov 2, 2017

Conversation

iMarkus
Copy link
Contributor

@iMarkus iMarkus commented Oct 23, 2017

Description:

Support for monitoring pyLoad download speed.

Pull request in home-assistant.github.io with documentation: home-assistant/home-assistant.io#3742

Example entry for configuration.yaml:

sensor:
  - platform: pyload
    host: 192.168.0.100
    port: 8001
    username: py
    password: load

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies are only imported inside functions that use them (example).
  • New files were added to .coveragerc.

@homeassistant
Copy link
Contributor

Hi @iMarkus,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!


def update(self):
"""Update cached response."""
try:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace


@Throttle(MIN_TIME_BETWEEN_UPDATES)

def update(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank lines found after function decorator

return response.json()

except requests.exceptions.ConnectionError as conn_exc:
_LOGGER.error("Failed to update pyLoad status from %s. Error: %s", self.api_url + 'statusServer', conn_exc)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (119 > 79 characters)


try:
response = requests.post(
self.api_url + 'statusServer', json=payload, cookies=self.login.cookies,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (88 > 79 characters)

self.headers = {'content-type': CONTENT_TYPE_JSON}

if username is not None and password is not None:
self.payload = {'username':username, 'password':password}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing whitespace after ':'

# Error calling the API, already logged in api.update()
return

if self.api.status is None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation contains mixed spaces and tabs

except requests.exceptions.ConnectionError:
# Error calling the API, already logged in api.update()
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation contains tabs
indentation contains mixed spaces and tabs
blank line contains whitespace

"""Return the state of the sensor."""
return self._state

@property

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation contains mixed spaces and tabs

def state(self):
"""Return the state of the sensor."""
return self._state

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation contains tabs
indentation contains mixed spaces and tabs
blank line contains whitespace

"""Return the name of the sensor."""
return self._name

@property

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation contains mixed spaces and tabs

@iMarkus iMarkus changed the title Create pyload.py pyLoad download sensor Oct 24, 2017
devices.append(new_sensor)
add_devices(devices)

class pyLoadSensor(Entity):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

})

# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

else:
self._state = value

class pyLoadAPI(object):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

"""Initialize pyLoad API and set headers needed later."""
self.api_url = api_url
self.status = None
self.headers = {'content-type': CONTENT_TYPE_JSON}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'content-type' should be present in const.py.


if username is not None and password is not None:
self.payload = {'username': username, 'password': password}
self.login = requests.post(api_url + 'login', data=self.payload)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All requests need a timeout.

self.api = api
self._state = None
self._unit_of_measurement = sensor_type[2]
self.update()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed see above.

DEFAULT_NAME = 'pyLoad'
DEFAULT_PORT = 8000

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty fast.

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_MONITORED_VARIABLES, default=['speed']):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one sensor type possible. If there are no other sensors possible with pyload then speed should be set and configuration option be removed. If there are other you plan to integrate then it's ok.


except requests.exceptions.ConnectionError as conn_exc:
_LOGGER.error("Failed to update pyLoad status from %s. Error: %s",
self.api_url + 'statusServer', conn_exc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would only make sense to log the URL if you could use multiple instances.

"""Initialize a new pyLoad sensor."""
self._name = '{} {}'.format(client_name, sensor_type[1])
self.type = sensor_type[0]
self.client_name = client_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used.

return response.json()

except requests.exceptions.ConnectionError as conn_exc:
_LOGGER.error("Failed to update pyLoad status. Error: %s", conn_exc)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (80 > 79 characters)


if username is not None and password is not None:
self.payload = {'username': username, 'password': password}
self.login = requests.post(api_url + 'login', data=self.payload,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

def __init__(self, api, sensor_type, client_name):
"""Initialize a new pyLoad sensor."""
self._name = '{} {}'.format(client_name, sensor_type[1])
self.type = sensor_type[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_NAME, CONF_PORT,
CONF_SSL, HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON, CONF_MONITORED_VARIABLES)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (84 > 79 characters)

Copy link
Member

@fabaff fabaff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🐦

@fabaff fabaff merged commit 4d19092 into home-assistant:dev Nov 2, 2017
@balloob balloob mentioned this pull request Nov 17, 2017
@MaluNoPeleke
Copy link

It doesn't work with https only:

2017-11-19 11:48:26 ERROR (Thread-4) [homeassistant.components.sensor.pyload] Error setting up pyLoad API: HTTPConnectionPool(host='https', port=80): Max retries exceeded with url: //my.dns.tld:8000/api/login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xb11f8450>: Failed to establish a new connection: [Errno -2] Name or service not known',))

@MartinHjelmare
Copy link
Member

If there's an issue, please open an issue and link to the PR which you suspect. Don't report issues in merged PRs. Thanks!

@home-assistant home-assistant locked and limited conversation to collaborators Nov 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants