hass-core/tests/components/sensibo/test_button.py
epenet 7bc2712142
Adjust root-import in tests (#78761)
* Adjust root-import in tests

* Adjust diagnostics

* Adjust button

* Adjust select

* Adjust device_tracker

* Adjust camera

* Adjust humidifier

* Adjust media_source

* Adjust update

* Adjust siren

* Adjust number

* Adjust alarm_control_panel

* Adjust notify

* Adjust sensor

* Adjust switch

* Revert anthemav

* Don't adjust demo humidifier
2022-09-19 15:22:23 +02:00

109 lines
3.6 KiB
Python

"""The test for the sensibo button platform."""
from __future__ import annotations
from datetime import datetime, timedelta
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from pysensibo.model import SensiboData
from pytest import MonkeyPatch, raises
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util import dt
from tests.common import async_fire_time_changed
async def test_button(
hass: HomeAssistant,
load_int: ConfigEntry,
monkeypatch: MonkeyPatch,
get_data: SensiboData,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the Sensibo button."""
state_button = hass.states.get("button.hallway_reset_filter")
state_filter_clean = hass.states.get("binary_sensor.hallway_filter_clean_required")
state_filter_last_reset = hass.states.get("sensor.hallway_filter_last_reset")
assert state_button.state is STATE_UNKNOWN
assert state_filter_clean.state is STATE_ON
assert state_filter_last_reset.state == "2022-03-12T15:24:26+00:00"
today = datetime(datetime.now().year + 1, 6, 19, 20, 0, 0).replace(tzinfo=dt.UTC)
today_str = today.isoformat()
freezer.move_to(today)
with patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices_data",
return_value=get_data,
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_reset_filter",
return_value={"status": "success"},
):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{
ATTR_ENTITY_ID: state_button.entity_id,
},
blocking=True,
)
await hass.async_block_till_done()
monkeypatch.setattr(get_data.parsed["ABC999111"], "filter_clean", False)
monkeypatch.setattr(
get_data.parsed["ABC999111"],
"filter_last_reset",
today,
)
with patch(
"homeassistant.components.sensibo.coordinator.SensiboClient.async_get_devices_data",
return_value=get_data,
):
async_fire_time_changed(
hass,
dt.utcnow() + timedelta(minutes=5),
)
await hass.async_block_till_done()
state_button = hass.states.get("button.hallway_reset_filter")
state_filter_clean = hass.states.get("binary_sensor.hallway_filter_clean_required")
state_filter_last_reset = hass.states.get("sensor.hallway_filter_last_reset")
assert state_button.state == today_str
assert state_filter_clean.state is STATE_OFF
assert state_filter_last_reset.state == today_str
async def test_button_failure(
hass: HomeAssistant,
load_int: ConfigEntry,
monkeypatch: MonkeyPatch,
get_data: SensiboData,
) -> None:
"""Test the Sensibo button fails."""
state_button = hass.states.get("button.hallway_reset_filter")
with patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices_data",
return_value=get_data,
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_reset_filter",
return_value={"status": "failure"},
):
with raises(HomeAssistantError):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{
ATTR_ENTITY_ID: state_button.entity_id,
},
blocking=True,
)