mirror of
https://github.com/home-assistant/core.git
synced 2026-08-28 02:24:46 -05:00
Add icon translations for Blebox integration (#172565)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import blebox_uniapi.button
|
||||
|
||||
from homeassistant.components.button import ButtonEntity
|
||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
@@ -13,6 +13,16 @@ from .util import blebox_command
|
||||
|
||||
PARALLEL_UPDATES = 1
|
||||
|
||||
BUTTON_TYPES: dict[str, ButtonEntityDescription] = {
|
||||
"up": ButtonEntityDescription(key="up", translation_key="up"),
|
||||
"down": ButtonEntityDescription(key="down", translation_key="down"),
|
||||
"fav": ButtonEntityDescription(key="fav", translation_key="fav"),
|
||||
"open": ButtonEntityDescription(key="open", translation_key="open"),
|
||||
"close": ButtonEntityDescription(key="close", translation_key="close"),
|
||||
}
|
||||
|
||||
_DEFAULT_BUTTON = ButtonEntityDescription(key="button")
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
@@ -35,22 +45,16 @@ class BleBoxButtonEntity(BleBoxEntity[blebox_uniapi.button.Button], ButtonEntity
|
||||
self, coordinator: BleBoxCoordinator, feature: blebox_uniapi.button.Button
|
||||
) -> None:
|
||||
"""Initialize a BleBox button feature."""
|
||||
super().__init__(coordinator, feature)
|
||||
self._attr_icon = self.get_icon()
|
||||
|
||||
def get_icon(self) -> str | None:
|
||||
"""Return icon for endpoint."""
|
||||
if "up" in self._feature.query_string:
|
||||
return "mdi:arrow-up-circle"
|
||||
if "down" in self._feature.query_string:
|
||||
return "mdi:arrow-down-circle"
|
||||
if "fav" in self._feature.query_string:
|
||||
return "mdi:heart-circle"
|
||||
if "open" in self._feature.query_string:
|
||||
return "mdi:arrow-up-circle"
|
||||
if "close" in self._feature.query_string:
|
||||
return "mdi:arrow-down-circle"
|
||||
return None
|
||||
super().__init__(coordinator, feature)
|
||||
self.entity_description = self._get_description()
|
||||
|
||||
def _get_description(self) -> ButtonEntityDescription:
|
||||
"""Return the description matching this button's query string."""
|
||||
for key, description in BUTTON_TYPES.items():
|
||||
if key in self._feature.query_string:
|
||||
return description
|
||||
return _DEFAULT_BUTTON
|
||||
|
||||
@blebox_command
|
||||
async def async_press(self) -> None:
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"entity": {
|
||||
"button": {
|
||||
"close": {
|
||||
"default": "mdi:arrow-down-circle"
|
||||
},
|
||||
"down": {
|
||||
"default": "mdi:arrow-down-circle"
|
||||
},
|
||||
"fav": {
|
||||
"default": "mdi:heart-circle"
|
||||
},
|
||||
"open": {
|
||||
"default": "mdi:arrow-up-circle"
|
||||
},
|
||||
"up": {
|
||||
"default": "mdi:arrow-up-circle"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"power_consumption": {
|
||||
"default": "mdi:lightning-bolt"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,9 +57,9 @@ SENSOR_TYPES = (
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="powerConsumption",
|
||||
translation_key="power_consumption",
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
suggested_display_precision=2,
|
||||
icon="mdi:lightning-bolt",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="humidity",
|
||||
|
||||
@@ -6,17 +6,18 @@ from unittest.mock import PropertyMock
|
||||
import blebox_uniapi
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import ATTR_ICON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .conftest import async_setup_entity, mock_feature
|
||||
|
||||
query_icon_matching = [
|
||||
("up", "mdi:arrow-up-circle"),
|
||||
("down", "mdi:arrow-down-circle"),
|
||||
("fav", "mdi:heart-circle"),
|
||||
("open", "mdi:arrow-up-circle"),
|
||||
("close", "mdi:arrow-down-circle"),
|
||||
query_translation_key_matching = [
|
||||
("up", "up"),
|
||||
("down", "down"),
|
||||
("fav", "fav"),
|
||||
("open", "open"),
|
||||
("close", "close"),
|
||||
("unknown_action", None),
|
||||
]
|
||||
|
||||
|
||||
@@ -56,16 +57,28 @@ async def test_tvliftbox_init(
|
||||
assert state.name == "My tvLiftBox tvLiftBox-open_or_stop"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input", query_icon_matching)
|
||||
async def test_get_icon(
|
||||
input, tvliftbox, hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
@pytest.mark.parametrize(
|
||||
("query_string", "expected_translation_key"),
|
||||
query_translation_key_matching,
|
||||
ids=[q[0] for q in query_translation_key_matching],
|
||||
)
|
||||
async def test_button_translation_key(
|
||||
query_string: str,
|
||||
expected_translation_key: str | None,
|
||||
tvliftbox: tuple[blebox_uniapi.button.Button, str],
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test if proper icon is returned."""
|
||||
"""Test that the correct translation_key is assigned based on query_string."""
|
||||
caplog.set_level(logging.ERROR)
|
||||
|
||||
feature_mock, entity_id = tvliftbox
|
||||
feature_mock.query_string = input[0]
|
||||
_ = await async_setup_entity(hass, entity_id)
|
||||
state = hass.states.get(entity_id)
|
||||
feature_mock.query_string = query_string
|
||||
await async_setup_entity(hass, entity_id)
|
||||
|
||||
assert state.attributes[ATTR_ICON] == input[1]
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is not None
|
||||
|
||||
entity = er.async_get(hass).async_get(entity_id)
|
||||
assert entity is not None
|
||||
assert entity.translation_key == expected_translation_key
|
||||
|
||||
Reference in New Issue
Block a user