Migrate kmtronic to has entity name (#107469)

This commit is contained in:
Joost Lekkerkerker 2024-01-08 09:14:37 +01:00 committed by GitHub
parent af209fe2b8
commit 1171a7a3d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 18 deletions

View file

@ -29,5 +29,12 @@
}
}
}
},
"entity": {
"switch": {
"relay": {
"name": "Relay {relay_id}"
}
}
}
}

View file

@ -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

View file

@ -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"