diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 71d56cda18a..c20296a2c18 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -168,7 +168,10 @@ class PrometheusMetrics: return "".join( [ c - if c in string.ascii_letters or c.isdigit() or c == "_" or c == ":" + if c in string.ascii_letters + or c in string.digits + or c == "_" + or c == ":" else f"u{hex(ord(c))}" for c in metric ] diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 206d7477509..5c6189a811e 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -46,6 +46,13 @@ async def prometheus_client(loop, hass, hass_client): sensor4.entity_id = "sensor.wind_direction" await sensor4.async_update_ha_state() + sensor5 = DemoSensor( + None, "SPS30 PM <1µm Weight concentration", 3.7069, None, "µg/m³", None + ) + sensor5.hass = hass + sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration" + await sensor5.async_update_ha_state() + return await hass_client() @@ -113,3 +120,9 @@ async def test_view(prometheus_client): # pylint: disable=redefined-outer-name 'entity="sensor.wind_direction",' 'friendly_name="Wind Direction"} 25.0' in body ) + + assert ( + 'sensor_unit_u0xb5g_per_mu0xb3{domain="sensor",' + 'entity="sensor.sps30_pm_1um_weight_concentration",' + 'friendly_name="SPS30 PM <1µm Weight concentration"} 3.7069' in body + )