jaroschek / home-assistant-myuplink

Custom Home Assistant integration for devices and sensors in myUplink account.
39 stars 8 forks source link

Handle skipping string-only parameters #104

Closed 7RST1 closed 2 months ago

7RST1 commented 2 months ago

I found my water heater to suddenly not load properly since last month, and found an error coming from parsing one of the parameters: bilde

The error I got ``` 2024-04-23 07:13:26.217 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.18760ne2220523854649_model_id for domain sensor with platform myuplink Traceback (most recent call last): File "/workspaces/core/homeassistant/components/sensor/__init__.py", line 658, in state numerical_value = int(value) ^^^^^^^^^^ ValueError: invalid literal for int() with base 10: '18760NE2220523854649' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/workspaces/core/homeassistant/components/sensor/__init__.py", line 661, in state numerical_value = float(value) ^^^^^^^^^^^^ ValueError: could not convert string to float: '18760NE2220523854649' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/workspaces/core/homeassistant/helpers/entity_platform.py", line 580, in _async_add_entities await coro File "/workspaces/core/homeassistant/helpers/entity_platform.py", line 892, in _async_add_entity await entity.add_to_platform_finish() File "/workspaces/core/homeassistant/helpers/entity.py", line 1343, in add_to_platform_finish self.async_write_ha_state() File "/workspaces/core/homeassistant/helpers/entity.py", line 995, in async_write_ha_state self._async_write_ha_state() File "/workspaces/core/homeassistant/helpers/entity.py", line 1118, in _async_write_ha_state state, attr, capabilities, shadowed_attr = self.__async_calculate_state() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspaces/core/homeassistant/helpers/entity.py", line 1053, in __async_calculate_state state = self._stringify_state(available) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspaces/core/homeassistant/helpers/entity.py", line 1001, in _stringify_state if (state := self.state) is None: ^^^^^^^^^^ File "/workspaces/core/homeassistant/components/sensor/__init__.py", line 665, in state raise ValueError( ValueError: Sensor sensor.18760ne2220523854649_model_id has device class 'None', state class 'None' unit '' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '18760NE2220523854649' () ```

When checking for fitting entity type, this one gets sensor. However, having just a string as a value doesn't seem to be supported. It tried casting it to int or float internally and failed, resulting in a stunted parsing process.

I implemented a check that tries casting it to int then float, and if it fails both, return a fitting_entity as None, thus none of the platforms will handle the parameter. ~The check isn't the most elegant, but python itself isn't either so I don't know if it can be done any better.~ Works for my setup at least, only affecting this parameter.

I also did some general tweaking of the function, and made it run once during __init__ so it doesn't have to run everytime a platform checks.

7RST1 commented 2 months ago

Just realized there was another pr for this 🤦, i got too eager fixing. Maybe some of this can be implemented after that has merged.