Added support for TekSavvy bandwidth sensor#11186
Conversation
| self.data = dict() | ||
| self.data["limit"] = self.bandwidth_cap | ||
|
|
||
| def _fetch_data(self): |
There was a problem hiding this comment.
Docstrings are required everywhere.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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): |
| sensors.append(TekSavvySensor(ts_data, variable, name)) | ||
| async_add_devices(sensors, True) | ||
|
|
||
| class TekSavvySensor(Entity): |
| ret = yield from ts_data.async_update() | ||
| except ValueError as error: | ||
| _LOGGER.error("Failed conversion %s %s", CONF_TOTAL_BANDWIDTH, error) | ||
| if ret == False: |
There was a problem hiding this comment.
comparison to False should be 'if cond is False:' or 'if not cond:'
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| }) | ||
|
|
||
| @asyncio.coroutine |
| import logging | ||
| from datetime import timedelta | ||
| import http.client | ||
| import json |
| """ | ||
| import logging | ||
| from datetime import timedelta | ||
| import http.client |
|
@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 |
There was a problem hiding this comment.
This belongs to the documentation.
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.util import Throttle | ||
| from homeassistant.const import ( | ||
| CONF_API_KEY, CONF_NAME, CONF_MONITORED_VARIABLES) |
There was a problem hiding this comment.
Please keep the import groups and sorted. Use isort if you don't want to do it manually.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Only allow integers here. This way you don't need the casting on line 73.
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
Make this _LOGGER.debug. Less verbose.
| self.websession = websession | ||
| self.api_key = api_key | ||
| self.bandwidth_cap = bandwidth_cap | ||
| self.data = dict() |
| except ValueError as error: | ||
| _LOGGER.error("Failed conversion %s %s", CONF_TOTAL_BANDWIDTH, error) | ||
| if ret is False: | ||
| return |
There was a problem hiding this comment.
You should log an invalid API key. I guess that for the users it's not very helpful that a conversation failed.
There was a problem hiding this comment.
Removed the conversion since we no longer have the cast. Also, added an error message if the user has an invalid API key.
|
@fabaff : Changes are done on my side. Thanks. |
|
Thanks! |
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):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.