Add entity translations and entity category for IMAP mail count sensor (#131152)

* Add entity translations and entity category for IMAP mail count sensor

* Update tests

* Support unit_of_measurement

* Add unit_of_measurement
This commit is contained in:
Jan Bouwhuis 2024-11-22 15:03:24 +01:00 committed by GitHub
parent ae592a0c35
commit 384b2af31e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 24 deletions

View File

@ -7,7 +7,7 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import CONF_USERNAME
from homeassistant.const import CONF_USERNAME, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -19,10 +19,10 @@ from .coordinator import ImapDataUpdateCoordinator
IMAP_MAIL_COUNT_DESCRIPTION = SensorEntityDescription(
key="imap_mail_count",
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=0,
translation_key="imap_mail_count",
name=None,
)

View File

@ -35,6 +35,14 @@
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
}
},
"entity": {
"sensor": {
"imap_mail_count": {
"name": "Messages",
"unit_of_measurement": "messages"
}
}
},
"exceptions": {
"copy_failed": {
"message": "Copying the message failed with \"{error}\"."

View File

@ -41,7 +41,7 @@ async def test_entry_diagnostics(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"

View File

@ -153,7 +153,7 @@ async def test_receiving_message_successfully(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"
@ -202,7 +202,7 @@ async def test_receiving_message_with_invalid_encoding(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"
@ -237,7 +237,7 @@ async def test_receiving_message_no_subject_to_from(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"
@ -273,7 +273,7 @@ async def test_initial_authentication_error(
assert await hass.config_entries.async_setup(config_entry.entry_id) == success
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
assert (state is not None) == success
@ -290,7 +290,7 @@ async def test_initial_invalid_folder_error(
assert await hass.config_entries.async_setup(config_entry.entry_id) == success
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
assert (state is not None) == success
@ -330,7 +330,7 @@ async def test_late_authentication_retry(
assert "Authentication failed, retrying" in caplog.text
# we still should have an entity with an unavailable state
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
assert state is not None
assert state.state == STATE_UNAVAILABLE
@ -371,7 +371,7 @@ async def test_late_authentication_error(
assert "Username or password incorrect, starting reauthentication" in caplog.text
# we still should have an entity with an unavailable state
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
assert state is not None
assert state.state == STATE_UNAVAILABLE
@ -415,7 +415,7 @@ async def test_late_folder_error(
assert "Selected mailbox folder is invalid" in caplog.text
# we still should have an entity with an unavailable state
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
assert state is not None
assert state.state == STATE_UNAVAILABLE
@ -444,7 +444,7 @@ async def test_handle_cleanup_exception(
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have an entity
assert state is not None
assert state.state == "0"
@ -456,7 +456,7 @@ async def test_handle_cleanup_exception(
await hass.async_block_till_done()
assert "Error while cleaning up imap connection" in caplog.text
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have an entity with an unavailable state
assert state is not None
@ -487,7 +487,7 @@ async def test_lost_connection_with_imap_push(
await hass.async_block_till_done()
assert "Lost imap.server.com (will attempt to reconnect after 10 s)" in caplog.text
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# Our entity should keep its current state as this
assert state is not None
assert state.state == "0"
@ -511,7 +511,7 @@ async def test_fetch_number_of_messages(
await hass.async_block_till_done()
assert "Invalid response for search" in caplog.text
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have an entity with an unavailable state
assert state is not None
assert state.state == STATE_UNAVAILABLE
@ -556,7 +556,7 @@ async def test_reset_last_message(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# We should have received one message
assert state is not None
assert state.state == "1"
@ -590,7 +590,7 @@ async def test_reset_last_message(
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# We should have message
assert state is not None
assert state.state == "0"
@ -607,7 +607,7 @@ async def test_reset_last_message(
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# We should have received one message
assert state is not None
assert state.state == "1"
@ -637,7 +637,7 @@ async def test_event_skipped_message_too_large(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# We should have received one message
assert state is not None
assert state.state == "1"
@ -667,7 +667,7 @@ async def test_message_is_truncated(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# We should have received one message
assert state is not None
assert state.state == "1"
@ -702,7 +702,7 @@ async def test_message_data(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# We should have received one message
assert state is not None
assert state.state == "1"
@ -747,7 +747,7 @@ async def test_custom_template(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"
@ -798,7 +798,7 @@ async def test_enforce_polling(
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"
@ -838,7 +838,7 @@ async def test_services(hass: HomeAssistant, mock_imap_protocol: MagicMock) -> N
# Make sure we have had one update (when polling)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=5))
await hass.async_block_till_done()
state = hass.states.get("sensor.imap_email_email_com")
state = hass.states.get("sensor.imap_email_email_com_messages")
# we should have received one message
assert state is not None
assert state.state == "1"