From 253e154a79c5efda82fc202783f639eb227265e9 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sun, 14 Oct 2018 17:11:39 +0200 Subject: [PATCH] Minor updates (#17436) --- homeassistant/components/habitica/__init__.py | 92 +++++++++---------- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/homeassistant/components/habitica/__init__.py b/homeassistant/components/habitica/__init__.py index 44b9e392157..8e77c6bf50b 100644 --- a/homeassistant/components/habitica/__init__.py +++ b/homeassistant/components/habitica/__init__.py @@ -1,55 +1,54 @@ """ The Habitica API component. -For more details about this platform, please refer to the documentation at +For more details about this component, please refer to the documentation at https://home-assistant.io/components/habitica/ """ - -import logging from collections import namedtuple +import logging import voluptuous as vol -from homeassistant.const import \ - CONF_NAME, CONF_URL, CONF_SENSORS, CONF_PATH, CONF_API_KEY + +from homeassistant.const import ( + CONF_API_KEY, CONF_NAME, CONF_PATH, CONF_SENSORS, CONF_URL) +from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.helpers import \ - config_validation as cv, discovery REQUIREMENTS = ['habitipy==0.2.0'] -_LOGGER = logging.getLogger(__name__) -DOMAIN = "habitica" -CONF_API_USER = "api_user" +_LOGGER = logging.getLogger(__name__) + +CONF_API_USER = 'api_user' + +DEFAULT_URL = 'https://habitica.com' +DOMAIN = 'habitica' ST = SensorType = namedtuple('SensorType', [ - "name", "icon", "unit", "path" + 'name', 'icon', 'unit', 'path' ]) SENSORS_TYPES = { - 'name': ST('Name', None, '', ["profile", "name"]), - 'hp': ST('HP', 'mdi:heart', 'HP', ["stats", "hp"]), - 'maxHealth': ST('max HP', 'mdi:heart', 'HP', ["stats", "maxHealth"]), - 'mp': ST('Mana', 'mdi:auto-fix', 'MP', ["stats", "mp"]), - 'maxMP': ST('max Mana', 'mdi:auto-fix', 'MP', ["stats", "maxMP"]), - 'exp': ST('EXP', 'mdi:star', 'EXP', ["stats", "exp"]), + 'name': ST('Name', None, '', ['profile', 'name']), + 'hp': ST('HP', 'mdi:heart', 'HP', ['stats', 'hp']), + 'maxHealth': ST('max HP', 'mdi:heart', 'HP', ['stats', 'maxHealth']), + 'mp': ST('Mana', 'mdi:auto-fix', 'MP', ['stats', 'mp']), + 'maxMP': ST('max Mana', 'mdi:auto-fix', 'MP', ['stats', 'maxMP']), + 'exp': ST('EXP', 'mdi:star', 'EXP', ['stats', 'exp']), 'toNextLevel': ST( - 'Next Lvl', 'mdi:star', 'EXP', ["stats", "toNextLevel"]), + 'Next Lvl', 'mdi:star', 'EXP', ['stats', 'toNextLevel']), 'lvl': ST( - 'Lvl', 'mdi:arrow-up-bold-circle-outline', 'Lvl', ["stats", "lvl"]), - 'gp': ST('Gold', 'mdi:coin', 'Gold', ["stats", "gp"]), - 'class': ST('Class', 'mdi:sword', '', ["stats", "class"]) + 'Lvl', 'mdi:arrow-up-bold-circle-outline', 'Lvl', ['stats', 'lvl']), + 'gp': ST('Gold', 'mdi:coin', 'Gold', ['stats', 'gp']), + 'class': ST('Class', 'mdi:sword', '', ['stats', 'class']) } INSTANCE_SCHEMA = vol.Schema({ - vol.Optional(CONF_URL, default='https://habitica.com'): cv.url, + vol.Optional(CONF_URL, default=DEFAULT_URL): cv.url, vol.Optional(CONF_NAME): cv.string, vol.Required(CONF_API_USER): cv.string, vol.Required(CONF_API_KEY): cv.string, vol.Optional(CONF_SENSORS, default=list(SENSORS_TYPES)): - vol.All( - cv.ensure_list, - vol.Unique(), - [vol.In(list(SENSORS_TYPES))]) + vol.All(cv.ensure_list, vol.Unique(), [vol.In(list(SENSORS_TYPES))]), }) has_unique_values = vol.Schema(vol.Unique()) # pylint: disable=invalid-name @@ -57,7 +56,7 @@ has_unique_values = vol.Schema(vol.Unique()) # pylint: disable=invalid-name def has_all_unique_users(value): - """Validate that all `api_user`s are unique.""" + """Validate that all API users are unique.""" api_users = [user[CONF_API_USER] for user in value] has_unique_values(api_users) return value @@ -75,9 +74,7 @@ def has_all_unique_users_names(value): INSTANCE_LIST_SCHEMA = vol.All( - cv.ensure_list, - has_all_unique_users, - has_all_unique_users_names, + cv.ensure_list, has_all_unique_users, has_all_unique_users_names, [INSTANCE_SCHEMA]) CONFIG_SCHEMA = vol.Schema({ @@ -87,23 +84,24 @@ CONFIG_SCHEMA = vol.Schema({ SERVICE_API_CALL = 'api_call' ATTR_NAME = CONF_NAME ATTR_PATH = CONF_PATH -ATTR_ARGS = "args" -EVENT_API_CALL_SUCCESS = "{0}_{1}_{2}".format( - DOMAIN, SERVICE_API_CALL, "success") +ATTR_ARGS = 'args' +EVENT_API_CALL_SUCCESS = '{0}_{1}_{2}'.format( + DOMAIN, SERVICE_API_CALL, 'success') SERVICE_API_CALL_SCHEMA = vol.Schema({ vol.Required(ATTR_NAME): str, vol.Required(ATTR_PATH): vol.All(cv.ensure_list, [str]), - vol.Optional(ATTR_ARGS): dict + vol.Optional(ATTR_ARGS): dict, }) async def async_setup(hass, config): - """Set up the habitica service.""" + """Set up the Habitica service.""" + from habitipy.aio import HabitipyAsync + conf = config[DOMAIN] data = hass.data[DOMAIN] = {} websession = async_get_clientsession(hass) - from habitipy.aio import HabitipyAsync class HAHabitipyAsync(HabitipyAsync): """Closure API class to hold session.""" @@ -116,7 +114,7 @@ async def async_setup(hass, config): username = instance[CONF_API_USER] password = instance[CONF_API_KEY] name = instance.get(CONF_NAME) - config_dict = {"url": url, "login": username, "password": password} + config_dict = {'url': url, 'login': username, 'password': password} api = HAHabitipyAsync(config_dict) user = await api.user.get() if name is None: @@ -125,34 +123,30 @@ async def async_setup(hass, config): if CONF_SENSORS in instance: hass.async_create_task( discovery.async_load_platform( - hass, "sensor", DOMAIN, - {"name": name, "sensors": instance[CONF_SENSORS]}, - config)) + hass, 'sensor', DOMAIN, + {'name': name, 'sensors': instance[CONF_SENSORS]}, config)) async def handle_api_call(call): name = call.data[ATTR_NAME] path = call.data[ATTR_PATH] api = hass.data[DOMAIN].get(name) if api is None: - _LOGGER.error( - "API_CALL: User '%s' not configured", name) + _LOGGER.error("API_CALL: User '%s' not configured", name) return try: for element in path: api = api[element] except KeyError: _LOGGER.error( - "API_CALL: Path %s is invalid" - " for api on '{%s}' element", path, element) + "API_CALL: Path %s is invalid for API on '{%s}' element", + path, element) return kwargs = call.data.get(ATTR_ARGS, {}) data = await api(**kwargs) - hass.bus.async_fire(EVENT_API_CALL_SUCCESS, { - "name": name, "path": path, "data": data - }) + hass.bus.async_fire( + EVENT_API_CALL_SUCCESS, {'name': name, 'path': path, 'data': data}) hass.services.async_register( - DOMAIN, SERVICE_API_CALL, - handle_api_call, + DOMAIN, SERVICE_API_CALL, handle_api_call, schema=SERVICE_API_CALL_SCHEMA) return True