Add entity name translations to prusalink entities (#90833)

pull/90855/head
Paul Bottein 2023-04-05 12:17:00 +02:00 committed by Franck Nijhof
parent 2a23583d67
commit 65b877bb77
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
6 changed files with 50 additions and 16 deletions

View File

@ -38,7 +38,7 @@ BUTTONS: dict[str, tuple[PrusaLinkButtonEntityDescription, ...]] = {
"printer": ( "printer": (
PrusaLinkButtonEntityDescription[PrinterInfo]( PrusaLinkButtonEntityDescription[PrinterInfo](
key="printer.cancel_job", key="printer.cancel_job",
name="Cancel Job", translation_key="cancel_job",
icon="mdi:cancel", icon="mdi:cancel",
press_fn=lambda api: cast(Coroutine, api.cancel_job()), press_fn=lambda api: cast(Coroutine, api.cancel_job()),
available_fn=lambda data: any( available_fn=lambda data: any(
@ -48,7 +48,7 @@ BUTTONS: dict[str, tuple[PrusaLinkButtonEntityDescription, ...]] = {
), ),
PrusaLinkButtonEntityDescription[PrinterInfo]( PrusaLinkButtonEntityDescription[PrinterInfo](
key="job.pause_job", key="job.pause_job",
name="Pause Job", translation_key="pause_job",
icon="mdi:pause", icon="mdi:pause",
press_fn=lambda api: cast(Coroutine, api.pause_job()), press_fn=lambda api: cast(Coroutine, api.pause_job()),
available_fn=lambda data: ( available_fn=lambda data: (
@ -58,7 +58,7 @@ BUTTONS: dict[str, tuple[PrusaLinkButtonEntityDescription, ...]] = {
), ),
PrusaLinkButtonEntityDescription[PrinterInfo]( PrusaLinkButtonEntityDescription[PrinterInfo](
key="job.resume_job", key="job.resume_job",
name="Resume Job", translation_key="resume_job",
icon="mdi:play", icon="mdi:play",
press_fn=lambda api: cast(Coroutine, api.resume_job()), press_fn=lambda api: cast(Coroutine, api.resume_job()),
available_fn=lambda data: cast(bool, data["state"]["flags"]["paused"]), available_fn=lambda data: cast(bool, data["state"]["flags"]["paused"]),

View File

@ -24,7 +24,7 @@ class PrusaLinkJobPreviewEntity(PrusaLinkEntity, Camera):
last_path = "" last_path = ""
last_image: bytes last_image: bytes
_attr_name = "Job Preview" _attr_translation_key = "job_preview"
def __init__(self, coordinator: JobUpdateCoordinator) -> None: def __init__(self, coordinator: JobUpdateCoordinator) -> None:
"""Initialize a PrusaLink camera entity.""" """Initialize a PrusaLink camera entity."""

View File

@ -65,7 +65,7 @@ SENSORS: dict[str, tuple[PrusaLinkSensorEntityDescription, ...]] = {
), ),
PrusaLinkSensorEntityDescription[PrinterInfo]( PrusaLinkSensorEntityDescription[PrinterInfo](
key="printer.telemetry.temp-bed", key="printer.telemetry.temp-bed",
name="Heatbed", translation_key="heatbed_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -74,7 +74,7 @@ SENSORS: dict[str, tuple[PrusaLinkSensorEntityDescription, ...]] = {
), ),
PrusaLinkSensorEntityDescription[PrinterInfo]( PrusaLinkSensorEntityDescription[PrinterInfo](
key="printer.telemetry.temp-nozzle", key="printer.telemetry.temp-nozzle",
name="Nozzle Temperature", translation_key="nozzle_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -85,7 +85,7 @@ SENSORS: dict[str, tuple[PrusaLinkSensorEntityDescription, ...]] = {
"job": ( "job": (
PrusaLinkSensorEntityDescription[JobInfo]( PrusaLinkSensorEntityDescription[JobInfo](
key="job.progress", key="job.progress",
name="Progress", translation_key="progress",
icon="mdi:progress-clock", icon="mdi:progress-clock",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
value_fn=lambda data: cast(float, data["progress"]["completion"]) * 100, value_fn=lambda data: cast(float, data["progress"]["completion"]) * 100,
@ -93,14 +93,14 @@ SENSORS: dict[str, tuple[PrusaLinkSensorEntityDescription, ...]] = {
), ),
PrusaLinkSensorEntityDescription[JobInfo]( PrusaLinkSensorEntityDescription[JobInfo](
key="job.filename", key="job.filename",
name="Filename", translation_key="filename",
icon="mdi:file-image-outline", icon="mdi:file-image-outline",
value_fn=lambda data: cast(str, data["job"]["file"]["display"]), value_fn=lambda data: cast(str, data["job"]["file"]["display"]),
available_fn=lambda data: data.get("job") is not None, available_fn=lambda data: data.get("job") is not None,
), ),
PrusaLinkSensorEntityDescription[JobInfo]( PrusaLinkSensorEntityDescription[JobInfo](
key="job.start", key="job.start",
name="Print Start", translation_key="print_start",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
icon="mdi:clock-start", icon="mdi:clock-start",
value_fn=ignore_variance( value_fn=ignore_variance(
@ -113,7 +113,7 @@ SENSORS: dict[str, tuple[PrusaLinkSensorEntityDescription, ...]] = {
), ),
PrusaLinkSensorEntityDescription[JobInfo]( PrusaLinkSensorEntityDescription[JobInfo](
key="job.finish", key="job.finish",
name="Print Finish", translation_key="print_finish",
icon="mdi:clock-end", icon="mdi:clock-end",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
value_fn=ignore_variance( value_fn=ignore_variance(

View File

@ -25,6 +25,40 @@
"pausing": "Pausing", "pausing": "Pausing",
"printing": "Printing" "printing": "Printing"
} }
},
"heatbed_temperature": {
"name": "Heatbed temperature"
},
"nozzle_temperature": {
"name": "Nozzle temperature"
},
"progress": {
"name": "Progress"
},
"filename": {
"name": "Filename"
},
"print_start": {
"name": "Print start"
},
"print_finish": {
"name": "Print finish"
}
},
"button": {
"cancel_job": {
"name": "Cancel job"
},
"pause_job": {
"name": "Pause job"
},
"resume_job": {
"name": "Resume job"
}
},
"camera": {
"job_preview": {
"name": "Preview"
} }
} }
} }

View File

@ -25,12 +25,12 @@ async def test_camera_no_job(
) -> None: ) -> None:
"""Test camera while no job active.""" """Test camera while no job active."""
assert await async_setup_component(hass, "prusalink", {}) assert await async_setup_component(hass, "prusalink", {})
state = hass.states.get("camera.mock_title_job_preview") state = hass.states.get("camera.mock_title_preview")
assert state is not None assert state is not None
assert state.state == "unavailable" assert state.state == "unavailable"
client = await hass_client() client = await hass_client()
resp = await client.get("/api/camera_proxy/camera.mock_title_job_preview") resp = await client.get("/api/camera_proxy/camera.mock_title_preview")
assert resp.status == 500 assert resp.status == 500
@ -43,19 +43,19 @@ async def test_camera_active_job(
) -> None: ) -> None:
"""Test camera while job active.""" """Test camera while job active."""
assert await async_setup_component(hass, "prusalink", {}) assert await async_setup_component(hass, "prusalink", {})
state = hass.states.get("camera.mock_title_job_preview") state = hass.states.get("camera.mock_title_preview")
assert state is not None assert state is not None
assert state.state == "idle" assert state.state == "idle"
client = await hass_client() client = await hass_client()
with patch("pyprusalink.PrusaLink.get_large_thumbnail", return_value=b"hello"): with patch("pyprusalink.PrusaLink.get_large_thumbnail", return_value=b"hello"):
resp = await client.get("/api/camera_proxy/camera.mock_title_job_preview") resp = await client.get("/api/camera_proxy/camera.mock_title_preview")
assert resp.status == 200 assert resp.status == 200
assert await resp.read() == b"hello" assert await resp.read() == b"hello"
# Make sure we hit cached value. # Make sure we hit cached value.
with patch("pyprusalink.PrusaLink.get_large_thumbnail", side_effect=ValueError): with patch("pyprusalink.PrusaLink.get_large_thumbnail", side_effect=ValueError):
resp = await client.get("/api/camera_proxy/camera.mock_title_job_preview") resp = await client.get("/api/camera_proxy/camera.mock_title_preview")
assert resp.status == 200 assert resp.status == 200
assert await resp.read() == b"hello" assert await resp.read() == b"hello"

View File

@ -49,7 +49,7 @@ async def test_sensors_no_job(hass: HomeAssistant, mock_config_entry, mock_api)
"printing", "printing",
] ]
state = hass.states.get("sensor.mock_title_heatbed") state = hass.states.get("sensor.mock_title_heatbed_temperature")
assert state is not None assert state is not None
assert state.state == "41.9" assert state.state == "41.9"
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS