Skip to content

Commit

Permalink
Correct imap sensor measurement class and add suggested precision (#9…
Browse files Browse the repository at this point in the history
…4060)

* Fix imap sensor measurement class and precision

* Test measurement class is set correctly

* Remove unrelated changes

* Move EntityDescription to module level
  • Loading branch information
jbouwh authored Jun 15, 2023
1 parent b104680 commit 5e55f83
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 14 additions & 3 deletions homeassistant/components/imap/sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +17,12 @@
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
Expand All @@ -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(
Expand All @@ -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)},
Expand Down
2 changes: 2 additions & 0 deletions tests/components/imap/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e55f83

Please sign in to comment.