Use string formatting (#18886)
* Use string formatting * Fix lint issue * Fix changepull/18917/head
commit
a62fc7ca04
|
@ -14,46 +14,48 @@ DEPENDENCIES = ['sense']
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
BIN_SENSOR_CLASS = 'power'
|
BIN_SENSOR_CLASS = 'power'
|
||||||
MDI_ICONS = {'ac': 'air-conditioner',
|
MDI_ICONS = {
|
||||||
'aquarium': 'fish',
|
'ac': 'air-conditioner',
|
||||||
'car': 'car-electric',
|
'aquarium': 'fish',
|
||||||
'computer': 'desktop-classic',
|
'car': 'car-electric',
|
||||||
'cup': 'coffee',
|
'computer': 'desktop-classic',
|
||||||
'dehumidifier': 'water-off',
|
'cup': 'coffee',
|
||||||
'dishes': 'dishwasher',
|
'dehumidifier': 'water-off',
|
||||||
'drill': 'toolbox',
|
'dishes': 'dishwasher',
|
||||||
'fan': 'fan',
|
'drill': 'toolbox',
|
||||||
'freezer': 'fridge-top',
|
'fan': 'fan',
|
||||||
'fridge': 'fridge-bottom',
|
'freezer': 'fridge-top',
|
||||||
'game': 'gamepad-variant',
|
'fridge': 'fridge-bottom',
|
||||||
'garage': 'garage',
|
'game': 'gamepad-variant',
|
||||||
'grill': 'stove',
|
'garage': 'garage',
|
||||||
'heat': 'fire',
|
'grill': 'stove',
|
||||||
'heater': 'radiatior',
|
'heat': 'fire',
|
||||||
'humidifier': 'water',
|
'heater': 'radiatior',
|
||||||
'kettle': 'kettle',
|
'humidifier': 'water',
|
||||||
'leafblower': 'leaf',
|
'kettle': 'kettle',
|
||||||
'lightbulb': 'lightbulb',
|
'leafblower': 'leaf',
|
||||||
'media_console': 'set-top-box',
|
'lightbulb': 'lightbulb',
|
||||||
'modem': 'router-wireless',
|
'media_console': 'set-top-box',
|
||||||
'outlet': 'power-socket-us',
|
'modem': 'router-wireless',
|
||||||
'papershredder': 'shredder',
|
'outlet': 'power-socket-us',
|
||||||
'printer': 'printer',
|
'papershredder': 'shredder',
|
||||||
'pump': 'water-pump',
|
'printer': 'printer',
|
||||||
'settings': 'settings',
|
'pump': 'water-pump',
|
||||||
'skillet': 'pot',
|
'settings': 'settings',
|
||||||
'smartcamera': 'webcam',
|
'skillet': 'pot',
|
||||||
'socket': 'power-plug',
|
'smartcamera': 'webcam',
|
||||||
'sound': 'speaker',
|
'socket': 'power-plug',
|
||||||
'stove': 'stove',
|
'sound': 'speaker',
|
||||||
'trash': 'trash-can',
|
'stove': 'stove',
|
||||||
'tv': 'television',
|
'trash': 'trash-can',
|
||||||
'vacuum': 'robot-vacuum',
|
'tv': 'television',
|
||||||
'washer': 'washing-machine'}
|
'vacuum': 'robot-vacuum',
|
||||||
|
'washer': 'washing-machine',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
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:
|
if discovery_info is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -67,14 +69,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
|
|
||||||
def sense_to_mdi(sense_icon):
|
def sense_to_mdi(sense_icon):
|
||||||
"""Convert sense icon to mdi 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):
|
class SenseDevice(BinarySensorDevice):
|
||||||
"""Implementation of a Sense energy device binary sensor."""
|
"""Implementation of a Sense energy device binary sensor."""
|
||||||
|
|
||||||
def __init__(self, data, device):
|
def __init__(self, data, device):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the Sense binary sensor."""
|
||||||
self._name = device['name']
|
self._name = device['name']
|
||||||
self._id = device['id']
|
self._id = device['id']
|
||||||
self._icon = sense_to_mdi(device['icon'])
|
self._icon = sense_to_mdi(device['icon'])
|
||||||
|
|
|
@ -8,20 +8,20 @@ import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
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
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.discovery import load_platform
|
||||||
|
|
||||||
REQUIREMENTS = ['sense_energy==0.5.1']
|
REQUIREMENTS = ['sense_energy==0.5.1']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SENSE_DATA = 'sense_data'
|
ACTIVE_UPDATE_RATE = 60
|
||||||
|
|
||||||
|
DEFAULT_TIMEOUT = 5
|
||||||
DOMAIN = 'sense'
|
DOMAIN = 'sense'
|
||||||
|
|
||||||
ACTIVE_UPDATE_RATE = 60
|
SENSE_DATA = 'sense_data'
|
||||||
DEFAULT_TIMEOUT = 5
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
|
@ -41,8 +41,8 @@ def setup(hass, config):
|
||||||
|
|
||||||
timeout = config[DOMAIN][CONF_TIMEOUT]
|
timeout = config[DOMAIN][CONF_TIMEOUT]
|
||||||
try:
|
try:
|
||||||
hass.data[SENSE_DATA] = Senseable(api_timeout=timeout,
|
hass.data[SENSE_DATA] = Senseable(
|
||||||
wss_timeout=timeout)
|
api_timeout=timeout, wss_timeout=timeout)
|
||||||
hass.data[SENSE_DATA].authenticate(username, password)
|
hass.data[SENSE_DATA].authenticate(username, password)
|
||||||
hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE
|
hass.data[SENSE_DATA].rate_limit = ACTIVE_UPDATE_RATE
|
||||||
except SenseAuthenticationException:
|
except SenseAuthenticationException:
|
||||||
|
|
|
@ -4,27 +4,31 @@ Support for monitoring a Sense energy sensor.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.sense/
|
https://home-assistant.io/components/sensor.sense/
|
||||||
"""
|
"""
|
||||||
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from datetime import timedelta
|
from homeassistant.components.sense import SENSE_DATA
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
from homeassistant.components.sense import SENSE_DATA
|
|
||||||
|
|
||||||
DEPENDENCIES = ['sense']
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ACTIVE_NAME = 'Energy'
|
ACTIVE_NAME = 'Energy'
|
||||||
PRODUCTION_NAME = 'Production'
|
ACTIVE_TYPE = 'active'
|
||||||
|
|
||||||
CONSUMPTION_NAME = 'Usage'
|
CONSUMPTION_NAME = 'Usage'
|
||||||
|
|
||||||
ACTIVE_TYPE = 'active'
|
DEPENDENCIES = ['sense']
|
||||||
|
|
||||||
|
ICON = 'mdi:flash'
|
||||||
|
|
||||||
|
MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=300)
|
||||||
|
|
||||||
|
PRODUCTION_NAME = 'Production'
|
||||||
|
|
||||||
|
|
||||||
class SensorConfig:
|
class SensorConfig:
|
||||||
"""Data structure holding sensor config."""
|
"""Data structure holding sensor configuration."""
|
||||||
|
|
||||||
def __init__(self, name, sensor_type):
|
def __init__(self, name, sensor_type):
|
||||||
"""Sensor name and type to pass to API."""
|
"""Sensor name and type to pass to API."""
|
||||||
|
@ -33,19 +37,17 @@ class SensorConfig:
|
||||||
|
|
||||||
|
|
||||||
# Sensor types/ranges
|
# Sensor types/ranges
|
||||||
SENSOR_TYPES = {'active': SensorConfig(ACTIVE_NAME, ACTIVE_TYPE),
|
SENSOR_TYPES = {
|
||||||
'daily': SensorConfig('Daily', 'DAY'),
|
'active': SensorConfig(ACTIVE_NAME, ACTIVE_TYPE),
|
||||||
'weekly': SensorConfig('Weekly', 'WEEK'),
|
'daily': SensorConfig('Daily', 'DAY'),
|
||||||
'monthly': SensorConfig('Monthly', 'MONTH'),
|
'weekly': SensorConfig('Weekly', 'WEEK'),
|
||||||
'yearly': SensorConfig('Yearly', 'YEAR')}
|
'monthly': SensorConfig('Monthly', 'MONTH'),
|
||||||
|
'yearly': SensorConfig('Yearly', 'YEAR'),
|
||||||
|
}
|
||||||
|
|
||||||
# Production/consumption variants
|
# Production/consumption variants
|
||||||
SENSOR_VARIANTS = [PRODUCTION_NAME.lower(), CONSUMPTION_NAME.lower()]
|
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):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Sense sensor."""
|
"""Set up the Sense sensor."""
|
||||||
|
@ -73,8 +75,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
update_call = update_active
|
update_call = update_active
|
||||||
else:
|
else:
|
||||||
update_call = update_trends
|
update_call = update_trends
|
||||||
devices.append(Sense(data, name, sensor_type,
|
devices.append(Sense(
|
||||||
is_production, update_call))
|
data, name, sensor_type, is_production, update_call))
|
||||||
|
|
||||||
add_entities(devices)
|
add_entities(devices)
|
||||||
|
|
||||||
|
@ -83,9 +85,9 @@ class Sense(Entity):
|
||||||
"""Implementation of a Sense energy sensor."""
|
"""Implementation of a Sense energy sensor."""
|
||||||
|
|
||||||
def __init__(self, data, name, sensor_type, is_production, update_call):
|
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
|
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._data = data
|
||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self.update_sensor = update_call
|
self.update_sensor = update_call
|
||||||
|
@ -132,6 +134,6 @@ class Sense(Entity):
|
||||||
else:
|
else:
|
||||||
self._state = round(self._data.active_power)
|
self._state = round(self._data.active_power)
|
||||||
else:
|
else:
|
||||||
state = self._data.get_trend(self._sensor_type,
|
state = self._data.get_trend(
|
||||||
self._is_production)
|
self._sensor_type, self._is_production)
|
||||||
self._state = round(state, 1)
|
self._state = round(state, 1)
|
||||||
|
|
Loading…
Reference in New Issue