From 0127f5880e42db8acaa0979ca8f621e2a1b442e9 Mon Sep 17 00:00:00 2001 From: myTselection Date: Wed, 2 Aug 2023 23:53:02 +0200 Subject: [PATCH] bugfixing for fuel price on route in betwen countries --- custom_components/carbu_com/manifest.json | 2 +- custom_components/carbu_com/sensor.py | 2 -- custom_components/carbu_com/utils.py | 33 ++++++++++++----------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/custom_components/carbu_com/manifest.json b/custom_components/carbu_com/manifest.json index a788535..8b44c20 100644 --- a/custom_components/carbu_com/manifest.json +++ b/custom_components/carbu_com/manifest.json @@ -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" } diff --git a/custom_components/carbu_com/sensor.py b/custom_components/carbu_com/sensor.py index 74c3a05..32b1458 100644 --- a/custom_components/carbu_com/sensor.py +++ b/custom_components/carbu_com/sensor.py @@ -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) diff --git a/custom_components/carbu_com/utils.py b/custom_components/carbu_com/utils.py index 38cb9d5..f6cf062 100644 --- a/custom_components/carbu_com/utils.py +++ b/custom_components/carbu_com/utils.py @@ -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"} @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)