Add entity translations to QNAP QSW (#98915)

pull/99322/head
Joost Lekkerkerker 2023-08-30 11:20:15 +02:00 committed by GitHub
parent a89a5f486d
commit 56b99d2bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 29 deletions

View File

@ -23,6 +23,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import UNDEFINED
from .const import ATTR_MESSAGE, DOMAIN, QSW_COORD_DATA
from .coordinator import QswDataCoordinator
@ -48,7 +49,6 @@ BINARY_SENSOR_TYPES: Final[tuple[QswBinarySensorEntityDescription, ...]] = (
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
key=QSD_FIRMWARE_CONDITION,
name="Anomaly",
subkey=QSD_ANOMALY,
),
)
@ -140,8 +140,10 @@ class QswBinarySensor(QswSensorEntity, BinarySensorEntity):
) -> None:
"""Initialize."""
super().__init__(coordinator, entry, type_id)
self._attr_name = f"{self.product} {description.name}"
if description.name == UNDEFINED:
self._attr_has_entity_name = True
else:
self._attr_name = f"{self.product} {description.name}"
self._attr_unique_id = (
f"{entry.unique_id}_{description.key}"
f"{description.sep_key}{description.subkey}"

View File

@ -39,7 +39,6 @@ BUTTON_TYPES: Final[tuple[QswButtonDescription, ...]] = (
device_class=ButtonDeviceClass.RESTART,
entity_category=EntityCategory.CONFIG,
key=QSW_REBOOT,
name="Reboot",
press_action=lambda qsw: qsw.reboot(),
),
)
@ -58,6 +57,8 @@ async def async_setup_entry(
class QswButton(QswDataEntity, ButtonEntity):
"""Define a QNAP QSW button."""
_attr_has_entity_name = True
entity_description: QswButtonDescription
def __init__(
@ -68,7 +69,6 @@ class QswButton(QswDataEntity, ButtonEntity):
) -> None:
"""Initialize."""
super().__init__(coordinator, entry)
self._attr_name = f"{self.product} {description.name}"
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
self.entity_description = description

View File

@ -120,6 +120,8 @@ class QswSensorEntity(QswDataEntity):
class QswFirmwareEntity(CoordinatorEntity[QswFirmwareCoordinator]):
"""Define a QNAP QSW firmware entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: QswFirmwareCoordinator,

View File

@ -43,6 +43,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import UNDEFINED
from .const import ATTR_MAX, DOMAIN, QSW_COORD_DATA, RPM
from .coordinator import QswDataCoordinator
@ -60,57 +61,57 @@ class QswSensorEntityDescription(SensorEntityDescription, QswEntityDescription):
SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = (
QswSensorEntityDescription(
translation_key="fan_1_speed",
icon="mdi:fan-speed-1",
key=QSD_SYSTEM_SENSOR,
name="Fan 1 Speed",
native_unit_of_measurement=RPM,
state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_FAN1_SPEED,
),
QswSensorEntityDescription(
translation_key="fan_2_speed",
icon="mdi:fan-speed-2",
key=QSD_SYSTEM_SENSOR,
name="Fan 2 Speed",
native_unit_of_measurement=RPM,
state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_FAN2_SPEED,
),
QswSensorEntityDescription(
translation_key="ports",
attributes={
ATTR_MAX: [QSD_SYSTEM_BOARD, QSD_PORT_NUM],
},
entity_registry_enabled_default=False,
icon="mdi:ethernet",
key=QSD_PORTS_STATUS,
name="Ports",
state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_LINK,
),
QswSensorEntityDescription(
entity_registry_enabled_default=False,
translation_key="rx",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download-network",
key=QSD_PORTS_STATISTICS,
name="RX",
native_unit_of_measurement=UnitOfInformation.BYTES,
state_class=SensorStateClass.TOTAL_INCREASING,
subkey=QSD_RX_OCTETS,
),
QswSensorEntityDescription(
entity_registry_enabled_default=False,
translation_key="rx_errors",
icon="mdi:close-network",
key=QSD_PORTS_STATISTICS,
entity_category=EntityCategory.DIAGNOSTIC,
name="RX Errors",
state_class=SensorStateClass.TOTAL_INCREASING,
subkey=QSD_RX_ERRORS,
),
QswSensorEntityDescription(
entity_registry_enabled_default=False,
translation_key="rx_speed",
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download-network",
key=QSD_PORTS_STATISTICS,
name="RX Speed",
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_RX_SPEED,
@ -121,36 +122,35 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = (
},
device_class=SensorDeviceClass.TEMPERATURE,
key=QSD_SYSTEM_SENSOR,
name="Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_TEMP,
),
QswSensorEntityDescription(
entity_registry_enabled_default=False,
translation_key="tx",
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload-network",
key=QSD_PORTS_STATISTICS,
name="TX",
native_unit_of_measurement=UnitOfInformation.BYTES,
state_class=SensorStateClass.TOTAL_INCREASING,
subkey=QSD_TX_OCTETS,
),
QswSensorEntityDescription(
entity_registry_enabled_default=False,
translation_key="tx_speed",
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload-network",
key=QSD_PORTS_STATISTICS,
name="TX Speed",
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_TX_SPEED,
),
QswSensorEntityDescription(
translation_key="uptime",
icon="mdi:timer-outline",
key=QSD_SYSTEM_TIME,
entity_category=EntityCategory.DIAGNOSTIC,
name="Uptime",
native_unit_of_measurement=UnitOfTime.SECONDS,
state_class=SensorStateClass.TOTAL_INCREASING,
subkey=QSD_UPTIME,
@ -363,7 +363,10 @@ class QswSensor(QswSensorEntity, SensorEntity):
"""Initialize."""
super().__init__(coordinator, entry, type_id)
self._attr_name = f"{self.product} {description.name}"
if description.name == UNDEFINED:
self._attr_has_entity_name = True
else:
self._attr_name = f"{self.product} {description.name}"
self._attr_unique_id = (
f"{entry.unique_id}_{description.key}"
f"{description.sep_key}{description.subkey}"

View File

@ -23,5 +23,36 @@
}
}
}
},
"entity": {
"sensor": {
"fan_1_speed": {
"name": "Fan 1 speed"
},
"fan_2_speed": {
"name": "Fan 2 speed"
},
"ports": {
"name": "Ports"
},
"rx": {
"name": "RX"
},
"rx_errors": {
"name": "RX errors"
},
"rx_speed": {
"name": "RX speed"
},
"tx": {
"name": "TX"
},
"tx_speed": {
"name": "TX speed"
},
"uptime": {
"name": "Uptime"
}
}
}
}

View File

@ -7,8 +7,6 @@ from aioqsw.const import (
QSD_DESCRIPTION,
QSD_FIRMWARE_CHECK,
QSD_FIRMWARE_INFO,
QSD_PRODUCT,
QSD_SYSTEM_BOARD,
QSD_VERSION,
)
@ -32,7 +30,6 @@ UPDATE_TYPES: Final[tuple[UpdateEntityDescription, ...]] = (
device_class=UpdateDeviceClass.FIRMWARE,
entity_category=EntityCategory.CONFIG,
key=QSW_UPDATE,
name="Firmware Update",
),
)
@ -63,9 +60,6 @@ class QswUpdate(QswFirmwareEntity, UpdateEntity):
) -> None:
"""Initialize."""
super().__init__(coordinator, entry)
self._attr_name = (
f"{self.get_device_value(QSD_SYSTEM_BOARD, QSD_PRODUCT)} {description.name}"
)
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
self.entity_description = description

View File

@ -17,7 +17,7 @@ async def test_qnap_qsw_create_binary_sensors(
await async_init_integration(hass)
state = hass.states.get("binary_sensor.qsw_m408_4c_anomaly")
state = hass.states.get("binary_sensor.qsw_m408_4c_problem")
assert state.state == STATE_OFF
assert state.attributes.get(ATTR_MESSAGE) is None

View File

@ -14,7 +14,7 @@ async def test_qnap_buttons(hass: HomeAssistant) -> None:
await async_init_integration(hass)
state = hass.states.get("button.qsw_m408_4c_reboot")
state = hass.states.get("button.qsw_m408_4c_restart")
assert state
assert state.state == STATE_UNKNOWN
@ -28,7 +28,7 @@ async def test_qnap_buttons(hass: HomeAssistant) -> None:
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.qsw_m408_4c_reboot"},
{ATTR_ENTITY_ID: "button.qsw_m408_4c_restart"},
blocking=True,
)
await hass.async_block_till_done()

View File

@ -127,5 +127,5 @@ async def test_coordinator_client_connector_error(
mock_firmware_update_check.assert_called_once()
update = hass.states.get("update.qsw_m408_4c_firmware_update")
update = hass.states.get("update.qsw_m408_4c_firmware")
assert update.state == STATE_UNAVAILABLE

View File

@ -34,7 +34,7 @@ async def test_qnap_qsw_update(hass: HomeAssistant) -> None:
await async_init_integration(hass)
update = hass.states.get("update.qsw_m408_4c_firmware_update")
update = hass.states.get("update.qsw_m408_4c_firmware")
assert update is not None
assert update.state == STATE_ON
assert (
@ -62,7 +62,7 @@ async def test_qnap_qsw_update(hass: HomeAssistant) -> None:
SERVICE_INSTALL,
{
ATTR_BACKUP: False,
ATTR_ENTITY_ID: "update.qsw_m408_4c_firmware_update",
ATTR_ENTITY_ID: "update.qsw_m408_4c_firmware",
},
blocking=True,
)
@ -71,7 +71,7 @@ async def test_qnap_qsw_update(hass: HomeAssistant) -> None:
mock_firmware_update_live.assert_called_once()
mock_users_verification.assert_called()
update = hass.states.get("update.qsw_m408_4c_firmware_update")
update = hass.states.get("update.qsw_m408_4c_firmware")
assert update is not None
assert update.state == STATE_OFF
assert (