dckiller51 / bodymiscale

Custom_components Body Metrics for Xiaomi Miscale 1 and 2 (esphome or BLE monitor for Homeassistant)
Apache License 2.0
205 stars 32 forks source link

Error after Core version 2023.7.0 installed #173

Closed criticallimit closed 1 year ago

criticallimit commented 1 year ago

This is a new error after 2023.7.0 installed:

Logger: homeassistant.components.sensor
Source: custom_components/bodymiscale/entity.py:40 
Integration: Sensor (documentation, issues) 
First occurred: 11:52:18 (2 occurrences) 
Last logged: 11:52:22

Error while setting up bodymiscale platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 353, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/bodymiscale/sensor.py", line 51, in async_setup_entry
    BodyScaleSensor(
  File "/config/custom_components/bodymiscale/sensor.py", line 165, in __init__
    super().__init__(handler, entity_description)
  File "/config/custom_components/bodymiscale/entity.py", line 40, in __init__
    if not self.entity_description.name.lower().startswith(name.lower()):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'UndefinedType' object has no attribute 'lower'

Bildschirmfoto

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:358 
First occurred: 11:52:18 (3 occurrences) 
Last logged: 11:52:22

TemplateError('ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr("bodymiscale.dirk", "height") | round (2) / 100 }}' but no default was specified') while processing template 'Template<template=({{ state_attr("bodymiscale.dirk", "height") | round (2) / 100 }}) renders=10>' for attribute '_attr_native_value' in entity 'sensor.waage_groesse_dirk'
TemplateError('ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr("bodymiscale.mo", "height") | round (2) / 100 }}' but no default was specified') while processing template 'Template<template=({{ state_attr("bodymiscale.mo", "height") | round (2) / 100 }}) renders=8>' for attribute '_attr_native_value' in entity 'sensor.waage_groesse_mo'
TemplateError('ValueError: Template error: round got invalid input 'None' when rendering template '{{ state_attr("bodymiscale.dirk", "height") | round (2) / 100 }}' but no default was specified') while processing template 'Template<template=({{ state_attr("bodymiscale.dirk", "height") | round (2) / 100 }}) renders=14>' for attribute '_attr_native_value' in entity 'sensor.waage_groesse_dirk'
dckiller51 commented 1 year ago

@criticallimit Hello, you have to wait because you are using a beta version. If you are an uninformed user, stay on the official versions or help solve the problems. Maybe we should wait for a bit of an answer here? https://github.com/home-assistant/core/pull/95547

yeaaaaaahh commented 1 year ago

Same error received today when upgrading to the official core version 2023.7

Logger: homeassistant.components.sensor Source: custom_components/bodymiscale/entity.py:40 Integration: Sensor (documentation, issues) First occurred: 7:30:58 AM (2 occurrences) Last logged: 7:30:58 AM

Error while setting up bodymiscale platform for sensor Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 353, in _async_setup_platform await asyncio.shield(task) File "/config/custom_components/bodymiscale/sensor.py", line 51, in async_setup_entry BodyScaleSensor( File "/config/custom_components/bodymiscale/sensor.py", line 165, in init super().init(handler, entity_description) File "/config/custom_components/bodymiscale/entity.py", line 40, in init if not self.entity_description.name.lower().startswith(name.lower()): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'UndefinedType' object has no attribute 'lower'

avbor commented 1 year ago

Same error on 2023.7:

Logger: homeassistant.components.sensor
Source: custom_components/bodymiscale/entity.py:40
Integration: Сенсор ([documentation](https://www.home-assistant.io/integrations/sensor), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+sensor%22))
First occurred: 11:21:11 (4 occurrences)
Last logged: 11:21:11

Error while setting up bodymiscale platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 353, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/bodymiscale/sensor.py", line 51, in async_setup_entry
    BodyScaleSensor(
  File "/config/custom_components/bodymiscale/sensor.py", line 165, in __init__
    super().__init__(handler, entity_description)
  File "/config/custom_components/bodymiscale/entity.py", line 40, in __init__
    if not self.entity_description.name.lower().startswith(name.lower()):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'UndefinedType' object has no attribute 'lower'
dckiller51 commented 1 year ago

@edenhaus Hi I hope you're fine. I need your help on this error. If I delete line 40 (entity.py) no worries but the names are inconsistent "normal". I don't understand their change.

menloperk commented 1 year ago

Same 'Unavailable' errors for all entities since upgrade to official 2023.7.0!

edenhaus commented 1 year ago

I will have a look. HA 2023.7 change something on the nameming

criticallimit commented 1 year ago

when I exclude lines 38 to 44 in the entity.py everything is working normal in my existing installation, but don´t know the efect for new installations. That´s maybe a workaround for us till the fault is found.

#        if self.entity_description.name:
#            # Name provided, using the provided one
#            if not self.entity_description.name.lower().startswith(name.lower()):
#                # Entity name should start with configurated name
#                self._attr_name = f"{name} {self.entity_description.name}"
#        else:
#            self._attr_name = f"{name} {self.entity_description.key.replace('_', ' ')}"
MoiEsFe commented 1 year ago

when I exclude lines 38 to 44 in the entity.py everything is working normal in my existing installation, but don´t know the efect for new installations. That´s maybe a workaround for us till the fault is found.

#        if self.entity_description.name:
#            # Name provided, using the provided one
#            if not self.entity_description.name.lower().startswith(name.lower()):
#                # Entity name should start with configurated name
#                self._attr_name = f"{name} {self.entity_description.name}"
#        else:
#            self._attr_name = f"{name} {self.entity_description.key.replace('_', ' ')}"

I have tried to exclude those lines, but it has not worked for me, in my case, I have several users.

I hope there will be a solution soon

arniebarni commented 1 year ago

Somehow the self.entity_description.name seems to be of type UndefinedType._singleton. So it's neither None nor does it provide the string.lower() function. These are just observations, I can't derive any meaning from that. However, I get it to work in a multi user setup with disabling the first bit of the if clause:

#       if self.entity_description.name:
        if False:
            # Name provided, using the provided one
            if not self.entity_description.name.lower().startswith(name.lower()):
                # Entity name should start with configurated name
                self._attr_name = f"{name} {self.entity_description.name}"
        else:
            self._attr_name = f"{name} {self.entity_description.key.replace('_', ' ')}"

So maybe catching the UndefinedType would help.

Cheers

criticallimit commented 1 year ago

This solved the problem on my installation:

        if self.entity_description.name and isinstance(self.entity_description.name, str):
criticallimit commented 1 year ago

my complete entity.py:

"""Bodymiscale entity module."""
from typing import Optional

from homeassistant.const import CONF_NAME
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo, Entity, EntityDescription

from .const import DOMAIN, NAME, VERSION
from .metrics import BodyScaleMetricsHandler

class BodyScaleBaseEntity(Entity):  # type: ignore[misc]
    """Body scale base entity."""

    _attr_should_poll = False

    def __init__(
        self,
        handler: BodyScaleMetricsHandler,
        entity_description: Optional[EntityDescription] = None,
    ):
        """Initialize the entity."""
        super().__init__()
        self._handler = handler
        if entity_description:
            self.entity_description = entity_description
        elif not hasattr(self, "entity_description"):
            raise ValueError(
                '"entity_description" must be either set as class variable or passed on init!'
            )

        if not self.entity_description.key:
            raise ValueError('"entity_description.key" must be either set!')

        name = handler.config[CONF_NAME]
        self._attr_unique_id = "_".join([DOMAIN, name, self.entity_description.key])

        if self.entity_description.name and isinstance(self.entity_description.name, str):
            # Name provided, using the provided one
            if not self.entity_description.name.lower().startswith(name.lower()):
               # Entity name should start with configurated name
               self._attr_name = f"{name} {self.entity_description.name}"
        else:
            self._attr_name = f"{name} {self.entity_description.key.replace('_', ' ')}"

    @property
    def device_info(self) -> Optional[DeviceInfo]:
        """Return device specific attributes."""
        return DeviceInfo(
            entry_type=DeviceEntryType.SERVICE,
            name=NAME,
            sw_version=VERSION,
            identifiers={(DOMAIN, self._handler.config_entry_id)},
        )
edenhaus commented 1 year ago

PR with fix is ready for testing. For feedback please comment directly in the PR