Correct imap sensor measurement class and add suggested precision (#94060)

* Fix imap sensor measurement class and precision

* Test measurement class is set correctly

* Remove unrelated changes

* Move EntityDescription to module level
This commit is contained in:
Jan Bouwhuis 2023-06-16 00:44:58 +02:00 committed by GitHub
parent b104680c6d
commit 5e55f83cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -1,7 +1,11 @@
"""IMAP sensor support."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME
from homeassistant.core import HomeAssistant
@ -13,6 +17,12 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import ImapPollingDataUpdateCoordinator, ImapPushDataUpdateCoordinator
from .const import DOMAIN
IMAP_MAIL_COUNT_DESCRIPTION = SensorEntityDescription(
key="imap_mail_count",
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=0,
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
@ -22,8 +32,7 @@ async def async_setup_entry(
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator = (
hass.data[DOMAIN][entry.entry_id]
)
async_add_entities([ImapSensor(coordinator)])
async_add_entities([ImapSensor(coordinator, IMAP_MAIL_COUNT_DESCRIPTION)])
class ImapSensor(
@ -38,9 +47,11 @@ class ImapSensor(
def __init__(
self,
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator,
description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{coordinator.config_entry.entry_id}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},

View File

@ -9,6 +9,7 @@ import pytest
from homeassistant.components.imap import DOMAIN
from homeassistant.components.imap.errors import InvalidAuth, InvalidFolder
from homeassistant.components.sensor.const import SensorStateClass
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.util.dt import utcnow
@ -135,6 +136,7 @@ async def test_receiving_message_successfully(
# we should have received one message
assert state is not None
assert state.state == "1"
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT
# we should have received one event
assert len(event_called) == 1