Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 21 additions & 116 deletions tests/components/blebox/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,128 +135,58 @@ async def test_update(saunabox, hass: HomeAssistant, config) -> None:
assert state.state == HVACMode.OFF


async def test_on_when_below_desired(saunabox, hass: HomeAssistant) -> None:
"""Test when temperature is below desired."""
async def test_set_hvac_mode_heat(saunabox, hass: HomeAssistant) -> None:
"""Test that setting HVAC mode to heat calls async_on."""

feature_mock, entity_id = saunabox

feature_mock.is_on = False
await async_setup_entity(hass, entity_id)

def turn_on():
feature_mock.is_on = True
feature_mock.is_heating = True
feature_mock.desired = 64.8
feature_mock.current = 25.7

feature_mock.mode = 1
feature_mock.async_on = AsyncMock(side_effect=turn_on)
await hass.services.async_call(
"climate",
SERVICE_SET_HVAC_MODE,
{"entity_id": entity_id, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True,
)
feature_mock.async_off.assert_not_called()
state = hass.states.get(entity_id)

assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING
assert state.attributes[ATTR_TEMPERATURE] == 64.8
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 25.7
assert state.state == HVACMode.HEAT


async def test_on_when_above_desired(saunabox, hass: HomeAssistant) -> None:
"""Test when temperature is below desired."""

feature_mock, entity_id = saunabox

feature_mock.is_on = False
await async_setup_entity(hass, entity_id)

def turn_on():
feature_mock.is_on = True
feature_mock.is_heating = False
feature_mock.desired = 23.4
feature_mock.current = 28.7

feature_mock.mode = 1
feature_mock.async_on = AsyncMock(side_effect=turn_on)

await hass.services.async_call(
"climate",
SERVICE_SET_HVAC_MODE,
{ATTR_ENTITY_ID: entity_id, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True,
)
feature_mock.async_off.assert_not_called()
state = hass.states.get(entity_id)

assert state.attributes[ATTR_TEMPERATURE] == 23.4
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 28.7
assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.IDLE
assert state.state == HVACMode.HEAT
feature_mock.async_on.assert_called_once_with()
feature_mock.async_off.assert_not_called()


async def test_off(saunabox, hass: HomeAssistant) -> None:
"""Test turning off."""
async def test_set_hvac_mode_off(saunabox, hass: HomeAssistant) -> None:
"""Test that setting HVAC mode to off calls async_off."""

feature_mock, entity_id = saunabox

feature_mock.is_on = True
feature_mock.is_heating = False
await async_setup_entity(hass, entity_id)

def turn_off():
feature_mock.is_on = False
feature_mock.is_heating = False
feature_mock.desired = 29.8
feature_mock.current = 22.7

feature_mock.async_off = AsyncMock(side_effect=turn_off)
await hass.services.async_call(
"climate",
SERVICE_SET_HVAC_MODE,
{"entity_id": entity_id, ATTR_HVAC_MODE: HVACMode.OFF},
blocking=True,
)
feature_mock.async_on.assert_not_called()
state = hass.states.get(entity_id)

assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.OFF
assert state.attributes[ATTR_TEMPERATURE] == 29.8
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 22.7
assert state.state == HVACMode.OFF
feature_mock.async_off.assert_called_once_with()
feature_mock.async_on.assert_not_called()


async def test_set_thermo(saunabox, hass: HomeAssistant) -> None:
"""Test setting thermostat."""
"""Test that setting the temperature calls async_set_temperature."""

feature_mock, entity_id = saunabox

feature_mock.is_on = False
feature_mock.is_heating = False
await async_setup_entity(hass, entity_id)

def set_temp(temp):
feature_mock.is_on = True
feature_mock.is_heating = True
feature_mock.desired = 29.2
feature_mock.current = 29.1

feature_mock.async_set_temperature = AsyncMock(side_effect=set_temp)
await hass.services.async_call(
"climate",
SERVICE_SET_TEMPERATURE,
{"entity_id": entity_id, ATTR_TEMPERATURE: 43.21},
blocking=True,
)
state = hass.states.get(entity_id)

assert state.attributes[ATTR_TEMPERATURE] == 29.2
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 29.1
assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING
assert state.state == HVACMode.HEAT
feature_mock.async_set_temperature.assert_called_once_with(43.21)


async def test_update_failure(
Expand All @@ -280,55 +210,30 @@ async def test_update_failure(
assert config_entry.state is ConfigEntryState.SETUP_RETRY


async def test_reding_hvac_actions(
saunabox, hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test hvac action for given device(mock) state."""

caplog.set_level(logging.ERROR)
async def test_hvac_action_heating(saunabox, hass: HomeAssistant) -> None:
"""Test hvac_action reflects a heating device state."""

feature_mock, entity_id = saunabox
await async_setup_entity(hass, entity_id)

def set_temperature(temp):
feature_mock.is_on = True
feature_mock.hvac_action = 1
feature_mock.mode = 1

feature_mock.async_set_temperature = AsyncMock(side_effect=set_temperature)
feature_mock.is_on = True
feature_mock.hvac_action = 1
feature_mock.mode = 1
await async_setup_entity(hass, entity_id)

await hass.services.async_call(
"climate",
SERVICE_SET_TEMPERATURE,
{"entity_id": entity_id, ATTR_TEMPERATURE: 43.21},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING
assert state.attributes[ATTR_HVAC_MODES] == [HVACMode.OFF, HVACMode.HEAT]


async def test_thermo_off(
thermobox, hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test hvac action off fir given device state."""
caplog.set_level(logging.ERROR)
async def test_hvac_action_off(thermobox, hass: HomeAssistant) -> None:
"""Test hvac_action reflects a device that is off."""

feature_mock, entity_id = thermobox
await async_setup_entity(hass, entity_id)

def set_off():
feature_mock.is_on = False
feature_mock.hvac_action = 0

feature_mock.async_off = AsyncMock(side_effect=set_off)
feature_mock.is_on = False
feature_mock.hvac_action = 0
await async_setup_entity(hass, entity_id)

await hass.services.async_call(
"climate",
SERVICE_SET_HVAC_MODE,
{"entity_id": entity_id, ATTR_HVAC_MODE: HVACMode.OFF},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.OFF
assert state.attributes[ATTR_HVAC_MODES] == [HVACMode.OFF, HVACMode.COOL]
78 changes: 17 additions & 61 deletions tests/components/blebox/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,16 @@ async def test_open(feature, hass: HomeAssistant) -> None:

feature_mock, entity_id = feature

feature_mock.state = 3 # manually stopped
await async_setup_entity(hass, entity_id)
assert hass.states.get(entity_id).state == CoverState.CLOSED

def open_gate():
feature_mock.state = 1 # opening

feature_mock.async_open = AsyncMock(side_effect=open_gate)
await hass.services.async_call(
"cover",
SERVICE_OPEN_COVER,
{"entity_id": entity_id},
blocking=True,
)
assert hass.states.get(entity_id).state == CoverState.OPENING

feature_mock.async_open.assert_called_once_with()


@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
Expand All @@ -282,18 +277,13 @@ async def test_close(feature, hass: HomeAssistant) -> None:

feature_mock, entity_id = feature

feature_mock.state = 4 # open
await async_setup_entity(hass, entity_id)
assert hass.states.get(entity_id).state == CoverState.OPEN

def close():
feature_mock.state = 0 # closing

feature_mock.async_close = AsyncMock(side_effect=close)
await hass.services.async_call(
"cover", SERVICE_CLOSE_COVER, {"entity_id": entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == CoverState.CLOSING

feature_mock.async_close.assert_called_once_with()


@pytest.mark.parametrize("feature", FIXTURES_SUPPORTING_STOP, indirect=["feature"])
Expand All @@ -302,18 +292,13 @@ async def test_stop(feature, hass: HomeAssistant) -> None:

feature_mock, entity_id = feature

feature_mock.state = 1 # opening
await async_setup_entity(hass, entity_id)
assert hass.states.get(entity_id).state == CoverState.OPENING

def stop():
feature_mock.state = 2 # manually stopped

feature_mock.async_stop = AsyncMock(side_effect=stop)
await hass.services.async_call(
"cover", SERVICE_STOP_COVER, {"entity_id": entity_id}, blocking=True
)
assert hass.states.get(entity_id).state == CoverState.OPEN

feature_mock.async_stop.assert_called_once_with()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -355,23 +340,16 @@ async def test_set_position(feature, hass: HomeAssistant) -> None:

feature_mock, entity_id = feature

feature_mock.state = 3 # closed
await async_setup_entity(hass, entity_id)
assert hass.states.get(entity_id).state == CoverState.CLOSED

def set_position(position):
assert position == 99 # inverted
feature_mock.state = 1 # opening
# feature_mock.current = position

feature_mock.async_set_position = AsyncMock(side_effect=set_position)
await hass.services.async_call(
"cover",
SERVICE_SET_COVER_POSITION,
{"entity_id": entity_id, ATTR_POSITION: 1},
blocking=True,
) # almost closed
assert hass.states.get(entity_id).state == CoverState.OPENING

feature_mock.async_set_position.assert_called_once_with(99) # inverted


async def test_unknown_position(shutterbox, hass: HomeAssistant) -> None:
Expand Down Expand Up @@ -540,80 +518,58 @@ async def test_set_tilt_position(shutterbox, hass: HomeAssistant) -> None:

feature_mock, entity_id = shutterbox

feature_mock.state = 3
await async_setup_entity(hass, entity_id)
assert hass.states.get(entity_id).state == CoverState.CLOSED

def set_tilt(tilt_position):
assert tilt_position == 20
feature_mock.state = 1

feature_mock.async_set_tilt_position = AsyncMock(side_effect=set_tilt)
await hass.services.async_call(
"cover",
SERVICE_SET_COVER_TILT_POSITION,
{"entity_id": entity_id, ATTR_TILT_POSITION: 80},
blocking=True,
)
assert hass.states.get(entity_id).state == CoverState.OPENING

feature_mock.async_set_tilt_position.assert_called_once_with(20)


@pytest.mark.parametrize(
("is_tilt_180", "expected_tilt_position", "expected_tilt_reported"),
("is_tilt_180", "expected_tilt_position"),
[
pytest.param(False, 0, 100, id="tilt_90"),
pytest.param(True, 50, 50, id="tilt_180"),
pytest.param(False, 0, id="tilt_90"),
pytest.param(True, 50, id="tilt_180"),
],
)
async def test_open_tilt(
shutterbox,
hass: HomeAssistant,
is_tilt_180: bool,
expected_tilt_position: int,
expected_tilt_reported: int,
) -> None:
"""Test opening tilt for 90-degree and 180-degree tilt shutters."""
feature_mock, entity_id = shutterbox
feature_mock.is_tilt_180 = is_tilt_180
feature_mock.tilt_current = 100
await async_setup_entity(hass, entity_id)

def set_tilt_position(tilt_position):
assert tilt_position == expected_tilt_position
feature_mock.tilt_current = tilt_position

feature_mock.async_set_tilt_position = AsyncMock(side_effect=set_tilt_position)

await hass.services.async_call(
"cover",
SERVICE_OPEN_COVER_TILT,
{"entity_id": entity_id},
blocking=True,
)
state = hass.states.get(entity_id)
assert (
state.attributes[ATTR_CURRENT_TILT_POSITION] == expected_tilt_reported
) # inverted

feature_mock.async_set_tilt_position.assert_called_once_with(expected_tilt_position)


async def test_close_tilt(shutterbox, hass: HomeAssistant) -> None:
"""Test closing tilt."""
feature_mock, entity_id = shutterbox

feature_mock.tilt_current = 0
await async_setup_entity(hass, entity_id)

def set_tilt_position(tilt_position):
assert tilt_position == 100
feature_mock.tilt_current = tilt_position

feature_mock.async_set_tilt_position = AsyncMock(side_effect=set_tilt_position)

await hass.services.async_call(
"cover",
SERVICE_CLOSE_COVER_TILT,
{"entity_id": entity_id},
blocking=True,
)
state = hass.states.get(entity_id)
assert state.attributes[ATTR_CURRENT_TILT_POSITION] == 0 # inverted

feature_mock.async_set_tilt_position.assert_called_once_with(100)
Loading
Loading