Skip to content

Commit caa957f

Browse files
⬆ Support v5 of the RTE API
We try v5 first and fallback to v4 if we receive a 403. There is apparently no way to know if an api key has been generated for v5 or v4 but they are incompatible. Only way to know is to test. Fix #68 Fix #67
1 parent bb3da73 commit caa957f

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Use [hacs](https://hacs.xyz/).
1414
### Get api access for RTE APIs
1515

1616
- Create an account on [RTE API website](https://data.rte-france.com/web/guest)
17-
- Register to the [Ecowatt API](https://data.rte-france.com/catalog/-/api/consumption/Ecowatt/v4.0) and click on "Abonnez-vous à l'API", create a new application
17+
- Register to the [Ecowatt API](https://data.rte-france.com/catalog/-/api/consumption/Ecowatt/v5.0) and click on "Abonnez-vous à l'API", create a new application
1818
- get the `client_id` and `client_secret` (uuid in both cases)
1919

2020
### Configure home-assistant

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Utilisez [hacs](https://hacs.xyz/).
1515
### Obtenir un accès API pour les API RTE
1616

1717
- Créer un compte sur [site API RTE](https://data.rte-france.com/web/guest)
18-
- Inscrivez-vous à l'[API Ecowatt](https://data.rte-france.com/catalog/-/api/consumption/Ecowatt/v4.0) et cliquez sur "Abonnez-vous à l'API", créez un nouvelle application
18+
- Inscrivez-vous à l'[API Ecowatt](https://data.rte-france.com/catalog/-/api/consumption/Ecowatt/v5.0) et cliquez sur "Abonnez-vous à l'API", créez un nouvelle application
1919
- obtenir le `client_id` et `client_secret` (uuid dans les deux cas)
2020

2121
### Configurer home-assistant

custom_components/rte_ecowatt/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def __init__(self, hass, config: ConfigType):
122122
self.config = config
123123
self.hass = hass
124124
self.oauth_client = AsyncOauthClient(config)
125+
self.api_version = "v5"
125126

126127
async def async_oauth_client(self):
127128
client = await self.oauth_client.client()
@@ -176,11 +177,23 @@ async def update_method(self):
176177
headers = {
177178
"Authorization": f"{self.token['token_type']} {self.token['access_token']}"
178179
}
179-
url = f"{BASE_URL}/open_api/ecowatt/v4/signals"
180+
url = f"{BASE_URL}/open_api/ecowatt/{self.api_version}/signals"
180181
if "ECOWATT_DEBUG" in os.environ:
181-
url = f"{BASE_URL}/open_api/ecowatt/v4/sandbox/signals"
182+
url = f"{BASE_URL}/open_api/ecowatt/{self.api_version}/sandbox/signals"
182183
api_result = await client.get(url, headers=headers)
183184
_LOGGER.info(f"data received, status code: {api_result.status}")
185+
if api_result.status == 403:
186+
# a code 403 is likely to be an api key from a previous version of the api
187+
if self.api_version == "v5":
188+
_LOGGER.warn(
189+
f"Received a 403, api key is likely from a previous version, downgrading to api v4. You can regenerate a new api key to avoid this warning"
190+
)
191+
self.api_version = "v4"
192+
return await self.update_method()
193+
else:
194+
raise UpdateFailed(
195+
f"Error communicating with RTE API: received a 403 from api, even with version {self.api_version}"
196+
)
184197
if api_result.status == 429:
185198
# a code 429 is expected when requesting more often than every 15minutes and not using the sandbox url
186199
# FIXME(kamaradclimber): avoid this error when home assistant is restarting by storing state and last update
@@ -225,7 +238,9 @@ async def async_added_to_hass(self):
225238
):
226239
_LOGGER.debug(f"Restoring state for {self.unique_id}")
227240
self._attr_native_value = extra_stored_data.native_value
228-
self._attr_native_unit_of_measurement = extra_stored_data.native_unit_of_measurement
241+
self._attr_native_unit_of_measurement = (
242+
extra_stored_data.native_unit_of_measurement
243+
)
229244
# sadly it seems as of 2023.6 we don't have any way to get back the additional attributes
230245
self.coordinator.last_update_success = True
231246
else:

0 commit comments

Comments
 (0)