zweckj / lmcloud

Library to interface with La Marzocco's cloud

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Check in true_model_name causes errors and breaks HA integration for non-Micra models

rccoleman opened this issue · comments

In this function:

    @property
    def true_model_name(self) -> str:
        """Return the model name from the cloud, even if it's not one we know about.
        Used for display only."""
        if self.model_name == LaMarzoccoModel.LINEA_MICRA:
            return "Linea Micra"
        if self.model_name in LaMarzoccoModel:
            return self.model_name
        return f"Unsupported Model ({self.model_name})"

This part causes an exception because self.model_name is a string and LaMarzoccoModel is an enum.
if self.model_name in LaMarzoccoModel:

It results in exceptions like the following with a non-Micra model:

2023-12-29 13:03:08.499 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 495, in async_add_entities
    tasks = [
            ^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 495, in <listcomp>
    tasks = [
            ^
  File "/config/custom_components/lamarzocco/binary_sensor.py", line 61, in <genexpr>
    LaMarzoccoBinarySensorEntity(coordinator, hass, description)
  File "/config/custom_components/lamarzocco/entity.py", line 53, in __init__
    model=self._lm_client.true_model_name,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/lmcloud/lmcloud.py", line 123, in true_model_name
    if self.model_name in LaMarzoccoModel:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/enum.py", line 740, in __contains__
    raise TypeError(
TypeError: unsupported operand type(s) for 'in': 'str' and 'EnumType'
2023-12-29 13:03:08.505 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 495, in async_add_entities
    tasks = [
            ^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 495, in <listcomp>
    tasks = [
            ^
  File "/config/custom_components/lamarzocco/button.py", line 44, in <genexpr>
    LaMarzoccoButtonEntity(coordinator, hass, description)
  File "/config/custom_components/lamarzocco/entity.py", line 53, in __init__
    model=self._lm_client.true_model_name,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/lmcloud/lmcloud.py", line 123, in true_model_name
    if self.model_name in LaMarzoccoModel:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/enum.py", line 740, in __contains__
    raise TypeError(
TypeError: unsupported operand type(s) for 'in': 'str' and 'EnumType'

These exceptions completely break the HA integration with v0.13.2b2 of the integration and HA 2024.1.0b2. Looks like it came in with a7d4c69.

One of these tests would fix it:

if self.model_name in LaMarzoccoModel._value2member_map_:
  return self.model_name

or

try:
  return LaMarzoccoModel(self.model_name)
except:
  return f"Unsupported Model ({self.model_name})"

I'm using this and it's working for me:

    @property                            
    def true_model_name(self) -> str:     
        """Return the model name from the cloud, even if it's not one we know about.
        Used for display only."""                                         
        if self.model_name == LaMarzoccoModel.LINEA_MICRA:                    
            return "Linea Micra"                      
        try:                                       
            return LaMarzoccoModel(self.model_name)         
        except:                                         
            return f"Unsupported Model ({self.model_name})"

I though HA uses Python 3.12 already (where I can test for string enums like that), I'll add a fix

Thanks! Python 3.12 isn't coming until Feb, from what I recall from Discord.

fixed in 0.4.19, thanks for letting me know!