optional displaying the sensors location on the map (#12375)

* Changed sensor attributes for GPS

the sensor is now using the correct attributes (latitued and
longitued) which enables them to show up on the map.

* added option to display the sensor on the map

the configuration will be extended by the optional 'show_on_map' flag.
Default is not display the sensor on the map.

* making the change non-breaking: old default behaviour

* removed doubled attributes for lat and lon
This commit is contained in:
Christoph Gerneth 2018-03-06 22:53:51 +01:00 committed by Fabian Affolter
parent 36b9c0a946
commit e6364b4ff6

View File

@ -12,7 +12,8 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS, CONF_NAME, TEMP_CELSIUS)
ATTR_ATTRIBUTION, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_MONITORED_CONDITIONS,
CONF_NAME, CONF_SHOW_ON_MAP, TEMP_CELSIUS)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
@ -54,6 +55,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MONITORED_CONDITIONS):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_SHOW_ON_MAP, default=False): cv.boolean,
})
@ -63,6 +65,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
from luftdaten import Luftdaten
name = config.get(CONF_NAME)
show_on_map = config.get(CONF_SHOW_ON_MAP)
sensor_id = config.get(CONF_SENSORID)
session = async_get_clientsession(hass)
@ -79,7 +82,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if luftdaten.data.values[variable] is None:
_LOGGER.warning("It might be that sensor %s is not providing "
"measurements for %s", sensor_id, variable)
devices.append(LuftdatenSensor(luftdaten, name, variable, sensor_id))
devices.append(
LuftdatenSensor(luftdaten, name, variable, sensor_id, show_on_map))
async_add_devices(devices)
@ -87,7 +91,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class LuftdatenSensor(Entity):
"""Implementation of a Luftdaten sensor."""
def __init__(self, luftdaten, name, sensor_type, sensor_id):
def __init__(self, luftdaten, name, sensor_type, sensor_id, show):
"""Initialize the Luftdaten sensor."""
self.luftdaten = luftdaten
self._name = name
@ -95,6 +99,7 @@ class LuftdatenSensor(Entity):
self._sensor_id = sensor_id
self.sensor_type = sensor_type
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._show_on_map = show
@property
def name(self):
@ -114,12 +119,15 @@ class LuftdatenSensor(Entity):
@property
def device_state_attributes(self):
"""Return the state attributes."""
onmap = ATTR_LATITUDE, ATTR_LONGITUDE
nomap = 'lat', 'long'
lat_format, lon_format = onmap if self._show_on_map else nomap
try:
attr = {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_SENSOR_ID: self._sensor_id,
'lat': self.luftdaten.data.meta['latitude'],
'long': self.luftdaten.data.meta['longitude'],
lat_format: self.luftdaten.data.meta['latitude'],
lon_format: self.luftdaten.data.meta['longitude'],
}
return attr
except KeyError: