-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Split out dovado to a component and sensor platform #20339
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
MartinHjelmare
merged 7 commits into
home-assistant:dev
from
rohankapoorcom:dovado-split
Jan 27, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c2927c
Split out dovado to a component and sensor platform
rohankapoorcom 714202d
Lint
rohankapoorcom b4a3b93
Address code review comments (#20339)
rohankapoorcom 98858ee
Switch to using a notify platform for dovado SMS (#20339)
rohankapoorcom e35207a
Optimizing imports
rohankapoorcom 646a8ae
Remove return on `setup_platform`.
rohankapoorcom 18cfa1a
Clean up unneeded constants
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,79 @@ | ||
| """ | ||
| Support for Dovado router. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/dovado/ | ||
| """ | ||
| import logging | ||
| from datetime import timedelta | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.const import ( | ||
| CONF_USERNAME, CONF_PASSWORD, CONF_HOST, CONF_PORT, | ||
| DEVICE_DEFAULT_NAME) | ||
| from homeassistant.util import Throttle | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| REQUIREMENTS = ['dovado==0.4.1'] | ||
|
|
||
| DOMAIN = 'dovado' | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Optional(CONF_HOST): cv.string, | ||
| vol.Optional(CONF_PORT): cv.port, | ||
| }) | ||
|
|
||
| MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) | ||
|
|
||
|
|
||
| def setup(hass, config): | ||
| """Set up the Dovado component.""" | ||
| import dovado | ||
|
|
||
| hass.data[DOMAIN] = DovadoData( | ||
| dovado.Dovado( | ||
| config[CONF_USERNAME], | ||
| config[CONF_PASSWORD], | ||
| config.get(CONF_HOST), | ||
| config.get(CONF_PORT) | ||
| ) | ||
| ) | ||
| return True | ||
|
|
||
|
|
||
| class DovadoData: | ||
| """Maintains a connection to the router.""" | ||
|
|
||
| def __init__(self, client): | ||
| """Set up a new Dovado connection.""" | ||
| self._client = client | ||
| self.state = {} | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Name of the router.""" | ||
| return self.state.get("product name", DEVICE_DEFAULT_NAME) | ||
|
|
||
| @Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
| def update(self): | ||
| """Update device state.""" | ||
| try: | ||
| self.state = self._client.state or {} | ||
| if not self.state: | ||
| return False | ||
| self.state.update( | ||
| connected=self.state.get("modem status") == "CONNECTED") | ||
| _LOGGER.debug("Received: %s", self.state) | ||
| return True | ||
| except OSError as error: | ||
| _LOGGER.warning("Could not contact the router: %s", error) | ||
|
|
||
| @property | ||
| def client(self): | ||
| """Dovado client instance.""" | ||
| return self._client | ||
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,38 @@ | ||
| """ | ||
| Support for SMS notifications from the Dovado router. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/notify.dovado/ | ||
| """ | ||
| import logging | ||
|
|
||
| from homeassistant.components.dovado import DOMAIN as DOVADO_DOMAIN | ||
| from homeassistant.components.notify import BaseNotificationService, \ | ||
| ATTR_TARGET | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEPENDENCIES = ['dovado'] | ||
|
|
||
|
|
||
| def get_service(hass, config, discovery_info=None): | ||
| """Get the Dovado Router SMS notification service.""" | ||
| return DovadoSMSNotificationService(hass.data[DOVADO_DOMAIN].client) | ||
|
|
||
|
|
||
| class DovadoSMSNotificationService(BaseNotificationService): | ||
| """Implement the notification service for the Dovado SMS component.""" | ||
|
|
||
| def __init__(self, client): | ||
| """Initialize the service.""" | ||
| self._client = client | ||
|
|
||
| def send_message(self, message, **kwargs): | ||
| """Send SMS to the specified target phone number.""" | ||
| target = kwargs.get(ATTR_TARGET) | ||
|
|
||
| if not target: | ||
| _LOGGER.error("One target is required") | ||
| return | ||
|
|
||
| self._client.send_sms(target, message) |
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,116 @@ | ||
| """ | ||
| Support for sensors from the Dovado router. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.dovado/ | ||
| """ | ||
| import logging | ||
| import re | ||
| from datetime import timedelta | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.components.dovado import DOMAIN as DOVADO_DOMAIN | ||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import CONF_SENSORS | ||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEPENDENCIES = ['dovado'] | ||
|
|
||
| MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) | ||
|
|
||
| SENSOR_UPLOAD = 'upload' | ||
| SENSOR_DOWNLOAD = 'download' | ||
| SENSOR_SIGNAL = 'signal' | ||
| SENSOR_NETWORK = 'network' | ||
| SENSOR_SMS_UNREAD = 'sms' | ||
|
|
||
| SENSORS = { | ||
| SENSOR_NETWORK: ('signal strength', 'Network', None, | ||
| 'mdi:access-point-network'), | ||
| SENSOR_SIGNAL: ('signal strength', 'Signal Strength', '%', | ||
| 'mdi:signal'), | ||
| SENSOR_SMS_UNREAD: ('sms unread', 'SMS unread', '', | ||
| 'mdi:message-text-outline'), | ||
| SENSOR_UPLOAD: ('traffic modem tx', 'Sent', 'GB', | ||
| 'mdi:cloud-upload'), | ||
| SENSOR_DOWNLOAD: ('traffic modem rx', 'Received', 'GB', | ||
| 'mdi:cloud-download'), | ||
| } | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_SENSORS): vol.All( | ||
| cv.ensure_list, [vol.In(SENSORS)] | ||
| ), | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_entities, discovery_info=None): | ||
| """Set up the Dovado sensor platform.""" | ||
| dovado = hass.data[DOVADO_DOMAIN] | ||
|
|
||
| entities = [] | ||
| for sensor in config[CONF_SENSORS]: | ||
| entities.append(DovadoSensor(dovado, sensor)) | ||
|
|
||
| add_entities(entities) | ||
|
|
||
|
|
||
| class DovadoSensor(Entity): | ||
| """Representation of a Dovado sensor.""" | ||
|
|
||
| def __init__(self, data, sensor): | ||
| """Initialize the sensor.""" | ||
| self._data = data | ||
| self._sensor = sensor | ||
| self._state = self._compute_state() | ||
|
|
||
| def _compute_state(self): | ||
| state = self._data.state.get(SENSORS[self._sensor][0]) | ||
| if self._sensor == SENSOR_NETWORK: | ||
| match = re.search(r"\((.+)\)", state) | ||
| return match.group(1) if match else None | ||
| if self._sensor == SENSOR_SIGNAL: | ||
| try: | ||
| return int(state.split()[0]) | ||
| except ValueError: | ||
| return None | ||
| if self._sensor == SENSOR_SMS_UNREAD: | ||
| return int(state) | ||
| if self._sensor in [SENSOR_UPLOAD, SENSOR_DOWNLOAD]: | ||
| return round(float(state) / 1e6, 1) | ||
| return state | ||
|
|
||
| def update(self): | ||
| """Update sensor values.""" | ||
| self._data.update() | ||
| self._state = self._compute_state() | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return "{} {}".format(self._data.name, SENSORS[self._sensor][1]) | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the sensor state.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return the icon for the sensor.""" | ||
| return SENSORS[self._sensor][3] | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit of measurement.""" | ||
| return SENSORS[self._sensor][2] | ||
|
|
||
| @property | ||
| def device_state_attributes(self): | ||
| """Return the state attributes.""" | ||
| return {k: v for k, v in self._data.state.items() | ||
| if k not in ['date', 'time']} |
Oops, something went wrong.
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.