-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Split out fastdotcom into a component and a sensor platform #20341
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a08f97a
Split out fastdotcom into a component and a sensor platform
rohankapoorcom 057f578
Update .coveragerc
rohankapoorcom 32851f5
Switching to async and using a Throttle
rohankapoorcom e23c1a7
Add the async_track_time_interval call
rohankapoorcom afda0b0
Remove the throttle
rohankapoorcom 05f8421
Reorder sensor methods and add should_poll property
rohankapoorcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """ | ||
| Support for testing internet speed via Fast.com. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/fastdotcom/ | ||
| """ | ||
|
|
||
| import logging | ||
| from datetime import timedelta | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.const import CONF_UPDATE_INTERVAL | ||
| from homeassistant.helpers.discovery import async_load_platform | ||
| from homeassistant.helpers.dispatcher import dispatcher_send | ||
| from homeassistant.helpers.event import async_track_time_interval | ||
|
|
||
| REQUIREMENTS = ['fastdotcom==0.0.3'] | ||
|
|
||
| DOMAIN = 'fastdotcom' | ||
| DATA_UPDATED = '{}_data_updated'.format(DOMAIN) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_MANUAL = 'manual' | ||
|
|
||
| DEFAULT_INTERVAL = timedelta(hours=1) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Optional(CONF_UPDATE_INTERVAL, default=DEFAULT_INTERVAL): | ||
| vol.All( | ||
| cv.time_period, cv.positive_timedelta | ||
| ), | ||
| vol.Optional(CONF_MANUAL, default=False): cv.boolean, | ||
| }) | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the Fast.com component.""" | ||
| conf = config[DOMAIN] | ||
| data = hass.data[DOMAIN] = SpeedtestData( | ||
| hass, conf[CONF_UPDATE_INTERVAL], conf[CONF_MANUAL] | ||
| ) | ||
|
|
||
| def update(call=None): | ||
| """Service call to manually update the data.""" | ||
| data.update() | ||
|
|
||
| hass.services.async_register(DOMAIN, 'speedtest', update) | ||
|
|
||
| hass.async_create_task( | ||
| async_load_platform(hass, 'sensor', DOMAIN, {}, config) | ||
| ) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class SpeedtestData: | ||
| """Get the latest data from fast.com.""" | ||
|
|
||
| def __init__(self, hass, interval, manual): | ||
| """Initialize the data object.""" | ||
| self.data = None | ||
| self._hass = hass | ||
| if not manual: | ||
| async_track_time_interval(self._hass, self.update, interval) | ||
|
|
||
| def update(self): | ||
| """Get the latest data from fast.com.""" | ||
| from fastdotcom import fast_com | ||
| _LOGGER.debug("Executing fast.com speedtest") | ||
| self.data = {'download': fast_com()} | ||
| dispatcher_send(self._hass, DATA_UPDATED) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """ | ||
| Support for Fast.com internet speed testing sensor. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.fastdotcom/ | ||
| """ | ||
| import logging | ||
|
|
||
| from homeassistant.components.fastdotcom import DOMAIN as FASTDOTCOM_DOMAIN, \ | ||
| DATA_UPDATED | ||
| from homeassistant.core import callback | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
| from homeassistant.helpers.restore_state import RestoreEntity | ||
|
|
||
| DEPENDENCIES = ['fastdotcom'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| ICON = 'mdi:speedometer' | ||
|
|
||
| UNIT_OF_MEASUREMENT = 'Mbit/s' | ||
|
|
||
|
|
||
| async def async_setup_platform(hass, config, async_add_entities, | ||
| discovery_info=None): | ||
| """Set up the Fast.com sensor.""" | ||
| async_add_entities([SpeedtestSensor(hass.data[FASTDOTCOM_DOMAIN])]) | ||
|
|
||
|
|
||
| class SpeedtestSensor(RestoreEntity): | ||
| """Implementation of a FAst.com sensor.""" | ||
|
|
||
| def __init__(self, speedtest_data): | ||
| """Initialize the sensor.""" | ||
| self._name = 'Fast.com Download' | ||
| self.speedtest_client = speedtest_data | ||
| self._state = None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the device.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit of measurement of this entity, if any.""" | ||
| return UNIT_OF_MEASUREMENT | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return icon.""" | ||
| return ICON | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """Return the polling requirement for this sensor.""" | ||
| return False | ||
|
|
||
| async def async_added_to_hass(self): | ||
|
rohankapoorcom marked this conversation as resolved.
|
||
| """Handle entity which will be added.""" | ||
| await super().async_added_to_hass() | ||
| state = await self.async_get_last_state() | ||
| if not state: | ||
| return | ||
| self._state = state.state | ||
|
|
||
| async_dispatcher_connect( | ||
| self.hass, DATA_UPDATED, self._schedule_immediate_update | ||
| ) | ||
|
|
||
| def update(self): | ||
| """Get the latest data and update the states.""" | ||
| data = self.speedtest_client.data | ||
| if data is None: | ||
| return | ||
| self._state = data['download'] | ||
|
|
||
| @callback | ||
| def _schedule_immediate_update(self): | ||
| self.async_schedule_update_ha_state(True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| speedtest: | ||
| description: Immediately take a speedest with Fast.com |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.