From dd0f11a062322a632a3027fc68914acfee9eaabd Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 4 Jan 2023 00:56:46 +0100 Subject: [PATCH] Add translation key for IPP printer integration (#84441) * Add translation key for IPP printer integration * Add tests --- homeassistant/components/ipp/sensor.py | 6 ++++++ homeassistant/components/ipp/strings.json | 11 +++++++++++ homeassistant/components/ipp/translations/en.json | 11 +++++++++++ tests/components/ipp/test_sensor.py | 10 +++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/ipp/sensor.py b/homeassistant/components/ipp/sensor.py index 293a6378b78..5058f6d10a8 100644 --- a/homeassistant/components/ipp/sensor.py +++ b/homeassistant/components/ipp/sensor.py @@ -66,11 +66,13 @@ class IPPSensor(IPPEntity, SensorEntity): key: str, name: str, unit_of_measurement: str | None = None, + translation_key: str | None = None, ) -> None: """Initialize IPP sensor.""" self._key = key self._attr_unique_id = f"{unique_id}_{key}" self._attr_native_unit_of_measurement = unit_of_measurement + self._attr_translation_key = translation_key super().__init__( entry_id=entry_id, @@ -136,6 +138,9 @@ class IPPMarkerSensor(IPPSensor): class IPPPrinterSensor(IPPSensor): """Defines an IPP printer sensor.""" + _attr_device_class = SensorDeviceClass.ENUM + _attr_options = ["idle", "printing", "stopped"] + def __init__( self, entry_id: str, unique_id: str, coordinator: IPPDataUpdateCoordinator ) -> None: @@ -148,6 +153,7 @@ class IPPPrinterSensor(IPPSensor): key="printer", name=coordinator.data.info.name, unit_of_measurement=None, + translation_key="printer", ) @property diff --git a/homeassistant/components/ipp/strings.json b/homeassistant/components/ipp/strings.json index c43b9a25463..fa7dd9b6bf8 100644 --- a/homeassistant/components/ipp/strings.json +++ b/homeassistant/components/ipp/strings.json @@ -31,5 +31,16 @@ "parse_error": "Failed to parse response from printer.", "unique_id_required": "Device missing unique identification required for discovery." } + }, + "entity": { + "sensor": { + "printer": { + "state": { + "printing": "Printing", + "idle": "Idle", + "stopped": "Stopped" + } + } + } } } diff --git a/homeassistant/components/ipp/translations/en.json b/homeassistant/components/ipp/translations/en.json index a8bfba5ac32..b530076a2af 100644 --- a/homeassistant/components/ipp/translations/en.json +++ b/homeassistant/components/ipp/translations/en.json @@ -31,5 +31,16 @@ "title": "Discovered printer" } } + }, + "entity": { + "sensor": { + "printer": { + "state": { + "printing": "Printing", + "idle": "Idle", + "stopped": "Stopped" + } + } + } } } \ No newline at end of file diff --git a/tests/components/ipp/test_sensor.py b/tests/components/ipp/test_sensor.py index d772c6e9163..f8dd94ffc72 100644 --- a/tests/components/ipp/test_sensor.py +++ b/tests/components/ipp/test_sensor.py @@ -3,7 +3,10 @@ from datetime import datetime from unittest.mock import patch from homeassistant.components.ipp.const import DOMAIN -from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.components.sensor import ( + ATTR_OPTIONS as SENSOR_ATTR_OPTIONS, + DOMAIN as SENSOR_DOMAIN, +) from homeassistant.const import ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -41,6 +44,11 @@ async def test_sensors( assert state assert state.attributes.get(ATTR_ICON) == "mdi:printer" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None + assert state.attributes.get(SENSOR_ATTR_OPTIONS) == ["idle", "printing", "stopped"] + + entry = registry.async_get("sensor.epson_xp_6000_series") + assert entry + assert entry.translation_key == "printer" state = hass.states.get("sensor.epson_xp_6000_series_black_ink") assert state