From 39f81444594debe3bb8cdf6c90f01480f9fb9efe Mon Sep 17 00:00:00 2001 From: Rui Dias Date: Mon, 16 Oct 2023 12:13:47 +0100 Subject: [PATCH] Add Geolocation property (#10) Co-authored-by: Rui Dias --- custom_components/precoscombustiveis/const.py | 2 +- custom_components/precoscombustiveis/dgeg.py | 23 +++++++++++++------ .../precoscombustiveis/sensor.py | 6 +++-- example.py | 8 +++---- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/custom_components/precoscombustiveis/const.py b/custom_components/precoscombustiveis/const.py index 3d70685..da23e58 100644 --- a/custom_components/precoscombustiveis/const.py +++ b/custom_components/precoscombustiveis/const.py @@ -7,6 +7,6 @@ DEFAULT_ICON = "mdi:gas-station" UNIT_OF_MEASUREMENT = "€" -API_URI_TEMPLATE = "https://precoscombustiveis.dgeg.gov.pt/api/PrecoComb/GetDadosPostoMapa?id={}" +API_URI_TEMPLATE = "https://precoscombustiveis.dgeg.gov.pt/api/PrecoComb/GetDadosPosto?id={}" CONF_STATIONID = "stationId" \ No newline at end of file diff --git a/custom_components/precoscombustiveis/dgeg.py b/custom_components/precoscombustiveis/dgeg.py index 64343c1..a55320a 100644 --- a/custom_components/precoscombustiveis/dgeg.py +++ b/custom_components/precoscombustiveis/dgeg.py @@ -45,16 +45,26 @@ def address(self): ] else: return None - + + @property + def latitude(self) -> float: + return float(self._data["Morada"]["Latitude"]) + + @property + def longitude(self) -> float: + return float(self._data["Morada"]["Longitude"]) + @property def fuels(self): return self._data["Combustiveis"] - @property - def lastUpdate(self) -> datetime: - return datetime.strptime( - self._data["DataAtualizacao"], - '%d-%m-%Y %H:%M') + def getLastUpdate(self, fuelType) -> datetime: + fuel = [f for f in self._data["Combustiveis"] if f["TipoCombustivel"] == fuelType][0] + if (fuel): + return datetime.strptime( + fuel["DataAtualizacao"], + '%Y-%m-%d %H:%M') + return None def getPrice(self, fuelType) -> float: fuel = [f for f in self._data["Combustiveis"] if f["TipoCombustivel"] == fuelType][0] @@ -65,7 +75,6 @@ def getPrice(self, fuelType) -> float: else: return 0 - class DGEG: """Interfaces to https://precoscombustiveis.dgeg.gov.pt/""" diff --git a/custom_components/precoscombustiveis/sensor.py b/custom_components/precoscombustiveis/sensor.py index b9e4e3d..682e7f9 100644 --- a/custom_components/precoscombustiveis/sensor.py +++ b/custom_components/precoscombustiveis/sensor.py @@ -112,9 +112,11 @@ def extra_state_attributes(self) -> Dict[str, Any]: "Brand": self._station.brand, "Name": self._station.name, "Address": self._station.address, + "Latitude": self._station.latitude, + "Longitude": self._station.longitude, "StationType": self._station.type, - "LastPriceUpdate": self._station.lastUpdate, - } + "LastPriceUpdate": station.getLastUpdate(self._fuelName), + } async def async_update(self) -> None: """Fetch new state data for the sensor.""" diff --git a/example.py b/example.py index c72e62d..5b9b443 100644 --- a/example.py +++ b/example.py @@ -12,7 +12,7 @@ async def main(): print("Please type the service station ID from which you want to obtain the prices.") print("Go to https://precoscombustiveis.dgeg.gov.pt/api/PrecoComb/ListarDadosPostos,") print("search for the desired station and copy the `Id`.") - stationId = input("Enter the Gas Station Id..: ") + stationId = input("Enter the Gas Station Id..: ") or "65167" station = await api.getStation(stationId) if (station): @@ -20,11 +20,11 @@ async def main(): print ("Station Name.....:", station.name) print ("Station Brand....:", station.brand) print ("Station Address..:", station.address) + print ("GPS..............:", station.latitude, station.longitude) print ("Station Type.....:", station.type) - print ("Last Update......:", station.lastUpdate) print (station.fuels) - print ("Gasóleo simples..:", station.getPrice("Gasóleo simples")) - print ("Gasóleo especial.:", station.getPrice("Gasóleo especial")) + print ("Gasóleo simples..:", station.getPrice("Gasóleo simples"), "€", "(", station.getLastUpdate("Gasóleo simples"), ")") + print ("Gasóleo especial.:", station.getPrice("Gasóleo especial"), "€", "(", station.getLastUpdate("Gasóleo especial"), ")") else: print ("Gas Station not found!")