Skip to content

Commit 39f8144

Browse files
Add Geolocation property (#10)
Co-authored-by: Rui Dias <rui.dias@outsystems.com>
1 parent 314e132 commit 39f8144

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

custom_components/precoscombustiveis/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
DEFAULT_ICON = "mdi:gas-station"
88
UNIT_OF_MEASUREMENT = "€"
99

10-
API_URI_TEMPLATE = "https://precoscombustiveis.dgeg.gov.pt/api/PrecoComb/GetDadosPostoMapa?id={}"
10+
API_URI_TEMPLATE = "https://precoscombustiveis.dgeg.gov.pt/api/PrecoComb/GetDadosPosto?id={}"
1111

1212
CONF_STATIONID = "stationId"

custom_components/precoscombustiveis/dgeg.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,26 @@ def address(self):
4545
]
4646
else:
4747
return None
48-
48+
49+
@property
50+
def latitude(self) -> float:
51+
return float(self._data["Morada"]["Latitude"])
52+
53+
@property
54+
def longitude(self) -> float:
55+
return float(self._data["Morada"]["Longitude"])
56+
4957
@property
5058
def fuels(self):
5159
return self._data["Combustiveis"]
5260

53-
@property
54-
def lastUpdate(self) -> datetime:
55-
return datetime.strptime(
56-
self._data["DataAtualizacao"],
57-
'%d-%m-%Y %H:%M')
61+
def getLastUpdate(self, fuelType) -> datetime:
62+
fuel = [f for f in self._data["Combustiveis"] if f["TipoCombustivel"] == fuelType][0]
63+
if (fuel):
64+
return datetime.strptime(
65+
fuel["DataAtualizacao"],
66+
'%Y-%m-%d %H:%M')
67+
return None
5868

5969
def getPrice(self, fuelType) -> float:
6070
fuel = [f for f in self._data["Combustiveis"] if f["TipoCombustivel"] == fuelType][0]
@@ -65,7 +75,6 @@ def getPrice(self, fuelType) -> float:
6575
else:
6676
return 0
6777

68-
6978
class DGEG:
7079
"""Interfaces to https://precoscombustiveis.dgeg.gov.pt/"""
7180

custom_components/precoscombustiveis/sensor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ def extra_state_attributes(self) -> Dict[str, Any]:
112112
"Brand": self._station.brand,
113113
"Name": self._station.name,
114114
"Address": self._station.address,
115+
"Latitude": self._station.latitude,
116+
"Longitude": self._station.longitude,
115117
"StationType": self._station.type,
116-
"LastPriceUpdate": self._station.lastUpdate,
117-
}
118+
"LastPriceUpdate": station.getLastUpdate(self._fuelName),
119+
}
118120

119121
async def async_update(self) -> None:
120122
"""Fetch new state data for the sensor."""

example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ async def main():
1212
print("Please type the service station ID from which you want to obtain the prices.")
1313
print("Go to https://precoscombustiveis.dgeg.gov.pt/api/PrecoComb/ListarDadosPostos,")
1414
print("search for the desired station and copy the `Id`.")
15-
stationId = input("Enter the Gas Station Id..: ")
15+
stationId = input("Enter the Gas Station Id..: ") or "65167"
1616

1717
station = await api.getStation(stationId)
1818
if (station):
1919
print ("Station Id.......:", station.id)
2020
print ("Station Name.....:", station.name)
2121
print ("Station Brand....:", station.brand)
2222
print ("Station Address..:", station.address)
23+
print ("GPS..............:", station.latitude, station.longitude)
2324
print ("Station Type.....:", station.type)
24-
print ("Last Update......:", station.lastUpdate)
2525
print (station.fuels)
26-
print ("Gasóleo simples..:", station.getPrice("Gasóleo simples"))
27-
print ("Gasóleo especial.:", station.getPrice("Gasóleo especial"))
26+
print ("Gasóleo simples..:", station.getPrice("Gasóleo simples"), "€", "(", station.getLastUpdate("Gasóleo simples"), ")")
27+
print ("Gasóleo especial.:", station.getPrice("Gasóleo especial"), "€", "(", station.getLastUpdate("Gasóleo especial"), ")")
2828
else:
2929
print ("Gas Station not found!")
3030

0 commit comments

Comments
 (0)