2019-04-03 15:40:03 +00:00
|
|
|
"""Support for the PrezziBenzina.it service."""
|
2018-12-18 14:47:38 +00:00
|
|
|
import datetime as dt
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2019-12-02 23:54:17 +00:00
|
|
|
from prezzibenzina import PrezziBenzinaPy
|
2018-12-18 14:47:38 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_TIME, CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FUEL = "fuel"
|
|
|
|
ATTR_SERVICE = "service"
|
|
|
|
ATTRIBUTION = "Data provided by PrezziBenzina.it"
|
2018-12-18 14:47:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_STATION = "station"
|
|
|
|
CONF_TYPES = "fuel_types"
|
2018-12-18 14:47:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:fuel"
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
FUEL_TYPES = [
|
2019-07-31 19:25:30 +00:00
|
|
|
"Benzina",
|
2018-12-18 14:47:38 +00:00
|
|
|
"Benzina speciale",
|
2019-07-31 19:25:30 +00:00
|
|
|
"Diesel",
|
2018-12-18 14:47:38 +00:00
|
|
|
"Diesel speciale",
|
2019-07-31 19:25:30 +00:00
|
|
|
"GPL",
|
|
|
|
"Metano",
|
2018-12-18 14:47:38 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=120)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_STATION): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, None): cv.string,
|
|
|
|
vol.Optional(CONF_TYPES, None): vol.All(cv.ensure_list, [vol.In(FUEL_TYPES)]),
|
|
|
|
}
|
|
|
|
)
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
|
2020-05-20 22:09:00 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-12-18 14:47:38 +00:00
|
|
|
"""Set up the PrezziBenzina sensor platform."""
|
|
|
|
|
|
|
|
station = config[CONF_STATION]
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
types = config.get(CONF_TYPES)
|
|
|
|
|
|
|
|
client = PrezziBenzinaPy()
|
|
|
|
dev = []
|
|
|
|
info = client.get_by_id(station)
|
|
|
|
|
|
|
|
if name is None:
|
|
|
|
name = client.get_station_name(station)
|
|
|
|
|
|
|
|
for index, info in enumerate(info):
|
2019-07-31 19:25:30 +00:00
|
|
|
if types is not None and info["fuel"] not in types:
|
2018-12-18 14:47:38 +00:00
|
|
|
continue
|
2019-07-31 19:25:30 +00:00
|
|
|
dev.append(
|
|
|
|
PrezziBenzinaSensor(
|
|
|
|
index, client, station, name, info["fuel"], info["service"]
|
|
|
|
)
|
|
|
|
)
|
2018-12-18 14:47:38 +00:00
|
|
|
|
2020-05-20 22:09:00 +00:00
|
|
|
add_entities(dev, True)
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PrezziBenzinaSensor(Entity):
|
|
|
|
"""Implementation of a PrezziBenzina sensor."""
|
|
|
|
|
2019-01-11 17:51:41 +00:00
|
|
|
def __init__(self, index, client, station, name, ft, srv):
|
2018-12-18 14:47:38 +00:00
|
|
|
"""Initialize the PrezziBenzina sensor."""
|
|
|
|
self._client = client
|
|
|
|
self._index = index
|
|
|
|
self._data = None
|
|
|
|
self._station = station
|
2019-09-03 18:35:00 +00:00
|
|
|
self._name = f"{name} {ft} {srv}"
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return ICON
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data["price"].replace(" €", "")
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data["price"].split(" ")[1]
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes of the last update."""
|
|
|
|
timestamp = dt.datetime.strptime(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._data["date"], "%d/%m/%Y %H:%M"
|
|
|
|
).isoformat()
|
2018-12-18 14:47:38 +00:00
|
|
|
|
|
|
|
attrs = {
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FUEL: self._data["fuel"],
|
|
|
|
ATTR_SERVICE: self._data["service"],
|
2018-12-18 14:47:38 +00:00
|
|
|
ATTR_TIME: timestamp,
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
|
2020-05-20 22:09:00 +00:00
|
|
|
def update(self):
|
2018-12-18 14:47:38 +00:00
|
|
|
"""Get the latest data and updates the states."""
|
|
|
|
self._data = self._client.get_by_id(self._station)[self._index]
|