Add unique id and show on map option in Tankerkoenig (#33400)

* Add unique id to set friendly name by UI

* Add ShowOnMap flag to show each/hide entity on map

* Update homeassistant/components/tankerkoenig/sensor.py

Co-Authored-By: guillempages <guillempages@users.noreply.github.com>

* Update homeassistant/components/tankerkoenig/sensor.py

Co-Authored-By: guillempages <guillempages@users.noreply.github.com>

* Update homeassistant/components/tankerkoenig/sensor.py

Co-Authored-By: guillempages <guillempages@users.noreply.github.com>

* Update homeassistant/components/tankerkoenig/sensor.py

Co-Authored-By: guillempages <guillempages@users.noreply.github.com>

* Update homeassistant/components/tankerkoenig/sensor.py

* Update homeassistant/components/tankerkoenig/__init__.py

* Update homeassistant/components/tankerkoenig

* Update homeassistant/components/tankerkoenig

* Update homeassistant/components/tankerkoenig/sensor.py

* Update homeassistant/components/tankerkoenig/sensor.py

Co-authored-by: guillempages <guillempages@users.noreply.github.com>
pull/33817/head
FlavorFx 2020-04-03 15:14:42 +02:00 committed by GitHub
parent 62835f0536
commit b7dd8cdf50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -13,6 +13,7 @@ from homeassistant.const import (
CONF_LONGITUDE,
CONF_RADIUS,
CONF_SCAN_INTERVAL,
CONF_SHOW_ON_MAP,
)
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
@ -52,6 +53,7 @@ CONFIG_SCHEMA = vol.Schema(
vol.Optional(CONF_STATIONS, default=[]): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(CONF_SHOW_ON_MAP, default=True): cv.boolean,
}
)
},
@ -106,6 +108,7 @@ class TankerkoenigData:
self.stations = {}
self.fuel_types = conf[CONF_FUEL_TYPES]
self.update_interval = conf[CONF_SCAN_INTERVAL]
self.show_on_map = conf[CONF_SHOW_ON_MAP]
self._hass = hass
def setup(self, latitude, longitude, radius, additional_stations):

View File

@ -59,7 +59,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
)
continue
sensor = FuelPriceSensor(
fuel, station, coordinator, f"{NAME}_{station['name']}_{fuel}"
fuel,
station,
coordinator,
f"{NAME}_{station['name']}_{fuel}",
tankerkoenig.show_on_map,
)
entities.append(sensor)
_LOGGER.debug("Added sensors %s", entities)
@ -70,7 +74,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class FuelPriceSensor(Entity):
"""Contains prices for fuel in a given station."""
def __init__(self, fuel_type, station, coordinator, name):
def __init__(self, fuel_type, station, coordinator, name, show_on_map):
"""Initialize the sensor."""
self._station = station
self._station_id = station["id"]
@ -84,6 +88,7 @@ class FuelPriceSensor(Entity):
self._postcode = station["postCode"]
self._street = station["street"]
self._price = station[fuel_type]
self._show_on_map = show_on_map
@property
def name(self):
@ -111,6 +116,11 @@ class FuelPriceSensor(Entity):
# key Fuel_type is not available when the fuel station is closed, use "get" instead of "[]" to avoid exceptions
return self._coordinator.data[self._station_id].get(self._fuel_type)
@property
def unique_id(self) -> str:
"""Return a unique identifier for this entity."""
return f"{self._station_id}_{self._fuel_type}"
@property
def device_state_attributes(self):
"""Return the attributes of the device."""
@ -125,9 +135,12 @@ class FuelPriceSensor(Entity):
ATTR_HOUSE_NUMBER: self._house_number,
ATTR_POSTCODE: self._postcode,
ATTR_CITY: self._city,
ATTR_LATITUDE: self._latitude,
ATTR_LONGITUDE: self._longitude,
}
if self._show_on_map:
attrs[ATTR_LATITUDE] = self._latitude
attrs[ATTR_LONGITUDE] = self._longitude
if data is not None and "status" in data:
attrs[ATTR_IS_OPEN] = data["status"] == "open"
return attrs