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
parent
d7f0b904d0
commit
845bcf3f7d
|
@ -16,6 +16,8 @@ from .subscriber import HarmonyCallback
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
TRANSLATABLE_POWER_OFF = "power_off"
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
@ -31,6 +33,8 @@ async def async_setup_entry(
|
||||||
class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
|
class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
|
||||||
"""Select representation of a Harmony activities."""
|
"""Select representation of a Harmony activities."""
|
||||||
|
|
||||||
|
_attr_device_class = f"{DOMAIN}__activities"
|
||||||
|
|
||||||
def __init__(self, name: str, data: HarmonyData) -> None:
|
def __init__(self, name: str, data: HarmonyData) -> None:
|
||||||
"""Initialize HarmonyActivitySelect class."""
|
"""Initialize HarmonyActivitySelect class."""
|
||||||
super().__init__(data=data)
|
super().__init__(data=data)
|
||||||
|
@ -42,23 +46,27 @@ class HarmonyActivitySelect(HarmonyEntity, SelectEntity):
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
"""Return a representative icon."""
|
"""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-off"
|
||||||
return "mdi:remote-tv"
|
return "mdi:remote-tv"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def options(self) -> list[str]:
|
def options(self) -> list[str]:
|
||||||
"""Return a set of selectable options."""
|
"""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
|
@property
|
||||||
def current_option(self) -> str | None:
|
def current_option(self) -> str | None:
|
||||||
"""Return the current activity."""
|
"""Return the current activity."""
|
||||||
_, activity_name = self._data.current_activity
|
_, activity_name = self._data.current_activity
|
||||||
|
if activity_name == ACTIVITY_POWER_OFF:
|
||||||
|
return TRANSLATABLE_POWER_OFF
|
||||||
return activity_name
|
return activity_name
|
||||||
|
|
||||||
async def async_select_option(self, option: str) -> None:
|
async def async_select_option(self, option: str) -> None:
|
||||||
"""Change the current activity."""
|
"""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)
|
await self._data.async_start_activity(option)
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"state": {
|
||||||
|
"harmony__activities": {
|
||||||
|
"power_off": "Power Off"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"state": {
|
||||||
|
"harmony__activities": {
|
||||||
|
"power_off": "Power Off"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ from homeassistant.components.select import (
|
||||||
SERVICE_SELECT_OPTION,
|
SERVICE_SELECT_OPTION,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
ATTR_DEVICE_CLASS,
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
|
@ -68,11 +69,12 @@ async def test_options(mock_hc, hass, mock_write_config):
|
||||||
# assert we have all options
|
# assert we have all options
|
||||||
state = hass.states.get(ENTITY_SELECT)
|
state = hass.states.get(ENTITY_SELECT)
|
||||||
assert state.attributes.get("options") == [
|
assert state.attributes.get("options") == [
|
||||||
"PowerOff",
|
"power_off",
|
||||||
"Nile-TV",
|
"Nile-TV",
|
||||||
"Play Music",
|
"Play Music",
|
||||||
"Watch TV",
|
"Watch TV",
|
||||||
]
|
]
|
||||||
|
assert state.attributes.get(ATTR_DEVICE_CLASS) == "harmony__activities"
|
||||||
|
|
||||||
|
|
||||||
async def test_select_option(mock_hc, hass, mock_write_config):
|
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_REMOTE, STATE_ON)
|
||||||
assert hass.states.is_state(ENTITY_SELECT, "Play Music")
|
assert hass.states.is_state(ENTITY_SELECT, "Play Music")
|
||||||
|
|
||||||
# turn off harmony by selecting PowerOff activity
|
# turn off harmony by selecting power_off activity
|
||||||
await _select_option_and_wait(hass, ENTITY_SELECT, "PowerOff")
|
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_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):
|
async def _select_option_and_wait(hass, entity, option):
|
||||||
|
|
Loading…
Reference in New Issue