Add entity translations to Switcher kis (#99223)

* Add entity translations to switcher_kis

* Add entity translations

* Fix tests
This commit is contained in:
Joost Lekkerkerker 2023-08-28 19:53:42 +02:00 committed by GitHub
parent fc6f48e076
commit 821d74e904
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 31 deletions

View File

@ -48,7 +48,7 @@ class SwitcherThermostatButtonEntityDescription(
THERMOSTAT_BUTTONS = [
SwitcherThermostatButtonEntityDescription(
key="assume_on",
name="Assume on",
translation_key="assume_on",
icon="mdi:fan",
entity_category=EntityCategory.CONFIG,
press_fn=lambda api, remote: api.control_breeze_device(
@ -58,7 +58,7 @@ THERMOSTAT_BUTTONS = [
),
SwitcherThermostatButtonEntityDescription(
key="assume_off",
name="Assume off",
translation_key="assume_off",
icon="mdi:fan-off",
entity_category=EntityCategory.CONFIG,
press_fn=lambda api, remote: api.control_breeze_device(
@ -68,7 +68,7 @@ THERMOSTAT_BUTTONS = [
),
SwitcherThermostatButtonEntityDescription(
key="vertical_swing_on",
name="Vertical swing on",
translation_key="vertical_swing_on",
icon="mdi:autorenew",
press_fn=lambda api, remote: api.control_breeze_device(
remote, swing=ThermostatSwing.ON
@ -77,7 +77,7 @@ THERMOSTAT_BUTTONS = [
),
SwitcherThermostatButtonEntityDescription(
key="vertical_swing_off",
name="Vertical swing off",
translation_key="vertical_swing_off",
icon="mdi:autorenew-off",
press_fn=lambda api, remote: api.control_breeze_device(
remote, swing=ThermostatSwing.OFF
@ -117,6 +117,7 @@ class SwitcherThermostatButtonEntity(
"""Representation of a Switcher climate entity."""
entity_description: SwitcherThermostatButtonEntityDescription
_attr_has_entity_name = True
def __init__(
self,
@ -129,7 +130,6 @@ class SwitcherThermostatButtonEntity(
self.entity_description = description
self._remote = remote
self._attr_name = f"{coordinator.name} {description.name}"
self._attr_unique_id = f"{coordinator.mac_address}-{description.key}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}

View File

@ -84,6 +84,9 @@ class SwitcherClimateEntity(
):
"""Representation of a Switcher climate entity."""
_attr_has_entity_name = True
_attr_name = None
def __init__(
self, coordinator: SwitcherDataUpdateCoordinator, remote: SwitcherBreezeRemote
) -> None:
@ -91,7 +94,6 @@ class SwitcherClimateEntity(
super().__init__(coordinator)
self._remote = remote
self._attr_name = coordinator.name
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}

View File

@ -55,6 +55,8 @@ class SwitcherCoverEntity(
):
"""Representation of a Switcher cover entity."""
_attr_has_entity_name = True
_attr_name = None
_attr_device_class = CoverDeviceClass.SHUTTER
_attr_supported_features = (
CoverEntityFeature.OPEN
@ -67,7 +69,6 @@ class SwitcherCoverEntity(
"""Initialize the entity."""
super().__init__(coordinator)
self._attr_name = coordinator.name
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}

View File

@ -25,14 +25,12 @@ from .const import SIGNAL_DEVICE_ADD
POWER_SENSORS: list[SensorEntityDescription] = [
SensorEntityDescription(
key="power_consumption",
name="Power Consumption",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="electric_current",
name="Electric Current",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
@ -40,11 +38,13 @@ POWER_SENSORS: list[SensorEntityDescription] = [
]
TIME_SENSORS: list[SensorEntityDescription] = [
SensorEntityDescription(
key="remaining_time", name="Remaining Time", icon="mdi:av-timer"
key="remaining_time",
translation_key="remaining_time",
icon="mdi:av-timer",
),
SensorEntityDescription(
key="auto_off_set",
name="Auto Shutdown",
translation_key="auto_shutdown",
icon="mdi:progress-clock",
entity_registry_enabled_default=False,
),
@ -85,6 +85,8 @@ class SwitcherSensorEntity(
):
"""Representation of a Switcher sensor entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: SwitcherDataUpdateCoordinator,
@ -94,9 +96,6 @@ class SwitcherSensorEntity(
super().__init__(coordinator)
self.entity_description = description
# Entity class attributes
self._attr_name = f"{coordinator.name} {description.name}"
self._attr_unique_id = (
f"{coordinator.device_id}-{coordinator.mac_address}-{description.key}"
)

View File

@ -10,6 +10,30 @@
"no_devices_found": "[%key:common::config_flow::abort::no_devices_found%]"
}
},
"entity": {
"button": {
"assume_on": {
"name": "Assume on"
},
"assume_off": {
"name": "Assume off"
},
"vertical_swing_on": {
"name": "Vertical swing on"
},
"vertical_swing_off": {
"name": "Vertical swing off"
}
},
"sensor": {
"remaining_time": {
"name": "Remaining time"
},
"auto_shutdown": {
"name": "Auto shutdown"
}
}
},
"services": {
"set_auto_off": {
"name": "Set auto off",

View File

@ -83,13 +83,15 @@ class SwitcherBaseSwitchEntity(
):
"""Representation of a Switcher switch entity."""
_attr_has_entity_name = True
_attr_name = None
def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self.control_result: bool | None = None
# Entity class attributes
self._attr_name = coordinator.name
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
@ -118,7 +120,7 @@ class SwitcherBaseSwitchEntity(
if error or not response or not response.successful:
_LOGGER.error(
"Call api for %s failed, api: '%s', args: %s, response/error: %s",
self.name,
self.coordinator.name,
api,
args,
response or error,
@ -150,7 +152,7 @@ class SwitcherBaseSwitchEntity(
_LOGGER.warning(
"Service '%s' is not supported by %s",
SERVICE_SET_AUTO_OFF_NAME,
self.name,
self.coordinator.name,
)
async def async_turn_on_with_timer_service(self, timer_minutes: int) -> None:
@ -158,7 +160,7 @@ class SwitcherBaseSwitchEntity(
_LOGGER.warning(
"Service '%s' is not supported by %s",
SERVICE_TURN_ON_WITH_TIMER_NAME,
self.name,
self.coordinator.name,
)

View File

@ -77,7 +77,7 @@ async def test_update_fail(
state = hass.states.get(entity_id)
assert state.state == STATE_UNAVAILABLE
entity_id = f"sensor.{slugify(device.name)}_power_consumption"
entity_id = f"sensor.{slugify(device.name)}_power"
state = hass.states.get(entity_id)
assert state.state == STATE_UNAVAILABLE
@ -92,7 +92,7 @@ async def test_update_fail(
state = hass.states.get(entity_id)
assert state.state != STATE_UNAVAILABLE
entity_id = f"sensor.{slugify(device.name)}_power_consumption"
entity_id = f"sensor.{slugify(device.name)}_power"
state = hass.states.get(entity_id)
assert state.state != STATE_UNAVAILABLE

View File

@ -13,16 +13,16 @@ DEVICE_SENSORS_TUPLE = (
(
DUMMY_PLUG_DEVICE,
[
"power_consumption",
"electric_current",
("power", "power_consumption"),
("current", "electric_current"),
],
),
(
DUMMY_WATER_HEATER_DEVICE,
[
"power_consumption",
"electric_current",
"remaining_time",
("power", "power_consumption"),
("current", "electric_current"),
("remaining_time", "remaining_time"),
],
),
)
@ -39,10 +39,10 @@ async def test_sensor_platform(hass: HomeAssistant, mock_bridge) -> None:
assert len(hass.data[DOMAIN][DATA_DEVICE]) == 2
for device, sensors in DEVICE_SENSORS_TUPLE:
for sensor in sensors:
for sensor, field in sensors:
entity_id = f"sensor.{slugify(device.name)}_{sensor}"
state = hass.states.get(entity_id)
assert state.state == str(getattr(device, sensor))
assert state.state == str(getattr(device, field))
async def test_sensor_disabled(hass: HomeAssistant, mock_bridge) -> None:
@ -80,13 +80,13 @@ async def test_sensor_update(hass: HomeAssistant, mock_bridge, monkeypatch) -> N
assert mock_bridge
device = DUMMY_WATER_HEATER_DEVICE
sensor = "power_consumption"
entity_id = f"sensor.{slugify(device.name)}_{sensor}"
field = "power_consumption"
entity_id = f"sensor.{slugify(device.name)}_power"
state = hass.states.get(entity_id)
assert state.state == str(getattr(device, sensor))
assert state.state == str(getattr(device, field))
monkeypatch.setattr(device, sensor, 1431)
monkeypatch.setattr(device, field, 1431)
mock_bridge.mock_callbacks([DUMMY_WATER_HEATER_DEVICE])
await hass.async_block_till_done()