mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Name unnamed update entities by their device class (#98579)
This commit is contained in:
parent
c268adb07e
commit
790523126e
@ -223,11 +223,6 @@
|
|||||||
"name": "Follow movement"
|
"name": "Follow movement"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"update": {
|
|
||||||
"firmware": {
|
|
||||||
"name": "[%key:component::update::entity_component::firmware::name%]"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"siren": {
|
"siren": {
|
||||||
"siren": {
|
"siren": {
|
||||||
"name": "[%key:component::siren::title%]"
|
"name": "[%key:component::siren::title%]"
|
||||||
|
@ -24,7 +24,6 @@ PARALLEL_UPDATES = 1
|
|||||||
|
|
||||||
UPDATE_ENTITY_TYPES = UpdateEntityDescription(
|
UPDATE_ENTITY_TYPES = UpdateEntityDescription(
|
||||||
key="version",
|
key="version",
|
||||||
translation_key="firmware",
|
|
||||||
device_class=UpdateDeviceClass.FIRMWARE,
|
device_class=UpdateDeviceClass.FIRMWARE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -131,11 +131,6 @@
|
|||||||
"litter_box": {
|
"litter_box": {
|
||||||
"name": "Litter box"
|
"name": "Litter box"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"update": {
|
|
||||||
"firmware": {
|
|
||||||
"name": "Firmware"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"services": {
|
"services": {
|
||||||
|
@ -24,7 +24,6 @@ SCAN_INTERVAL = timedelta(days=1)
|
|||||||
|
|
||||||
FIRMWARE_UPDATE_ENTITY = UpdateEntityDescription(
|
FIRMWARE_UPDATE_ENTITY = UpdateEntityDescription(
|
||||||
key="firmware",
|
key="firmware",
|
||||||
translation_key="firmware",
|
|
||||||
device_class=UpdateDeviceClass.FIRMWARE,
|
device_class=UpdateDeviceClass.FIRMWARE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -91,11 +91,6 @@
|
|||||||
"hot_days_extra_watering": {
|
"hot_days_extra_watering": {
|
||||||
"name": "Extra water on hot days"
|
"name": "Extra water on hot days"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"update": {
|
|
||||||
"firmware": {
|
|
||||||
"name": "Firmware"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"services": {
|
"services": {
|
||||||
|
@ -44,7 +44,6 @@ UPDATE_STATE_MAP = {
|
|||||||
|
|
||||||
UPDATE_DESCRIPTION = RainMachineEntityDescription(
|
UPDATE_DESCRIPTION = RainMachineEntityDescription(
|
||||||
key="update",
|
key="update",
|
||||||
translation_key="firmware",
|
|
||||||
api_category=DATA_MACHINE_FIRMWARE_UPDATE_STATUS,
|
api_category=DATA_MACHINE_FIRMWARE_UPDATE_STATUS,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -216,6 +216,13 @@ class UpdateEntity(RestoreEntity):
|
|||||||
"""Version installed and in use."""
|
"""Version installed and in use."""
|
||||||
return self._attr_installed_version
|
return self._attr_installed_version
|
||||||
|
|
||||||
|
def _default_to_device_class_name(self) -> bool:
|
||||||
|
"""Return True if an unnamed entity should be named by its device class.
|
||||||
|
|
||||||
|
For updates this is True if the entity has a device class.
|
||||||
|
"""
|
||||||
|
return self.device_class is not None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self) -> UpdateDeviceClass | None:
|
def device_class(self) -> UpdateDeviceClass | None:
|
||||||
"""Return the class of this entity."""
|
"""Return the class of this entity."""
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""The tests for the Update component."""
|
"""The tests for the Update component."""
|
||||||
|
from collections.abc import Generator
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -24,6 +25,7 @@ from homeassistant.components.update.const import (
|
|||||||
ATTR_TITLE,
|
ATTR_TITLE,
|
||||||
UpdateEntityFeature,
|
UpdateEntityFeature,
|
||||||
)
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
@ -34,12 +36,24 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, State, callback
|
from homeassistant.core import HomeAssistant, State, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import MockEntityPlatform, mock_restore_cache
|
from tests.common import (
|
||||||
|
MockConfigEntry,
|
||||||
|
MockEntityPlatform,
|
||||||
|
MockModule,
|
||||||
|
MockPlatform,
|
||||||
|
mock_config_flow,
|
||||||
|
mock_integration,
|
||||||
|
mock_platform,
|
||||||
|
mock_restore_cache,
|
||||||
|
)
|
||||||
from tests.typing import WebSocketGenerator
|
from tests.typing import WebSocketGenerator
|
||||||
|
|
||||||
|
TEST_DOMAIN = "test"
|
||||||
|
|
||||||
|
|
||||||
class MockUpdateEntity(UpdateEntity):
|
class MockUpdateEntity(UpdateEntity):
|
||||||
"""Mock UpdateEntity to use in tests."""
|
"""Mock UpdateEntity to use in tests."""
|
||||||
@ -752,3 +766,101 @@ async def test_release_notes_entity_does_not_support_release_notes(
|
|||||||
result = await client.receive_json()
|
result = await client.receive_json()
|
||||||
assert result["error"]["code"] == "not_supported"
|
assert result["error"]["code"] == "not_supported"
|
||||||
assert result["error"]["message"] == "Entity does not support release notes"
|
assert result["error"]["message"] == "Entity does not support release notes"
|
||||||
|
|
||||||
|
|
||||||
|
class MockFlow(ConfigFlow):
|
||||||
|
"""Test flow."""
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def config_flow_fixture(hass: HomeAssistant) -> Generator[None, None, None]:
|
||||||
|
"""Mock config flow."""
|
||||||
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
||||||
|
|
||||||
|
with mock_config_flow(TEST_DOMAIN, MockFlow):
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
async def test_name(hass: HomeAssistant) -> None:
|
||||||
|
"""Test update name."""
|
||||||
|
|
||||||
|
async def async_setup_entry_init(
|
||||||
|
hass: HomeAssistant, config_entry: ConfigEntry
|
||||||
|
) -> bool:
|
||||||
|
"""Set up test config entry."""
|
||||||
|
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
|
||||||
|
return True
|
||||||
|
|
||||||
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
||||||
|
mock_integration(
|
||||||
|
hass,
|
||||||
|
MockModule(
|
||||||
|
TEST_DOMAIN,
|
||||||
|
async_setup_entry=async_setup_entry_init,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Unnamed update entity without device class -> no name
|
||||||
|
entity1 = UpdateEntity()
|
||||||
|
entity1.entity_id = "update.test1"
|
||||||
|
|
||||||
|
# Unnamed update entity with device class but has_entity_name False -> no name
|
||||||
|
entity2 = UpdateEntity()
|
||||||
|
entity2.entity_id = "update.test2"
|
||||||
|
entity2._attr_device_class = UpdateDeviceClass.FIRMWARE
|
||||||
|
|
||||||
|
# Unnamed update entity with device class and has_entity_name True -> named
|
||||||
|
entity3 = UpdateEntity()
|
||||||
|
entity3.entity_id = "update.test3"
|
||||||
|
entity3._attr_device_class = UpdateDeviceClass.FIRMWARE
|
||||||
|
entity3._attr_has_entity_name = True
|
||||||
|
|
||||||
|
# Unnamed update entity with device class and has_entity_name True -> named
|
||||||
|
entity4 = UpdateEntity()
|
||||||
|
entity4.entity_id = "update.test4"
|
||||||
|
entity4.entity_description = UpdateEntityDescription(
|
||||||
|
"test",
|
||||||
|
UpdateDeviceClass.FIRMWARE,
|
||||||
|
has_entity_name=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_setup_entry_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up test update platform via config entry."""
|
||||||
|
async_add_entities([entity1, entity2, entity3, entity4])
|
||||||
|
|
||||||
|
mock_platform(
|
||||||
|
hass,
|
||||||
|
f"{TEST_DOMAIN}.{DOMAIN}",
|
||||||
|
MockPlatform(async_setup_entry=async_setup_entry_platform),
|
||||||
|
)
|
||||||
|
|
||||||
|
config_entry = MockConfigEntry(domain=TEST_DOMAIN)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(entity1.entity_id)
|
||||||
|
assert state
|
||||||
|
assert "device_class" not in state.attributes
|
||||||
|
assert "friendly_name" not in state.attributes
|
||||||
|
|
||||||
|
state = hass.states.get(entity2.entity_id)
|
||||||
|
assert state
|
||||||
|
assert state.attributes.get("device_class") == "firmware"
|
||||||
|
assert "friendly_name" not in state.attributes
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
"device_class": "firmware",
|
||||||
|
"friendly_name": "Firmware",
|
||||||
|
}
|
||||||
|
state = hass.states.get(entity3.entity_id)
|
||||||
|
assert state
|
||||||
|
assert expected.items() <= state.attributes.items()
|
||||||
|
|
||||||
|
state = hass.states.get(entity4.entity_id)
|
||||||
|
assert state
|
||||||
|
assert expected.items() <= state.attributes.items()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user