Use string formatting (#18886)

* Use string formatting

* Fix lint issue

* Fix change
pull/18917/head
Paulus Schoutsen 2018-12-02 10:53:09 +01:00 committed by GitHub
commit a62fc7ca04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 69 deletions

View File

@ -14,46 +14,48 @@ DEPENDENCIES = ['sense']
_LOGGER = logging.getLogger(__name__)
BIN_SENSOR_CLASS = 'power'
MDI_ICONS = {'ac': 'air-conditioner',
'aquarium': 'fish',
'car': 'car-electric',
'computer': 'desktop-classic',
'cup': 'coffee',
'dehumidifier': 'water-off',
'dishes': 'dishwasher',
'drill': 'toolbox',
'fan': 'fan',
'freezer': 'fridge-top',
'fridge': 'fridge-bottom',
'game': 'gamepad-variant',
'garage': 'garage',
'grill': 'stove',
'heat': 'fire',
'heater': 'radiatior',
'humidifier': 'water',
'kettle': 'kettle',
'leafblower': 'leaf',
'lightbulb': 'lightbulb',
'media_console': 'set-top-box',
'modem': 'router-wireless',
'outlet': 'power-socket-us',
'papershredder': 'shredder',
'printer': 'printer',
'pump': 'water-pump',
'settings': 'settings',
'skillet': 'pot',
'smartcamera': 'webcam',
'socket': 'power-plug',
'sound': 'speaker',
'stove': 'stove',
'trash': 'trash-can',
'tv': 'television',
'vacuum': 'robot-vacuum',
'washer': 'washing-machine'}
MDI_ICONS = {
'ac': 'air-conditioner',
'aquarium': 'fish',
'car': 'car-electric',
'computer': 'desktop-classic',
'cup': 'coffee',
'dehumidifier': 'water-off',
'dishes': 'dishwasher',
'drill': 'toolbox',
'fan': 'fan',
'freezer': 'fridge-top',
'fridge': 'fridge-bottom',
'game': 'gamepad-variant',
'garage': 'garage',
'grill': 'stove',
'heat': 'fire',
'heater': 'radiatior',
'humidifier': 'water',
'kettle': 'kettle',
'leafblower': 'leaf',
'lightbulb': 'lightbulb',
'media_console': 'set-top-box',
'modem': 'router-wireless',
'outlet': 'power-socket-us',
'papershredder': 'shredder',
'printer': 'printer',
'pump': 'water-pump',
'settings': 'settings',
'skillet': 'pot',
'smartcamera': 'webcam',
'socket': 'power-plug',
'sound': 'speaker',
'stove': 'stove',
'trash': 'trash-can',
'tv': 'television',
'vacuum': 'robot-vacuum',
'washer': 'washing-machine',
}
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Sense sensor."""
"""Set up the Sense binary sensor."""
if discovery_info is None:
return
@ -67,14 +69,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
def sense_to_mdi(sense_icon):
"""Convert sense icon to mdi icon."""
return 'mdi:' + MDI_ICONS.get(sense_icon, 'power-plug')
return 'mdi:{}'.format(MDI_ICONS.get(sense_icon, 'power-plug'))
class SenseDevice(BinarySensorDevice):
"""Implementation of a Sense energy device binary sensor."""
def __init__(self, data, device):
"""Initialize the sensor."""
"""Initialize the Sense binary sensor."""
self._name = device['name']
self._id = device['id']
self._icon = sense_to_mdi(device['icon'])

View File

@ -8,20 +8,20 @@ import logging
import voluptuous as vol
from homeassistant.helpers.discovery import load_platform
from homeassistant.const import (CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT)
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
REQUIREMENTS = ['sense_energy==0.5.1']
_LOGGER = logging.getLogger(__name__)
SENSE_DATA = 'sense_data'
ACTIVE_UPDATE_RATE = 60
DEFAULT_TIMEOUT = 5
DOMAIN = 'sense'
ACTIVE_UPDATE_RATE = 60
DEFAULT_TIMEOUT = 5
SENSE_DATA = 'sense_data'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
@ -41,8 +41,8 @@ def setup(hass, config):
timeout = config[DOMAIN][CONF_TIMEOUT]
try:
hass.data[SENSE_DATA] = Senseable(api_timeout=timeout,
wss_timeout=timeout)
hass.data[SENSE_DATA] = Senseable(
api_timeout=timeout, wss_timeout=timeout)
hass.data[SENSE_DATA].authenticate(username, password)
hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE
except SenseAuthenticationException:

View File

@ -4,27 +4,31 @@ Support for monitoring a Sense energy sensor.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.sense/
"""
from datetime import timedelta
import logging
from datetime import timedelta
from homeassistant.components.sense import SENSE_DATA
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
from homeassistant.components.sense import SENSE_DATA
DEPENDENCIES = ['sense']
_LOGGER = logging.getLogger(__name__)
ACTIVE_NAME = 'Energy'
PRODUCTION_NAME = 'Production'
ACTIVE_TYPE = 'active'
CONSUMPTION_NAME = 'Usage'
ACTIVE_TYPE = 'active'
DEPENDENCIES = ['sense']
ICON = 'mdi:flash'
MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=300)
PRODUCTION_NAME = 'Production'
class SensorConfig:
"""Data structure holding sensor config."""
"""Data structure holding sensor configuration."""
def __init__(self, name, sensor_type):
"""Sensor name and type to pass to API."""
@ -33,19 +37,17 @@ class SensorConfig:
# Sensor types/ranges
SENSOR_TYPES = {'active': SensorConfig(ACTIVE_NAME, ACTIVE_TYPE),
'daily': SensorConfig('Daily', 'DAY'),
'weekly': SensorConfig('Weekly', 'WEEK'),
'monthly': SensorConfig('Monthly', 'MONTH'),
'yearly': SensorConfig('Yearly', 'YEAR')}
SENSOR_TYPES = {
'active': SensorConfig(ACTIVE_NAME, ACTIVE_TYPE),
'daily': SensorConfig('Daily', 'DAY'),
'weekly': SensorConfig('Weekly', 'WEEK'),
'monthly': SensorConfig('Monthly', 'MONTH'),
'yearly': SensorConfig('Yearly', 'YEAR'),
}
# Production/consumption variants
SENSOR_VARIANTS = [PRODUCTION_NAME.lower(), CONSUMPTION_NAME.lower()]
ICON = 'mdi:flash'
MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=300)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Sense sensor."""
@ -73,8 +75,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
update_call = update_active
else:
update_call = update_trends
devices.append(Sense(data, name, sensor_type,
is_production, update_call))
devices.append(Sense(
data, name, sensor_type, is_production, update_call))
add_entities(devices)
@ -83,9 +85,9 @@ class Sense(Entity):
"""Implementation of a Sense energy sensor."""
def __init__(self, data, name, sensor_type, is_production, update_call):
"""Initialize the sensor."""
"""Initialize the Sense sensor."""
name_type = PRODUCTION_NAME if is_production else CONSUMPTION_NAME
self._name = "%s %s" % (name, name_type)
self._name = "{} {}".format(name, name_type)
self._data = data
self._sensor_type = sensor_type
self.update_sensor = update_call
@ -132,6 +134,6 @@ class Sense(Entity):
else:
self._state = round(self._data.active_power)
else:
state = self._data.get_trend(self._sensor_type,
self._is_production)
state = self._data.get_trend(
self._sensor_type, self._is_production)
self._state = round(state, 1)