From 1171a7a3d9e56880c9230c7d84c30ed92a233527 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 8 Jan 2024 09:14:37 +0100 Subject: [PATCH] Migrate kmtronic to has entity name (#107469) --- .../components/kmtronic/strings.json | 7 +++ homeassistant/components/kmtronic/switch.py | 5 +- tests/components/kmtronic/test_switch.py | 49 ++++++++++++------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/kmtronic/strings.json b/homeassistant/components/kmtronic/strings.json index 6cecea12f22..f3c1f75d818 100644 --- a/homeassistant/components/kmtronic/strings.json +++ b/homeassistant/components/kmtronic/strings.json @@ -29,5 +29,12 @@ } } } + }, + "entity": { + "switch": { + "relay": { + "name": "Relay {relay_id}" + } + } } } diff --git a/homeassistant/components/kmtronic/switch.py b/homeassistant/components/kmtronic/switch.py index cd1b181803f..144c05e927e 100644 --- a/homeassistant/components/kmtronic/switch.py +++ b/homeassistant/components/kmtronic/switch.py @@ -32,6 +32,9 @@ async def async_setup_entry( class KMtronicSwitch(CoordinatorEntity, SwitchEntity): """KMtronic Switch Entity.""" + _attr_translation_key = "relay" + _attr_has_entity_name = True + def __init__(self, hub, coordinator, relay, reverse, config_entry_id): """Pass coordinator to CoordinatorEntity.""" super().__init__(coordinator) @@ -46,7 +49,7 @@ class KMtronicSwitch(CoordinatorEntity, SwitchEntity): configuration_url=hub.host, ) - self._attr_name = f"Relay{relay.id}" + self._attr_translation_placeholders = {"relay_id": relay.id} self._attr_unique_id = f"{config_entry_id}_relay{relay.id}" @property diff --git a/tests/components/kmtronic/test_switch.py b/tests/components/kmtronic/test_switch.py index 57a695c5919..cb72aba2704 100644 --- a/tests/components/kmtronic/test_switch.py +++ b/tests/components/kmtronic/test_switch.py @@ -39,15 +39,18 @@ async def test_relay_on_off( text="", ) - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "off" await hass.services.async_call( - "switch", "turn_on", {"entity_id": "switch.relay1"}, blocking=True + "switch", + "turn_on", + {"entity_id": "switch.controller_1_1_1_1_relay_1"}, + blocking=True, ) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "on" # Mocks the response for turning a relay1 off @@ -57,11 +60,14 @@ async def test_relay_on_off( ) await hass.services.async_call( - "switch", "turn_off", {"entity_id": "switch.relay1"}, blocking=True + "switch", + "turn_off", + {"entity_id": "switch.controller_1_1_1_1_relay_1"}, + blocking=True, ) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "off" # Mocks the response for turning a relay1 on @@ -71,11 +77,14 @@ async def test_relay_on_off( ) await hass.services.async_call( - "switch", "toggle", {"entity_id": "switch.relay1"}, blocking=True + "switch", + "toggle", + {"entity_id": "switch.controller_1_1_1_1_relay_1"}, + blocking=True, ) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "on" @@ -95,7 +104,7 @@ async def test_update(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) assert await async_setup_component(hass, DOMAIN, {}) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "off" aioclient_mock.clear_requests() @@ -106,7 +115,7 @@ async def test_update(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) async_fire_time_changed(hass, future) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "on" @@ -128,7 +137,7 @@ async def test_failed_update( assert await async_setup_component(hass, DOMAIN, {}) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "off" aioclient_mock.clear_requests() @@ -140,7 +149,7 @@ async def test_failed_update( async_fire_time_changed(hass, future) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == STATE_UNAVAILABLE future += timedelta(minutes=10) @@ -152,7 +161,7 @@ async def test_failed_update( async_fire_time_changed(hass, future) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == STATE_UNAVAILABLE @@ -180,15 +189,18 @@ async def test_relay_on_off_reversed( text="", ) - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "on" await hass.services.async_call( - "switch", "turn_off", {"entity_id": "switch.relay1"}, blocking=True + "switch", + "turn_off", + {"entity_id": "switch.controller_1_1_1_1_relay_1"}, + blocking=True, ) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "off" # Mocks the response for turning a relay1 off @@ -198,9 +210,12 @@ async def test_relay_on_off_reversed( ) await hass.services.async_call( - "switch", "turn_on", {"entity_id": "switch.relay1"}, blocking=True + "switch", + "turn_on", + {"entity_id": "switch.controller_1_1_1_1_relay_1"}, + blocking=True, ) await hass.async_block_till_done() - state = hass.states.get("switch.relay1") + state = hass.states.get("switch.controller_1_1_1_1_relay_1") assert state.state == "on"