Skip to content

Added support for TekSavvy bandwidth sensor#11186

Merged
fabaff merged 9 commits into
home-assistant:devfrom
jpjodoin:teksavvy
Jan 18, 2018
Merged

Added support for TekSavvy bandwidth sensor#11186
fabaff merged 9 commits into
home-assistant:devfrom
jpjodoin:teksavvy

Conversation

@jpjodoin
Copy link
Copy Markdown
Contributor

@jpjodoin jpjodoin commented Dec 17, 2017

Description:

TekSavvy is a an internet provider in Canada. This sensors allows to monitor bandwidth usage.

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4227

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: teksavvy
    api_key: API_KEY
    total_bandwidth: 400
    monitored_variables:
     - usage
     - usage_gb
     - limit
     - onpeak_download
     - onpeak_upload
     - onpeak_total
     - offpeak_download
     - offpeak_upload
     - offpeak_total
     - onpeak_remaining

Checklist:

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

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

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @jpjodoin,

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!

self.data = dict()
self.data["limit"] = self.bandwidth_cap

def _fetch_data(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Docstrings are required everywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Would be nice if we could add that to lint.

conn.request('GET', req, '', headers)
response = conn.getresponse()
json_data = response.read().decode("utf-8")
data = json.loads(json_data)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One option would be to use the existing rest sensor to retrieve the data as it's done for the pvoutput platform. A more modern approach would be to do it with helpers.aiohttp_client like here and here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Refactored the code to use aiohttp_client.

Thanks !

url = "https://api.teksavvy.com/"\
"web/Usage/UsageSummaryRecords?$filter=IsCurrent%20eq%20true"
with async_timeout.timeout(REQUEST_TIMEOUT, loop=self.loop):
req = yield from self.websession.get(url, headers = headers)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unexpected spaces around keyword / parameter equals

if self.type in self.teksavvydata.data:
self._state = round(self.teksavvydata.data[self.type], 2)

class TekSavvyData(object):
Copy link
Copy Markdown

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

sensors.append(TekSavvySensor(ts_data, variable, name))
async_add_devices(sensors, True)

class TekSavvySensor(Entity):
Copy link
Copy Markdown

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

ret = yield from ts_data.async_update()
except ValueError as error:
_LOGGER.error("Failed conversion %s %s", CONF_TOTAL_BANDWIDTH, error)
if ret == False:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})

@asyncio.coroutine
Copy link
Copy Markdown

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

import logging
from datetime import timedelta
import http.client
import json
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'json' imported but unused

"""
import logging
from datetime import timedelta
import http.client
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'http.client' imported but unused

@jpjodoin
Copy link
Copy Markdown
Contributor Author

jpjodoin commented Jan 7, 2018

@fabaff Ready on my side, let me know if I should change anything else. Thanks!

You can get your API key here:
https://myaccount.teksavvy.com/ApiKey/ApiKeyManagement

TekSavvy only counts download only as part of the bandwidth
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This belongs to the documentation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle
from homeassistant.const import (
CONF_API_KEY, CONF_NAME, CONF_MONITORED_VARIABLES)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please keep the import groups and sorted. Use isort if you don't want to do it manually.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for the isort tip.

vol.Required(CONF_MONITORED_VARIABLES):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_TOTAL_BANDWIDTH): cv.string,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only allow integers here. This way you don't need the casting on line 73.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Allows only positive integer now.

def async_update(self):
"""Get the TekSavvy bandwidth data from the web service."""
headers = {"TekSavvy-APIKey": self.api_key}
_LOGGER.info("Updaing TekSavvy data")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Make this _LOGGER.debug. Less verbose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

self.websession = websession
self.api_key = api_key
self.bandwidth_cap = bandwidth_cap
self.data = dict()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can use {} here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

except ValueError as error:
_LOGGER.error("Failed conversion %s %s", CONF_TOTAL_BANDWIDTH, error)
if ret is False:
return
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should log an invalid API key. I guess that for the users it's not very helpful that a conversation failed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the conversion since we no longer have the cast. Also, added an error message if the user has an invalid API key.

@jpjodoin
Copy link
Copy Markdown
Contributor Author

@fabaff : Changes are done on my side. Thanks.

@jpjodoin
Copy link
Copy Markdown
Contributor Author

Thanks!

@fabaff fabaff merged commit 8ca45ac into home-assistant:dev Jan 18, 2018
@balloob balloob mentioned this pull request Jan 26, 2018
@home-assistant home-assistant locked and limited conversation to collaborators May 29, 2018
@jpjodoin jpjodoin deleted the teksavvy branch August 27, 2019 20:19
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.

5 participants