meichthys / uptime_kuma

Uptime Kuma HACS integration
114 stars 23 forks source link

sensor_name_from_url does not handle - character in URL #74

Closed gregrob closed 1 year ago

gregrob commented 1 year ago

The function sensor_name_from_url inside of utils.py does not appear to handle a - character in the url.

My uptime kuma URLs is uptime-kuma.home.lan.

Once the integration is started this results in the following error being logged:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 714, in _async_add_entity
    raise HomeAssistantError(f"Invalid entity ID: {entity.entity_id}")
homeassistant.exceptions.HomeAssistantError: Invalid entity ID: sensor.uptimekuma_uptime-kuma_home_lan

Python doesn't like the - character in that name. URLs are allowed to contain other special characters but I think this particular character will be very common for uptime-kuma instances. @PhilippMundhenk there are probably better ways but would it be possible to add another line to the function to achieve this cleaning:

def sensor_name_from_url(url: str) -> str:
    parsed_url = urlparse(url)
    clean_url = parsed_url.netloc.replace(":"+str(parsed_url.port), "")
    clean_url.replace("-", "_")
    return clean_url.replace(".", "_")
PhilippMundhenk commented 1 year ago

Indeed, I missed that. I decided to follow the approach from @meichthys just above and replace all special chars with underscores, that should more generally solve this problem.

meichthys commented 1 year ago

Fix released in v2.3.0 πŸ‘

gregrob commented 1 year ago

Perfect - resolves it for me - thanks again πŸ˜€πŸ‘