2017-02-03 07:43:03 +00:00
|
|
|
"""The test for the moon sensor platform."""
|
|
|
|
from datetime import datetime
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
from homeassistant.components.homeassistant import (
|
|
|
|
DOMAIN as HA_DOMAIN,
|
|
|
|
SERVICE_UPDATE_ENTITY,
|
|
|
|
)
|
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
|
|
|
from homeassistant.setup import async_setup_component
|
2019-12-09 13:20:40 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
DAY1 = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC)
|
|
|
|
DAY2 = datetime(2017, 1, 18, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_moon_day1(hass):
|
2017-02-03 07:43:03 +00:00
|
|
|
"""Test the Moon sensor."""
|
2020-06-29 16:39:24 +00:00
|
|
|
config = {"sensor": {"platform": "moon", "name": "moon_day1"}}
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
await async_setup_component(hass, HA_DOMAIN, {})
|
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
assert hass.states.get("sensor.moon_day1")
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.moon.sensor.dt_util.utcnow", return_value=DAY1
|
|
|
|
):
|
|
|
|
await async_update_entity(hass, "sensor.moon_day1")
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
assert hass.states.get("sensor.moon_day1").state == "waxing_crescent"
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_moon_day2(hass):
|
|
|
|
"""Test the Moon sensor."""
|
|
|
|
config = {"sensor": {"platform": "moon", "name": "moon_day2"}}
|
|
|
|
|
|
|
|
await async_setup_component(hass, HA_DOMAIN, {})
|
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.moon_day2")
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.moon.sensor.dt_util.utcnow", return_value=DAY2
|
|
|
|
):
|
|
|
|
await async_update_entity(hass, "sensor.moon_day2")
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.moon_day2").state == "waning_gibbous"
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def async_update_entity(hass, entity_id):
|
|
|
|
"""Run an update action for an entity."""
|
|
|
|
await hass.services.async_call(
|
2020-08-27 11:56:20 +00:00
|
|
|
HA_DOMAIN,
|
|
|
|
SERVICE_UPDATE_ENTITY,
|
|
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
|
|
blocking=True,
|
2020-06-29 16:39:24 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|