parent
ac594e6bce
commit
7390e3a997
9 changed files with 290 additions and 242 deletions
|
@ -1,119 +1,105 @@
|
|||
"""Tests for the IPP sensor platform."""
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from homeassistant.components.ipp.const import DOMAIN
|
||||
from homeassistant.components.sensor import (
|
||||
ATTR_OPTIONS as SENSOR_ATTR_OPTIONS,
|
||||
DOMAIN as SENSOR_DOMAIN,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.sensor import ATTR_OPTIONS
|
||||
from homeassistant.const import ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import init_integration, mock_connection
|
||||
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.freeze_time("2019-11-11 09:10:32+00:00")
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the creation and values of the IPP sensors."""
|
||||
mock_connection(aioclient_mock)
|
||||
|
||||
entry = await init_integration(hass, aioclient_mock, skip_setup=True)
|
||||
registry = er.async_get(hass)
|
||||
|
||||
# Pre-create registry entries for disabled by default sensors
|
||||
registry.async_get_or_create(
|
||||
SENSOR_DOMAIN,
|
||||
DOMAIN,
|
||||
"cfe92100-67c4-11d4-a45f-f8d027761251_uptime",
|
||||
suggested_object_id="epson_xp_6000_series_uptime",
|
||||
disabled_by=None,
|
||||
)
|
||||
|
||||
test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.components.ipp.sensor.utcnow", return_value=test_time):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series")
|
||||
state = hass.states.get("sensor.test_ha_1000_series")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:printer"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
|
||||
assert state.attributes.get(SENSOR_ATTR_OPTIONS) == ["idle", "printing", "stopped"]
|
||||
assert state.attributes.get(ATTR_OPTIONS) == ["idle", "printing", "stopped"]
|
||||
|
||||
entry = registry.async_get("sensor.epson_xp_6000_series")
|
||||
entry = entity_registry.async_get("sensor.test_ha_1000_series")
|
||||
assert entry
|
||||
assert entry.translation_key == "printer"
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_black_ink")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_black_ink")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:water"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is PERCENTAGE
|
||||
assert state.state == "58"
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_photo_black_ink")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_photo_black_ink")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:water"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is PERCENTAGE
|
||||
assert state.state == "98"
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_cyan_ink")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_cyan_ink")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:water"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is PERCENTAGE
|
||||
assert state.state == "91"
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_yellow_ink")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_yellow_ink")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:water"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is PERCENTAGE
|
||||
assert state.state == "95"
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_magenta_ink")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_magenta_ink")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:water"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is PERCENTAGE
|
||||
assert state.state == "73"
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_uptime")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_uptime")
|
||||
assert state
|
||||
assert state.attributes.get(ATTR_ICON) == "mdi:clock-outline"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
|
||||
assert state.state == "2019-10-26T15:37:00+00:00"
|
||||
assert state.state == "2019-11-11T09:10:02+00:00"
|
||||
|
||||
entry = registry.async_get("sensor.epson_xp_6000_series_uptime")
|
||||
entry = entity_registry.async_get("sensor.test_ha_1000_series_uptime")
|
||||
assert entry
|
||||
assert entry.unique_id == "cfe92100-67c4-11d4-a45f-f8d027761251_uptime"
|
||||
|
||||
|
||||
async def test_disabled_by_default_sensors(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the disabled by default IPP sensors."""
|
||||
await init_integration(hass, aioclient_mock)
|
||||
registry = er.async_get(hass)
|
||||
|
||||
state = hass.states.get("sensor.epson_xp_6000_series_uptime")
|
||||
state = hass.states.get("sensor.test_ha_1000_series_uptime")
|
||||
assert state is None
|
||||
|
||||
entry = registry.async_get("sensor.epson_xp_6000_series_uptime")
|
||||
entry = registry.async_get("sensor.test_ha_1000_series_uptime")
|
||||
assert entry
|
||||
assert entry.disabled
|
||||
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
|
||||
|
||||
|
||||
async def test_missing_entry_unique_id(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_ipp: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the unique_id of IPP sensor when printer is missing identifiers."""
|
||||
entry = await init_integration(hass, aioclient_mock, uuid=None, unique_id=None)
|
||||
mock_config_entry.unique_id = None
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
registry = er.async_get(hass)
|
||||
|
||||
entity = registry.async_get("sensor.epson_xp_6000_series")
|
||||
entity = registry.async_get("sensor.test_ha_1000_series")
|
||||
assert entity
|
||||
assert entity.unique_id == f"{entry.entry_id}_printer"
|
||||
assert entity.unique_id == f"{mock_config_entry.entry_id}_printer"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue