Skip to content

Commit

Permalink
Add Geolocation property (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui Dias <rui.dias@outsystems.com>
  • Loading branch information
netsoft-ruidias and rud2020 authored Oct 16, 2023
1 parent 314e132 commit 39f8144
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion custom_components/precoscombustiveis/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 16 additions & 7 deletions custom_components/precoscombustiveis/dgeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -65,7 +75,6 @@ def getPrice(self, fuelType) -> float:
else:
return 0


class DGEG:
"""Interfaces to https://precoscombustiveis.dgeg.gov.pt/"""

Expand Down
6 changes: 4 additions & 2 deletions custom_components/precoscombustiveis/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
8 changes: 4 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ 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):
print ("Station Id.......:", station.id)
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!")

Expand Down

0 comments on commit 39f8144

Please sign in to comment.