Files
home-assistant-core/homeassistant/components/streamlabswater/__init__.py
T
cpopp 1bca313421 Add Streamlabs Water Monitor (#21205)
* Add Streamlabs Water Monitor

* Fail Streamlabswater component setup when given invalid parameters

The Streamlabs Water component is unable to recover if it is given
an invalid API key or location id so this change is to ensure
we validate they are correct during setup and return a failure
if they are not.

* Prime Streamlabswater component sensors so data is available immediately

The sensors for the component were not causing an immediate load of
data from the API when being set up so there was some lag after
startup before values would show up.  This change does an explicit
update when the sensors are setup to ensure data is viewable
immediately after startup.

* Switch Streamlabswater logging to use %s for string formatting

* Update Streamlabswater component with correct dependencies

Dependencies were incorrectly specified using DEPENDS rather
than DEPENDENCIES

* Streamlabswater pull request feedback

Remove detailed class docstrings since they're in the documentation,
reduce code duplication in sensor classes, and remove periods from
the end of log messages.

* Reduce line length in Streamlabswater sensor

* Add docstring on Streamlabswater service callback method

* Get rid of unnecessary initializers in Streamlabswater sensor

* Add manifest file for Streamlabs Water Monitor

* Remove unused REQUIREMENTS
2019-06-06 13:55:08 -07:00

85 lines
2.3 KiB
Python

"""Support for Streamlabs Water Monitor devices."""
import logging
import voluptuous as vol
from homeassistant.const import CONF_API_KEY
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
DOMAIN = 'streamlabswater'
_LOGGER = logging.getLogger(__name__)
ATTR_AWAY_MODE = 'away_mode'
SERVICE_SET_AWAY_MODE = 'set_away_mode'
AWAY_MODE_AWAY = 'away'
AWAY_MODE_HOME = 'home'
STREAMLABSWATER_COMPONENTS = [
'sensor', 'binary_sensor'
]
CONF_LOCATION_ID = "location_id"
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_LOCATION_ID): cv.string
})
}, extra=vol.ALLOW_EXTRA)
SET_AWAY_MODE_SCHEMA = vol.Schema({
vol.Required(ATTR_AWAY_MODE): vol.In([AWAY_MODE_AWAY, AWAY_MODE_HOME])
})
def setup(hass, config):
"""Set up the streamlabs water component."""
from streamlabswater import streamlabswater
conf = config[DOMAIN]
api_key = conf.get(CONF_API_KEY)
location_id = conf.get(CONF_LOCATION_ID)
client = streamlabswater.StreamlabsClient(api_key)
locations = client.get_locations().get('locations')
if locations is None:
_LOGGER.error("Unable to retrieve locations. Verify API key")
return False
if location_id is None:
location = locations[0]
location_id = location['locationId']
_LOGGER.info("Streamlabs Water Monitor auto-detected location_id=%s",
location_id)
else:
location = next((
l for l in locations if location_id == l['locationId']), None)
if location is None:
_LOGGER.error("Supplied location_id is invalid")
return False
location_name = location['name']
hass.data[DOMAIN] = {
'client': client,
'location_id': location_id,
'location_name': location_name
}
for component in STREAMLABSWATER_COMPONENTS:
discovery.load_platform(hass, component, DOMAIN, {}, config)
def set_away_mode(service):
"""Set the StreamLabsWater Away Mode."""
away_mode = service.data.get(ATTR_AWAY_MODE)
client.update_location(location_id, away_mode)
hass.services.register(
DOMAIN, SERVICE_SET_AWAY_MODE, set_away_mode,
schema=SET_AWAY_MODE_SCHEMA)
return True