Use config entry title as sensor name in Filesize (#131109)

* Use config entry title as sensor name in Filesize

* snapshot

* snapshot
pull/131221/head
G Johansson 2024-11-21 21:36:54 +01:00 committed by GitHub
parent 88b54bbaf7
commit da023ffbd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 28 deletions

View File

@ -82,7 +82,6 @@ class FilesizeEntity(CoordinatorEntity[FileSizeCoordinator], SensorEntity):
) -> None:
"""Initialize the Filesize sensor."""
super().__init__(coordinator)
base_name = str(coordinator.path.absolute()).rsplit("/", maxsplit=1)[-1]
self._attr_unique_id = (
entry_id if description.key == "file" else f"{entry_id}-{description.key}"
)
@ -90,7 +89,6 @@ class FilesizeEntity(CoordinatorEntity[FileSizeCoordinator], SensorEntity):
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, entry_id)},
name=base_name,
)
@property

View File

@ -1,5 +1,5 @@
# serializer version: 1
# name: test_sensors[load_platforms0][sensor.file_txt_created-entry]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_created-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -11,7 +11,7 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.file_txt_created',
'entity_id': 'sensor.mock_file_test_filesize_txt_created',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
@ -32,21 +32,21 @@
'unit_of_measurement': None,
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_created-state]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_created-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'file.txt Created',
'friendly_name': 'mock_file_test_filesize.txt Created',
}),
'context': <ANY>,
'entity_id': 'sensor.file_txt_created',
'entity_id': 'sensor.mock_file_test_filesize_txt_created',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2024-11-20T18:19:04+00:00',
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_last_updated-entry]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_last_updated-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -58,7 +58,7 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.file_txt_last_updated',
'entity_id': 'sensor.mock_file_test_filesize_txt_last_updated',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
@ -79,21 +79,21 @@
'unit_of_measurement': None,
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_last_updated-state]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_last_updated-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'file.txt Last updated',
'friendly_name': 'mock_file_test_filesize.txt Last updated',
}),
'context': <ANY>,
'entity_id': 'sensor.file_txt_last_updated',
'entity_id': 'sensor.mock_file_test_filesize_txt_last_updated',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2024-11-20T18:19:24+00:00',
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_size-entry]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_size-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -107,7 +107,7 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.file_txt_size',
'entity_id': 'sensor.mock_file_test_filesize_txt_size',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
@ -128,23 +128,23 @@
'unit_of_measurement': <UnitOfInformation.MEGABYTES: 'MB'>,
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_size-state]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_size-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'data_size',
'friendly_name': 'file.txt Size',
'friendly_name': 'mock_file_test_filesize.txt Size',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfInformation.MEGABYTES: 'MB'>,
}),
'context': <ANY>,
'entity_id': 'sensor.file_txt_size',
'entity_id': 'sensor.mock_file_test_filesize_txt_size',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0.0',
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_size_in_bytes-entry]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_size_in_bytes-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -158,7 +158,7 @@
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.file_txt_size_in_bytes',
'entity_id': 'sensor.mock_file_test_filesize_txt_size_in_bytes',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
@ -179,16 +179,16 @@
'unit_of_measurement': <UnitOfInformation.BYTES: 'B'>,
})
# ---
# name: test_sensors[load_platforms0][sensor.file_txt_size_in_bytes-state]
# name: test_sensors[load_platforms0][sensor.mock_file_test_filesize_txt_size_in_bytes-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'data_size',
'friendly_name': 'file.txt Size in bytes',
'friendly_name': 'mock_file_test_filesize.txt Size in bytes',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfInformation.BYTES: 'B'>,
}),
'context': <ANY>,
'entity_id': 'sensor.file_txt_size_in_bytes',
'entity_id': 'sensor.mock_file_test_filesize_txt_size_in_bytes',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,

View File

@ -7,9 +7,10 @@ from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.filesize.const import DOMAIN
from homeassistant.const import CONF_FILE_PATH, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from . import TEST_FILE_NAME, async_create_file
@ -68,7 +69,10 @@ async def test_invalid_path(
async def test_valid_path(
hass: HomeAssistant, tmp_path: Path, mock_config_entry: MockConfigEntry
hass: HomeAssistant,
tmp_path: Path,
mock_config_entry: MockConfigEntry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test for a valid path."""
testfile = str(tmp_path.joinpath("file.txt"))
@ -82,10 +86,15 @@ async def test_valid_path(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.file_txt_size")
state = hass.states.get("sensor.mock_file_test_filesize_txt_size")
assert state
assert state.state == "0.0"
device = device_registry.async_get_device(
identifiers={(DOMAIN, mock_config_entry.entry_id)}
)
assert device.name == mock_config_entry.title
await hass.async_add_executor_job(os.remove, testfile)
@ -104,12 +113,12 @@ async def test_state_unavailable(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.file_txt_size")
state = hass.states.get("sensor.mock_file_test_filesize_txt_size")
assert state
assert state.state == "0.0"
await hass.async_add_executor_job(os.remove, testfile)
await async_update_entity(hass, "sensor.file_txt_size")
await async_update_entity(hass, "sensor.mock_file_test_filesize_txt_size")
state = hass.states.get("sensor.file_txt_size")
state = hass.states.get("sensor.mock_file_test_filesize_txt_size")
assert state.state == STATE_UNAVAILABLE