Move 'show_on_map' to const (#9727)

This commit is contained in:
Fabian Affolter 2017-10-07 15:11:41 +02:00 committed by GitHub
parent bd5b70c3cd
commit 19a20b3b13
3 changed files with 52 additions and 101 deletions

View file

@ -13,7 +13,8 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDevice, PLATFORM_SCHEMA) BinarySensorDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, ATTR_LONGITUDE, ATTR_LATITUDE) from homeassistant.const import (
CONF_NAME, ATTR_LONGITUDE, ATTR_LATITUDE, CONF_SHOW_ON_MAP)
from homeassistant.util import Throttle from homeassistant.util import Throttle
REQUIREMENTS = ['pyiss==1.0.1'] REQUIREMENTS = ['pyiss==1.0.1']
@ -23,8 +24,6 @@ _LOGGER = logging.getLogger(__name__)
ATTR_ISS_NEXT_RISE = 'next_rise' ATTR_ISS_NEXT_RISE = 'next_rise'
ATTR_ISS_NUMBER_PEOPLE_SPACE = 'number_of_people_in_space' ATTR_ISS_NUMBER_PEOPLE_SPACE = 'number_of_people_in_space'
CONF_SHOW_ON_MAP = 'show_on_map'
DEFAULT_NAME = 'ISS' DEFAULT_NAME = 'ISS'
DEFAULT_DEVICE_CLASS = 'visible' DEFAULT_DEVICE_CLASS = 'visible'

View file

@ -4,7 +4,6 @@ Support for AirVisual air quality sensors.
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.airvisual/ https://home-assistant.io/components/sensor.airvisual/
""" """
import asyncio import asyncio
from logging import getLogger from logging import getLogger
from datetime import timedelta from datetime import timedelta
@ -15,13 +14,15 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ( from homeassistant.const import (
ATTR_ATTRIBUTION, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_API_KEY, ATTR_ATTRIBUTION, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_API_KEY,
CONF_LATITUDE, CONF_LONGITUDE, CONF_MONITORED_CONDITIONS, CONF_STATE) CONF_LATITUDE, CONF_LONGITUDE, CONF_MONITORED_CONDITIONS, CONF_STATE,
CONF_SHOW_ON_MAP)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle from homeassistant.util import Throttle
_LOGGER = getLogger(__name__)
REQUIREMENTS = ['pyairvisual==1.0.0'] REQUIREMENTS = ['pyairvisual==1.0.0']
_LOGGER = getLogger(__name__)
ATTR_CITY = 'city' ATTR_CITY = 'city'
ATTR_COUNTRY = 'country' ATTR_COUNTRY = 'country'
ATTR_POLLUTANT_SYMBOL = 'pollutant_symbol' ATTR_POLLUTANT_SYMBOL = 'pollutant_symbol'
@ -32,7 +33,7 @@ ATTR_TIMESTAMP = 'timestamp'
CONF_CITY = 'city' CONF_CITY = 'city'
CONF_COUNTRY = 'country' CONF_COUNTRY = 'country'
CONF_RADIUS = 'radius' CONF_RADIUS = 'radius'
CONF_SHOW_ON_MAP = 'show_on_map' CONF_ATTRIBUTION = "Data provided by AirVisual"
MASS_PARTS_PER_MILLION = 'ppm' MASS_PARTS_PER_MILLION = 'ppm'
MASS_PARTS_PER_BILLION = 'ppb' MASS_PARTS_PER_BILLION = 'ppb'
@ -40,56 +41,22 @@ VOLUME_MICROGRAMS_PER_CUBIC_METER = 'µg/m3'
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=10) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=10)
POLLUTANT_LEVEL_MAPPING = [{ POLLUTANT_LEVEL_MAPPING = [
'label': 'Good', {'label': 'Good', 'minimum': 0, 'maximum': 50},
'minimum': 0, {'label': 'Moderate', 'minimum': 51, 'maximum': 100},
'maximum': 50 {'label': 'Unhealthy for sensitive group', 'minimum': 101, 'maximum': 150},
}, { {'label': 'Unhealthy', 'minimum': 151, 'maximum': 200},
'label': 'Moderate', {'label': 'Very Unhealthy', 'minimum': 201, 'maximum': 300},
'minimum': 51, {'label': 'Hazardous', 'minimum': 301, 'maximum': 10000}
'maximum': 100 ]
}, {
'label': 'Unhealthy for Sensitive Groups',
'minimum': 101,
'maximum': 150
}, {
'label': 'Unhealthy',
'minimum': 151,
'maximum': 200
}, {
'label': 'Very Unhealthy',
'minimum': 201,
'maximum': 300
}, {
'label': 'Hazardous',
'minimum': 301,
'maximum': 10000
}]
POLLUTANT_MAPPING = { POLLUTANT_MAPPING = {
'co': { 'co': {'label': 'Carbon Monoxide', 'unit': MASS_PARTS_PER_MILLION},
'label': 'Carbon Monoxide', 'n2': {'label': 'Nitrogen Dioxide', 'unit': MASS_PARTS_PER_BILLION},
'unit': MASS_PARTS_PER_MILLION 'o3': {'label': 'Ozone', 'unit': MASS_PARTS_PER_BILLION},
}, 'p1': {'label': 'PM10', 'unit': VOLUME_MICROGRAMS_PER_CUBIC_METER},
'n2': { 'p2': {'label': 'PM2.5', 'unit': VOLUME_MICROGRAMS_PER_CUBIC_METER},
'label': 'Nitrogen Dioxide', 's2': {'label': 'Sulfur Dioxide', 'unit': MASS_PARTS_PER_BILLION},
'unit': MASS_PARTS_PER_BILLION
},
'o3': {
'label': 'Ozone',
'unit': MASS_PARTS_PER_BILLION
},
'p1': {
'label': 'PM10',
'unit': VOLUME_MICROGRAMS_PER_CUBIC_METER
},
'p2': {
'label': 'PM2.5',
'unit': VOLUME_MICROGRAMS_PER_CUBIC_METER
},
's2': {
'label': 'Sulfur Dioxide',
'unit': MASS_PARTS_PER_BILLION
}
} }
SENSOR_LOCALES = {'cn': 'Chinese', 'us': 'U.S.'} SENSOR_LOCALES = {'cn': 'Chinese', 'us': 'U.S.'}
@ -100,24 +67,16 @@ SENSOR_TYPES = [
] ]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): vol.Required(CONF_API_KEY): cv.string,
cv.string,
vol.Required(CONF_MONITORED_CONDITIONS): vol.Required(CONF_MONITORED_CONDITIONS):
vol.All(cv.ensure_list, [vol.In(SENSOR_LOCALES)]), vol.All(cv.ensure_list, [vol.In(SENSOR_LOCALES)]),
vol.Optional(CONF_LATITUDE): vol.Optional(CONF_CITY): cv.string,
cv.latitude, vol.Optional(CONF_COUNTRY): cv.string,
vol.Optional(CONF_LONGITUDE): vol.Optional(CONF_LATITUDE): cv.latitude,
cv.longitude, vol.Optional(CONF_LONGITUDE): cv.longitude,
vol.Optional(CONF_RADIUS, default=1000): vol.Optional(CONF_RADIUS, default=1000): cv.positive_int,
cv.positive_int, vol.Optional(CONF_SHOW_ON_MAP, default=True): cv.boolean,
vol.Optional(CONF_CITY): vol.Optional(CONF_STATE): cv.string,
cv.string,
vol.Optional(CONF_STATE):
cv.string,
vol.Optional(CONF_COUNTRY):
cv.string,
vol.Optional(CONF_SHOW_ON_MAP, default=True):
cv.boolean
}) })
@ -126,8 +85,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Configure the platform and add the sensors.""" """Configure the platform and add the sensors."""
import pyairvisual as pav import pyairvisual as pav
_LOGGER.debug('Received configuration: %s', config)
api_key = config.get(CONF_API_KEY) api_key = config.get(CONF_API_KEY)
monitored_locales = config.get(CONF_MONITORED_CONDITIONS) monitored_locales = config.get(CONF_MONITORED_CONDITIONS)
latitude = config.get(CONF_LATITUDE, hass.config.latitude) latitude = config.get(CONF_LATITUDE, hass.config.latitude)
@ -139,23 +96,17 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
show_on_map = config.get(CONF_SHOW_ON_MAP) show_on_map = config.get(CONF_SHOW_ON_MAP)
if city and state and country: if city and state and country:
_LOGGER.debug('Using city, state, and country: %s, %s, %s', city, _LOGGER.debug(
state, country) "Using city, state, and country: %s, %s, %s", city, state, country)
data = AirVisualData( data = AirVisualData(
pav.Client(api_key), pav.Client(api_key), city=city, state=state, country=country,
city=city,
state=state,
country=country,
show_on_map=show_on_map) show_on_map=show_on_map)
else: else:
_LOGGER.debug('Using latitude and longitude: %s, %s', latitude, _LOGGER.debug(
longitude) "Using latitude and longitude: %s, %s", latitude, longitude)
data = AirVisualData( data = AirVisualData(
pav.Client(api_key), pav.Client(api_key), latitude=latitude, longitude=longitude,
latitude=latitude, radius=radius, show_on_map=show_on_map)
longitude=longitude,
radius=radius,
show_on_map=show_on_map)
sensors = [] sensors = []
for locale in monitored_locales: for locale in monitored_locales:
@ -176,7 +127,7 @@ class AirVisualBaseSensor(Entity):
"""Define a base class for all of our sensors.""" """Define a base class for all of our sensors."""
def __init__(self, data, name, icon, locale): def __init__(self, data, name, icon, locale):
"""Initialize.""" """Initialize the sensor."""
self._data = data self._data = data
self._icon = icon self._icon = icon
self._locale = locale self._locale = locale
@ -186,9 +137,9 @@ class AirVisualBaseSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the device state attributes."""
attrs = { attrs = {
ATTR_ATTRIBUTION: '©AirVisual', ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_CITY: self._data.city, ATTR_CITY: self._data.city,
ATTR_COUNTRY: self._data.country, ATTR_COUNTRY: self._data.country,
ATTR_REGION: self._data.state, ATTR_REGION: self._data.state,
@ -222,7 +173,7 @@ class AirVisualBaseSensor(Entity):
@asyncio.coroutine @asyncio.coroutine
def async_update(self): def async_update(self):
"""Update the status of the sensor.""" """Update the status of the sensor."""
_LOGGER.debug('Updating sensor: %s', self._name) _LOGGER.debug("Updating sensor: %s", self._name)
self._data.update() self._data.update()
@ -267,14 +218,14 @@ class MainPollutantSensor(AirVisualBaseSensor):
"""Define a sensor to the main pollutant of an area.""" """Define a sensor to the main pollutant of an area."""
def __init__(self, data, name, icon, locale): def __init__(self, data, name, icon, locale):
"""Initialize.""" """Initialize the sensor."""
super().__init__(data, name, icon, locale) super().__init__(data, name, icon, locale)
self._symbol = None self._symbol = None
self._unit = None self._unit = None
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the device state attributes."""
return merge_two_dicts(super().device_state_attributes, { return merge_two_dicts(super().device_state_attributes, {
ATTR_POLLUTANT_SYMBOL: self._symbol, ATTR_POLLUTANT_SYMBOL: self._symbol,
ATTR_POLLUTANT_UNIT: self._unit ATTR_POLLUTANT_UNIT: self._unit
@ -295,7 +246,7 @@ class AirVisualData(object):
"""Define an object to hold sensor data.""" """Define an object to hold sensor data."""
def __init__(self, client, **kwargs): def __init__(self, client, **kwargs):
"""Initialize.""" """Initialize the AirVisual data element."""
self._client = client self._client = client
self.pollution_info = None self.pollution_info = None
@ -316,12 +267,12 @@ class AirVisualData(object):
try: try:
if self.city and self.state and self.country: if self.city and self.state and self.country:
resp = self._client.city(self.city, self.state, resp = self._client.city(
self.country).get('data') self.city, self.state, self.country).get('data')
else: else:
resp = self._client.nearest_city(self.latitude, self.longitude, resp = self._client.nearest_city(
self._radius).get('data') self.latitude, self.longitude, self._radius).get('data')
_LOGGER.debug('New data retrieved: %s', resp) _LOGGER.debug("New data retrieved: %s", resp)
self.city = resp.get('city') self.city = resp.get('city')
self.state = resp.get('state') self.state = resp.get('state')
@ -330,7 +281,7 @@ class AirVisualData(object):
'coordinates') 'coordinates')
self.pollution_info = resp.get('current', {}).get('pollution', {}) self.pollution_info = resp.get('current', {}).get('pollution', {})
except exceptions.HTTPError as exc_info: except exceptions.HTTPError as exc_info:
_LOGGER.error('Unable to retrieve data on this location: %s', _LOGGER.error("Unable to retrieve data on this location: %s",
self.__dict__) self.__dict__)
_LOGGER.debug(exc_info) _LOGGER.debug(exc_info)
self.pollution_info = {} self.pollution_info = {}

View file

@ -144,6 +144,7 @@ CONF_SCAN_INTERVAL = 'scan_interval'
CONF_SENDER = 'sender' CONF_SENDER = 'sender'
CONF_SENSOR_TYPE = 'sensor_type' CONF_SENSOR_TYPE = 'sensor_type'
CONF_SENSORS = 'sensors' CONF_SENSORS = 'sensors'
CONF_SHOW_ON_MAP = 'show_on_map'
CONF_SLAVE = 'slave' CONF_SLAVE = 'slave'
CONF_SSL = 'ssl' CONF_SSL = 'ssl'
CONF_STATE = 'state' CONF_STATE = 'state'