Skip to content

Commit

Permalink
bugfixing for fuel price on route in betwen countries
Browse files Browse the repository at this point in the history
  • Loading branch information
myTselection committed Aug 2, 2023
1 parent 97cdcc0 commit 0127f58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion custom_components/carbu_com/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/myTselection/carbu_com/issues",
"requirements": ["bs4","requests","ratelimit"],
"version": "6.0.0"
"version": "7.0.1"
}
2 changes: 0 additions & 2 deletions custom_components/carbu_com/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ async def _forced_update(self):
await self.get_fuel_price_info(FuelType.SUPER95)
if self._country.lower() in ['be','fr','lu']:
await self.get_fuel_price_prediction_info(FuelType.SUPER95_Prediction)
else:
_LOGGER.debug(f"{NAME} not getting fuel price_info {self._super95} FueltType.SUPER95.name_lowercase {FuelType.SUPER95.name_lowercase}")

if self._super98:
await self.get_fuel_price_info(FuelType.SUPER98)
Expand Down
33 changes: 18 additions & 15 deletions custom_components/carbu_com/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self):
# Country = country code: BE/FR/LU/DE/IT

@sleep_and_retry
@limits(calls=1, period=1)
@limits(calls=1, period=5)
def convertPostalCode(self, postalcode, country, town = ''):
_LOGGER.debug(f"convertPostalCode: postalcode: {postalcode}, country: {country}, town: {town}")
header = {"Content-Type": "application/x-www-form-urlencoded"}
Expand Down Expand Up @@ -128,9 +128,10 @@ def convertLocationBoundingBox(self, postalcode, country, town):
country_name = "Italy"
if country.lower() == 'nl':
country_name = "Netherlands"
orig_boundingbox = self.searchGeocodeOSM(postalcode, town, country_name).get('boundingbox')
if len(orig_boundingbox) < 3:
orig_location = self.searchGeocodeOSM(postalcode, town, country_name)
if orig_location is None:
return []
orig_boundingbox = orig_location.get('boundingbox')
boundingboxes = [orig_boundingbox, [float(orig_boundingbox[0])-0.045, float(orig_boundingbox[1])+0.045, float(orig_boundingbox[2])-0.045, float(orig_boundingbox[3])+0.045], [float(orig_boundingbox[0])-0.09, float(orig_boundingbox[1])+0.09, float(orig_boundingbox[2])-0.09, float(orig_boundingbox[3])+0.09]]
return boundingboxes

Expand Down Expand Up @@ -571,7 +572,7 @@ def getOilPrediction(self):


@sleep_and_retry
@limits(calls=1, period=1)
@limits(calls=1, period=5)
def getStationInfo(self, postalcode, country, fuel_type: FuelType, town="", max_distance=0, filter=""):
town = None
locationinfo = None
Expand All @@ -585,7 +586,7 @@ def getStationInfo(self, postalcode, country, fuel_type: FuelType, town="", max_
countryname = carbuLocationInfo.get("cn")
locationinfo = carbuLocationInfo.get("id")
_LOGGER.debug(f"convertPostalCode postalcode: {postalcode}, town: {town}, city: {city}, countryname: {countryname}, locationinfo: {locationinfo}")
if country.lower() in ["it"]:
if country.lower() in ["it","nl"]:
itLocationInfo = self.convertLocationBoundingBox(postalcode, country, town)
locationinfo = itLocationInfo

Expand Down Expand Up @@ -789,7 +790,9 @@ def getPriceOnRouteLatLon(self, fuel_type: FuelType, from_latitude, from_longitu
postal_code_country = self.reverseGeocodeOSM((route[i]['maneuver']['location'][0], route[i]['maneuver']['location'][1]))
if postal_code_country[0] is not None and postal_code_country[0] not in processedPostalCodes:
_LOGGER.debug(f"Get route postalcode {postal_code_country[0]}, processedPostalCodes {processedPostalCodes}")
bestAroundPostalCode = self.getStationInfo(postal_code_country[0], postal_code_country[1], fuel_type, '', 3, filter)
bestAroundPostalCode = self.getStationInfo(postal_code_country[0], postal_code_country[1], fuel_type, postal_code_country[2], 3, filter)
if bestAroundPostalCode is None:
continue
processedPostalCodes.extend(bestAroundPostalCode.get('postalcodes'))
if (bestPriceOnRoute is None) or (bestAroundPostalCode.get('price') is not None and bestAroundPostalCode.get('price',999) < bestPriceOnRoute):
bestStationOnRoute = bestAroundPostalCode
Expand Down Expand Up @@ -883,17 +886,17 @@ def searchGeocodeOSM(self, postalcode, city, country):
nominatim_response = requests.get(nominatim_url)
nominatim_data = nominatim_response.json()
_LOGGER.debug(f"nominatim_data {nominatim_data}")
boundingbox = []
location = []
if len(nominatim_data) > 0:
location = nominatim_data[0]
lat = location.get('lat')
lon = location.get('lon')
boundingbox = location.get('boundingbox')
min_lat = boundingbox[0]
max_lat = boundingbox[1]
min_lon = boundingbox[2]
max_lon = boundingbox[3]
return location
# lat = location.get('lat')
# lon = location.get('lon')
# boundingbox = location.get('boundingbox')
# min_lat = boundingbox[0]
# max_lat = boundingbox[1]
# min_lon = boundingbox[2]
# max_lon = boundingbox[3]
return location

@sleep_and_retry
@limits(calls=1, period=2)
Expand Down

0 comments on commit 0127f58

Please sign in to comment.