mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
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:
parent
b104680c6d
commit
5e55f83cbc
@ -1,7 +1,11 @@
|
|||||||
"""IMAP sensor support."""
|
"""IMAP sensor support."""
|
||||||
from __future__ import annotations
|
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.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_USERNAME
|
from homeassistant.const import CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -13,6 +17,12 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|||||||
from . import ImapPollingDataUpdateCoordinator, ImapPushDataUpdateCoordinator
|
from . import ImapPollingDataUpdateCoordinator, ImapPushDataUpdateCoordinator
|
||||||
from .const import DOMAIN
|
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(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
@ -22,8 +32,7 @@ async def async_setup_entry(
|
|||||||
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator = (
|
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator = (
|
||||||
hass.data[DOMAIN][entry.entry_id]
|
hass.data[DOMAIN][entry.entry_id]
|
||||||
)
|
)
|
||||||
|
async_add_entities([ImapSensor(coordinator, IMAP_MAIL_COUNT_DESCRIPTION)])
|
||||||
async_add_entities([ImapSensor(coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class ImapSensor(
|
class ImapSensor(
|
||||||
@ -38,9 +47,11 @@ class ImapSensor(
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator,
|
coordinator: ImapPushDataUpdateCoordinator | ImapPollingDataUpdateCoordinator,
|
||||||
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
self.entity_description = description
|
||||||
self._attr_unique_id = f"{coordinator.config_entry.entry_id}"
|
self._attr_unique_id = f"{coordinator.config_entry.entry_id}"
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
|
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
|
||||||
|
@ -9,6 +9,7 @@ import pytest
|
|||||||
|
|
||||||
from homeassistant.components.imap import DOMAIN
|
from homeassistant.components.imap import DOMAIN
|
||||||
from homeassistant.components.imap.errors import InvalidAuth, InvalidFolder
|
from homeassistant.components.imap.errors import InvalidAuth, InvalidFolder
|
||||||
|
from homeassistant.components.sensor.const import SensorStateClass
|
||||||
from homeassistant.const import STATE_UNAVAILABLE
|
from homeassistant.const import STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
@ -135,6 +136,7 @@ async def test_receiving_message_successfully(
|
|||||||
# we should have received one message
|
# we should have received one message
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state == "1"
|
assert state.state == "1"
|
||||||
|
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
# we should have received one event
|
# we should have received one event
|
||||||
assert len(event_called) == 1
|
assert len(event_called) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user