From f10a5b7ee89653b328efa97a8d1d992d51296945 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 22 Aug 2023 22:09:18 +0200 Subject: [PATCH] Add entity translations to Dexcom (#98795) --- homeassistant/components/dexcom/sensor.py | 7 ++- homeassistant/components/dexcom/strings.json | 10 ++++ tests/components/dexcom/test_sensor.py | 48 ++++++-------------- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/dexcom/sensor.py b/homeassistant/components/dexcom/sensor.py index 1a3c2a21011..126d946e57d 100644 --- a/homeassistant/components/dexcom/sensor.py +++ b/homeassistant/components/dexcom/sensor.py @@ -38,6 +38,8 @@ async def async_setup_entry( class DexcomSensorEntity(CoordinatorEntity, SensorEntity): """Base Dexcom sensor entity.""" + _attr_has_entity_name = True + def __init__( self, coordinator: DataUpdateCoordinator, username: str, entry_id: str, key: str ) -> None: @@ -54,6 +56,7 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity): """Representation of a Dexcom glucose value sensor.""" _attr_icon = GLUCOSE_VALUE_ICON + _attr_translation_key = "glucose_value" def __init__( self, @@ -66,7 +69,6 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity): super().__init__(coordinator, username, entry_id, "value") self._attr_native_unit_of_measurement = unit_of_measurement self._key = "mg_dl" if unit_of_measurement == MG_DL else "mmol_l" - self._attr_name = f"{DOMAIN}_{username}_glucose_value" @property def native_value(self): @@ -79,12 +81,13 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity): class DexcomGlucoseTrendSensor(DexcomSensorEntity): """Representation of a Dexcom glucose trend sensor.""" + _attr_translation_key = "glucose_trend" + def __init__( self, coordinator: DataUpdateCoordinator, username: str, entry_id: str ) -> None: """Initialize the sensor.""" super().__init__(coordinator, username, entry_id, "trend") - self._attr_name = f"{DOMAIN}_{username}_glucose_trend" @property def icon(self): diff --git a/homeassistant/components/dexcom/strings.json b/homeassistant/components/dexcom/strings.json index 35d80371c12..7efc2708bcc 100644 --- a/homeassistant/components/dexcom/strings.json +++ b/homeassistant/components/dexcom/strings.json @@ -28,5 +28,15 @@ } } } + }, + "entity": { + "sensor": { + "glucose_value": { + "name": "Glucose value" + }, + "glucose_trend": { + "name": "Glucose trend" + } + } } } diff --git a/tests/components/dexcom/test_sensor.py b/tests/components/dexcom/test_sensor.py index 8e1974a3533..a211f0606f3 100644 --- a/tests/components/dexcom/test_sensor.py +++ b/tests/components/dexcom/test_sensor.py @@ -19,13 +19,9 @@ async def test_sensors(hass: HomeAssistant) -> None: """Test we get sensor data.""" await init_integration(hass) - test_username_glucose_value = hass.states.get( - "sensor.dexcom_test_username_glucose_value" - ) + test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value") assert test_username_glucose_value.state == str(GLUCOSE_READING.value) - test_username_glucose_trend = hass.states.get( - "sensor.dexcom_test_username_glucose_trend" - ) + test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend") assert test_username_glucose_trend.state == GLUCOSE_READING.trend_description @@ -37,16 +33,12 @@ async def test_sensors_unknown(hass: HomeAssistant) -> None: "homeassistant.components.dexcom.Dexcom.get_current_glucose_reading", return_value=None, ): - await async_update_entity(hass, "sensor.dexcom_test_username_glucose_value") - await async_update_entity(hass, "sensor.dexcom_test_username_glucose_trend") + await async_update_entity(hass, "sensor.test_username_glucose_value") + await async_update_entity(hass, "sensor.test_username_glucose_trend") - test_username_glucose_value = hass.states.get( - "sensor.dexcom_test_username_glucose_value" - ) + test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value") assert test_username_glucose_value.state == STATE_UNKNOWN - test_username_glucose_trend = hass.states.get( - "sensor.dexcom_test_username_glucose_trend" - ) + test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend") assert test_username_glucose_trend.state == STATE_UNKNOWN @@ -58,16 +50,12 @@ async def test_sensors_update_failed(hass: HomeAssistant) -> None: "homeassistant.components.dexcom.Dexcom.get_current_glucose_reading", side_effect=SessionError, ): - await async_update_entity(hass, "sensor.dexcom_test_username_glucose_value") - await async_update_entity(hass, "sensor.dexcom_test_username_glucose_trend") + await async_update_entity(hass, "sensor.test_username_glucose_value") + await async_update_entity(hass, "sensor.test_username_glucose_trend") - test_username_glucose_value = hass.states.get( - "sensor.dexcom_test_username_glucose_value" - ) + test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value") assert test_username_glucose_value.state == STATE_UNAVAILABLE - test_username_glucose_trend = hass.states.get( - "sensor.dexcom_test_username_glucose_trend" - ) + test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend") assert test_username_glucose_trend.state == STATE_UNAVAILABLE @@ -75,13 +63,9 @@ async def test_sensors_options_changed(hass: HomeAssistant) -> None: """Test we handle sensor unavailable.""" entry = await init_integration(hass) - test_username_glucose_value = hass.states.get( - "sensor.dexcom_test_username_glucose_value" - ) + test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value") assert test_username_glucose_value.state == str(GLUCOSE_READING.value) - test_username_glucose_trend = hass.states.get( - "sensor.dexcom_test_username_glucose_trend" - ) + test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend") assert test_username_glucose_trend.state == GLUCOSE_READING.trend_description with patch( @@ -99,11 +83,7 @@ async def test_sensors_options_changed(hass: HomeAssistant) -> None: assert entry.options == {CONF_UNIT_OF_MEASUREMENT: MMOL_L} - test_username_glucose_value = hass.states.get( - "sensor.dexcom_test_username_glucose_value" - ) + test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value") assert test_username_glucose_value.state == str(GLUCOSE_READING.mmol_l) - test_username_glucose_trend = hass.states.get( - "sensor.dexcom_test_username_glucose_trend" - ) + test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend") assert test_username_glucose_trend.state == GLUCOSE_READING.trend_description