Translate `PowerOff` state of `select` entity in Harmony integration (#77491)

* Add custom device_class for select entity

* Update tests

* Make the state PowerOff translatable

* Update strings.select.json file

* add select.en.json

Co-authored-by: J. Nick Koston <nick@koston.org>
pull/82606/head
Maciej Bieniek 2022-11-24 00:08:43 +01:00 committed by GitHub
parent d7f0b904d0
commit 845bcf3f7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 6 deletions

View File

@ -16,6 +16,8 @@ from .subscriber import HarmonyCallback
_LOGGER = logging.getLogger(__name__)
TRANSLATABLE_POWER_OFF = "power_off"
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
@ -31,6 +33,8 @@ async def async_setup_entry(
class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
"""Select representation of a Harmony activities."""
_attr_device_class = f"{DOMAIN}__activities"
def __init__(self, name: str, data: HarmonyData) -> None:
"""Initialize HarmonyActivitySelect class."""
super().__init__(data=data)
@ -42,23 +46,27 @@ class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
@property
def icon(self) -> str:
"""Return a representative icon."""
if not self.available or self.current_option == ACTIVITY_POWER_OFF:
if not self.available or self.current_option == TRANSLATABLE_POWER_OFF:
return "mdi:remote-tv-off"
return "mdi:remote-tv"
@property
def options(self) -> list[str]:
"""Return a set of selectable options."""
return [ACTIVITY_POWER_OFF] + sorted(self._data.activity_names)
return [TRANSLATABLE_POWER_OFF] + sorted(self._data.activity_names)
@property
def current_option(self) -> str | None:
"""Return the current activity."""
_, activity_name = self._data.current_activity
if activity_name == ACTIVITY_POWER_OFF:
return TRANSLATABLE_POWER_OFF
return activity_name
async def async_select_option(self, option: str) -> None:
"""Change the current activity."""
if option == TRANSLATABLE_POWER_OFF:
await self._data.async_start_activity(ACTIVITY_POWER_OFF)
await self._data.async_start_activity(option)
async def async_added_to_hass(self) -> None:

View File

@ -0,0 +1,7 @@
{
"state": {
"harmony__activities": {
"power_off": "Power Off"
}
}
}

View File

@ -0,0 +1,7 @@
{
"state": {
"harmony__activities": {
"power_off": "Power Off"
}
}
}

View File

@ -9,6 +9,7 @@ from homeassistant.components.select import (
SERVICE_SELECT_OPTION,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
CONF_HOST,
CONF_NAME,
@ -68,11 +69,12 @@ async def test_options(mock_hc, hass, mock_write_config):
# assert we have all options
state = hass.states.get(ENTITY_SELECT)
assert state.attributes.get("options") == [
"PowerOff",
"power_off",
"Nile-TV",
"Play Music",
"Watch TV",
]
assert state.attributes.get(ATTR_DEVICE_CLASS) == "harmony__activities"
async def test_select_option(mock_hc, hass, mock_write_config):
@ -94,10 +96,10 @@ async def test_select_option(mock_hc, hass, mock_write_config):
assert hass.states.is_state(ENTITY_REMOTE, STATE_ON)
assert hass.states.is_state(ENTITY_SELECT, "Play Music")
# turn off harmony by selecting PowerOff activity
await _select_option_and_wait(hass, ENTITY_SELECT, "PowerOff")
# turn off harmony by selecting power_off activity
await _select_option_and_wait(hass, ENTITY_SELECT, "power_off")
assert hass.states.is_state(ENTITY_REMOTE, STATE_OFF)
assert hass.states.is_state(ENTITY_SELECT, "PowerOff")
assert hass.states.is_state(ENTITY_SELECT, "power_off")
async def _select_option_and_wait(hass, entity, option):