2017-02-03 07:43:03 +00:00
|
|
|
"""The test for the moon sensor platform."""
|
2022-02-22 13:59:59 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2022-02-22 13:59:59 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.moon.sensor import (
|
|
|
|
MOON_ICONS,
|
|
|
|
STATE_FIRST_QUARTER,
|
|
|
|
STATE_FULL_MOON,
|
|
|
|
STATE_LAST_QUARTER,
|
|
|
|
STATE_NEW_MOON,
|
|
|
|
STATE_WANING_CRESCENT,
|
|
|
|
STATE_WANING_GIBBOUS,
|
|
|
|
STATE_WAXING_CRESCENT,
|
|
|
|
STATE_WAXING_GIBBOUS,
|
|
|
|
)
|
2022-12-03 10:25:04 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_OPTIONS, SensorDeviceClass
|
|
|
|
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_FRIENDLY_NAME, ATTR_ICON
|
2022-02-22 13:59:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-20 09:24:15 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
2022-03-03 23:12:33 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
|
2022-02-22 13:59:59 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("moon_value", "native_value", "icon"),
|
2022-02-22 13:59:59 +00:00
|
|
|
[
|
|
|
|
(0, STATE_NEW_MOON, MOON_ICONS[STATE_NEW_MOON]),
|
|
|
|
(5, STATE_WAXING_CRESCENT, MOON_ICONS[STATE_WAXING_CRESCENT]),
|
|
|
|
(7, STATE_FIRST_QUARTER, MOON_ICONS[STATE_FIRST_QUARTER]),
|
|
|
|
(12, STATE_WAXING_GIBBOUS, MOON_ICONS[STATE_WAXING_GIBBOUS]),
|
|
|
|
(14.3, STATE_FULL_MOON, MOON_ICONS[STATE_FULL_MOON]),
|
|
|
|
(20.1, STATE_WANING_GIBBOUS, MOON_ICONS[STATE_WANING_GIBBOUS]),
|
|
|
|
(20.8, STATE_LAST_QUARTER, MOON_ICONS[STATE_LAST_QUARTER]),
|
|
|
|
(23, STATE_WANING_CRESCENT, MOON_ICONS[STATE_WANING_CRESCENT]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_moon_day(
|
2022-03-03 23:12:33 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
moon_value: float,
|
|
|
|
native_value: str,
|
|
|
|
icon: str,
|
2022-02-22 13:59:59 +00:00
|
|
|
) -> None:
|
2020-06-29 16:39:24 +00:00
|
|
|
"""Test the Moon sensor."""
|
2022-03-03 23:12:33 +00:00
|
|
|
mock_config_entry.add_to_hass(hass)
|
2020-06-29 16:39:24 +00:00
|
|
|
|
|
|
|
with patch(
|
2022-02-22 13:59:59 +00:00
|
|
|
"homeassistant.components.moon.sensor.moon.phase", return_value=moon_value
|
2020-06-29 16:39:24 +00:00
|
|
|
):
|
2022-03-03 23:12:33 +00:00
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2020-06-29 16:39:24 +00:00
|
|
|
|
2022-07-20 09:24:15 +00:00
|
|
|
state = hass.states.get("sensor.moon_phase")
|
2022-03-03 23:12:33 +00:00
|
|
|
assert state
|
2022-02-22 13:59:59 +00:00
|
|
|
assert state.state == native_value
|
2022-03-03 23:12:33 +00:00
|
|
|
assert state.attributes[ATTR_ICON] == icon
|
2022-07-20 09:24:15 +00:00
|
|
|
assert state.attributes[ATTR_FRIENDLY_NAME] == "Moon Phase"
|
2022-12-03 10:25:04 +00:00
|
|
|
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENUM
|
|
|
|
assert state.attributes[ATTR_OPTIONS] == [
|
|
|
|
STATE_FIRST_QUARTER,
|
|
|
|
STATE_FULL_MOON,
|
|
|
|
STATE_LAST_QUARTER,
|
|
|
|
STATE_NEW_MOON,
|
|
|
|
STATE_WANING_CRESCENT,
|
|
|
|
STATE_WANING_GIBBOUS,
|
|
|
|
STATE_WAXING_CRESCENT,
|
|
|
|
STATE_WAXING_GIBBOUS,
|
|
|
|
]
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2022-03-03 23:12:33 +00:00
|
|
|
entity_registry = er.async_get(hass)
|
2022-07-20 09:24:15 +00:00
|
|
|
entry = entity_registry.async_get("sensor.moon_phase")
|
2022-03-03 23:12:33 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == mock_config_entry.entry_id
|
2022-12-03 10:25:04 +00:00
|
|
|
assert entry.translation_key == "phase"
|
2022-07-20 09:24:15 +00:00
|
|
|
|
|
|
|
device_registry = dr.async_get(hass)
|
|
|
|
assert entry.device_id
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
|
|
assert device_entry
|
|
|
|
assert device_entry.name == "Moon"
|
|
|
|
assert device_entry.entry_type is dr.DeviceEntryType.SERVICE
|