CoMPaTech / stromer

Stromer custom_component for Home Assistant
MIT License
13 stars 5 forks source link

[BUG]: Integration not working after upgrade #48

Closed simonwolf83 closed 11 months ago

simonwolf83 commented 11 months ago

Describe the bug.

Hi. I upgraded the integration from v0.2.3 to v0.2.5 and it stopped working. Showing an error in the integrations panel: "could not authenticate".

Kind regards Simon

What version of Home Assistant Core has the issue?

2023.11.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

How did you install the stromer integration

HACS

Logging

2023-11-05 22:00:43.881 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration stromer which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2023-11-05 22:01:03.667 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer attempt: 1/10 2023-11-05 22:01:04.537 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer bike: {'nickname': 'DeLorean', 'bikeid': xxxxxx, 'bikemodel': 'ST2', 'biketype': 'ST1-k', 'color': 'dark grey', 'size': 'sport M', 'hardware': 'omniinterfacec', 'connectivity': 'standard', 'has_crash_detection': True, 'maintenance_feature': {'display_maintenance_event': False, 'next_maintenance_km': 1000, 'next_maintenance_date': '20241024', 'next_maintenance_interval': 1000, 'customer_enabled_maintenance': True, 'last_maintenance_reset_km': None, 'last_maintenance_reset_date': None}} 2023-11-05 22:01:05.931 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer status: {'trip_distance': 118.5, 'suiversion': '4.4.1.5', 'average_speed_trip': 27.5, 'average_speed_total': 27.5, 'power_on_cycles': 13, 'tntversion': '4.6', 'atmospheric_pressure': 0, 'battery_SOC': 65, 'assistance_level': -40, 'bike_speed': 0.0, 'trip_time': 15483, 'battery_health': 99, 'theft_flag': False, 'motor_temp': 32.2, 'battery_temp': 13.9, 'rcvts': 1699218065, 'average_energy_consumption': 10, 'total_time': 15483, 'total_distance': 118.5, 'light_on': 0, 'total_energy_consumption': 1210, 'lock_flag': True} 2023-11-05 22:01:07.436 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer position: {'latitude': xx.xxxxxx, 'longitude': x.xxxxxx, 'altitude': xxx.x, 'speed': 0.159, 'timets': 1699218065, 'rcvts': 1699218066} 2023-11-05 22:01:07.437 ERROR (MainThread) [custom_components.stromer.stromer] Stromer error: api call failed 10 times, cowardly failing 2023-11-05 22:01:07.437 ERROR (MainThread) [custom_components.stromer.stromer] Stromer unable to update: 2023-11-05 22:01:07.437 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer connected! 2023-11-05 22:01:07.437 DEBUG (MainThread) [custom_components.stromer.const] Stromer entry: <homeassistant.config_entries.ConfigEntry object at 0x7f921231f0> 2023-11-05 22:01:07.438 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer attempt: 1/10 2023-11-05 22:01:09.347 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer bike: {'nickname': 'DeLorean', 'bikeid': xxxxxx, 'bikemodel': 'ST2', 'biketype': 'ST1-k', 'color': 'dark grey', 'size': 'sport M', 'hardware': 'omniinterfacec', 'connectivity': 'standard', 'has_crash_detection': True, 'maintenance_feature': {'display_maintenance_event': False, 'next_maintenance_km': 1000, 'next_maintenance_date': '20241024', 'next_maintenance_interval': 1000, 'customer_enabled_maintenance': True, 'last_maintenance_reset_km': None, 'last_maintenance_reset_date': None}} 2023-11-05 22:01:09.496 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer status: {'trip_distance': 118.5, 'suiversion': '4.4.1.5', 'average_speed_trip': 27.5, 'average_speed_total': 27.5, 'power_on_cycles': 13, 'tntversion': '4.6', 'atmospheric_pressure': 0, 'battery_SOC': 65, 'assistance_level': -40, 'bike_speed': 0.0, 'trip_time': 15483, 'battery_health': 99, 'theft_flag': False, 'motor_temp': 32.2, 'battery_temp': 13.9, 'rcvts': 1699218065, 'average_energy_consumption': 10, 'total_time': 15483, 'total_distance': 118.5, 'light_on': 0, 'total_energy_consumption': 1210, 'lock_flag': True} 2023-11-05 22:01:10.426 DEBUG (MainThread) [custom_components.stromer.stromer] Stromer position: {'latitude': xx.xxxxx, 'longitude': xx.xxxxx, 'altitude': xxx.xx, 'speed': 0.134, 'timets': 1699218069, 'rcvts': 1699218070} 2023-11-05 22:01:10.426 ERROR (MainThread) [custom_components.stromer.stromer] Stromer error: api call failed 10 times, cowardly failing 2023-11-05 22:01:10.426 DEBUG (MainThread) [custom_components.stromer.const] Finished fetching stromer data in 2.989 seconds (success: False) 2023-11-05 22:01:10.427 WARNING (MainThread) [homeassistant.config_entries] Config entry 'DeLorean' for stromer integration could not authenticate

Diagnostics information

No response

Additional information

No response

simonwolf83 commented 11 months ago

I played around a bit. (I am not a Python expert). If i change the the function in stromer.py from this:

async def stromer_update(self):
        attempts = 0
        while attempts < 10:
            if attempts == 5:
                LOGGER.info("Reconnecting to Stromer API")
                await self.stromer_connect()
            attempts += 1
            try:
                LOGGER.debug("Stromer attempt: {}/10".format(attempts))
                self.bike = await self.stromer_call_api(endpoint="bike/")
                LOGGER.debug("Stromer bike: {}".format(self.bike))

                self.bike_id = self.bike["bikeid"]
                self.bike_name = self.bike["nickname"]
                self.bike_model = self.bike["biketype"]

                endpoint = f"bike/{self.bike_id}/state/"
                data = {"cached": "false"}
                # LOGGER.debug("Stromer endpoint: {}".format(endpoint))
                self.status = await self.stromer_call_api(endpoint=endpoint, data=data)
                LOGGER.debug("Stromer status: {}".format(self.status))

                endpoint = f"bike/{self.bike_id}/position/"
                self.position = await self.stromer_call_api(endpoint=endpoint, data=data)
                LOGGER.debug("Stromer position: {}".format(self.position))
                break

            except Exception as e:
                LOGGER.error("Stromer error: api call failed: {}".format(e))
                LOGGER.debug("Stromer retry: {}/10".format(attempts))

        LOGGER.error("Stromer error: api call failed 10 times, cowardly failing")
        raise ApiError

to this:

async def stromer_update(self):
        attempts = 0
        while attempts < 10:
            if attempts == 5:
                LOGGER.info("Reconnecting to Stromer API")
                await self.stromer_connect()
            attempts += 1
            try:
                LOGGER.debug("Stromer attempt: {}/10".format(attempts))
                self.bike = await self.stromer_call_api(endpoint="bike/")
                LOGGER.debug("Stromer bike: {}".format(self.bike))

                self.bike_id = self.bike["bikeid"]
                self.bike_name = self.bike["nickname"]
                self.bike_model = self.bike["biketype"]

                endpoint = f"bike/{self.bike_id}/state/"
                data = {"cached": "false"}
                # LOGGER.debug("Stromer endpoint: {}".format(endpoint))
                self.status = await self.stromer_call_api(endpoint=endpoint, data=data)
                LOGGER.debug("Stromer status: {}".format(self.status))

                endpoint = f"bike/{self.bike_id}/position/"
                self.position = await self.stromer_call_api(endpoint=endpoint, data=data)
                LOGGER.debug("Stromer position: {}".format(self.position))
                return

            except Exception as e:
                LOGGER.error("Stromer error: api call failed: {}".format(e))
                LOGGER.debug("Stromer retry: {}/10".format(attempts))

        LOGGER.error("Stromer error: api call failed 10 times, cowardly failing")
        raise ApiError

the issue is fixed.

CoMPaTech commented 11 months ago

Thanks, I mistakenly updated the unittest to include the API fix ... I'll rework yours into it instead of re(re)verting. Thanx!