LaMetric platform and notify module (#8230)
* First version of a LaMetrci platform with a Notify module * Cleanup, fix formatting bugs * More formatting * Formatting * Updated requirements * formatting * Formatting * More formatting * Dummy commit for new Travis CI run * Refactoring class methods to instance methods * Cleanup unused classed * Removed Eddystone_weatherurl that had nothing to do with this component * Cleanup class methods * Cleanup requirements * Removed broad excepts Removed storage of LaMetric devices * Removed unused import
This commit is contained in:
parent
e12a9eaadd
commit
7536e825fa
4 changed files with 181 additions and 0 deletions
83
homeassistant/components/lametric.py
Normal file
83
homeassistant/components/lametric.py
Normal file
|
@ -0,0 +1,83 @@
|
|||
"""
|
||||
Support for LaMetric time.
|
||||
|
||||
This is the base platform to support LaMetric components:
|
||||
Notify, Light, Mediaplayer
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/lametric/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
REQUIREMENTS = ['lmnotify==0.0.4']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_CLIENT_ID = 'client_id'
|
||||
CONF_CLIENT_SECRET = 'client_secret'
|
||||
|
||||
DOMAIN = 'lametric'
|
||||
LAMETRIC_DEVICES = 'LAMETRIC_DEVICES'
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Required(CONF_CLIENT_ID): cv.string,
|
||||
vol.Required(CONF_CLIENT_SECRET): cv.string,
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
# pylint: disable=broad-except
|
||||
def setup(hass, config):
|
||||
"""Set up the LaMetricManager."""
|
||||
_LOGGER.debug("Setting up LaMetric platform")
|
||||
conf = config[DOMAIN]
|
||||
hlmn = HassLaMetricManager(client_id=conf[CONF_CLIENT_ID],
|
||||
client_secret=conf[CONF_CLIENT_SECRET])
|
||||
devices = hlmn.manager().get_devices()
|
||||
|
||||
found = False
|
||||
hass.data[DOMAIN] = hlmn
|
||||
for dev in devices:
|
||||
_LOGGER.debug("Discovered LaMetric device: %s", dev)
|
||||
found = True
|
||||
|
||||
return found
|
||||
|
||||
|
||||
class HassLaMetricManager():
|
||||
"""
|
||||
A class that encapsulated requests to the LaMetric manager.
|
||||
|
||||
As the original class does not have a re-connect feature that is needed
|
||||
for applications running for a long time as the OAuth tokens expire. This
|
||||
class implements this reconnect() feature.
|
||||
"""
|
||||
|
||||
def __init__(self, client_id, client_secret):
|
||||
"""Initialize HassLaMetricManager and connect to LaMetric."""
|
||||
from lmnotify import LaMetricManager
|
||||
|
||||
_LOGGER.debug("Connecting to LaMetric")
|
||||
self.lmn = LaMetricManager(client_id, client_secret)
|
||||
self._client_id = client_id
|
||||
self._client_secret = client_secret
|
||||
|
||||
def reconnect(self):
|
||||
"""
|
||||
Reconnect to LaMetric.
|
||||
|
||||
This is usually necessary when the OAuth token is expired.
|
||||
"""
|
||||
from lmnotify import LaMetricManager
|
||||
_LOGGER.debug("Reconnecting to LaMetric")
|
||||
self.lmn = LaMetricManager(self._client_id,
|
||||
self._client_secret)
|
||||
|
||||
def manager(self):
|
||||
"""Return the global LaMetricManager instance."""
|
||||
return self.lmn
|
Loading…
Add table
Add a link
Reference in a new issue