Update tests

This commit is contained in:
Oscar Calvo
2023-08-11 03:31:54 +00:00
parent c18b789cc3
commit c9f1263975
3 changed files with 28 additions and 62 deletions

View File

@@ -11,6 +11,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from .climate import CCM15Coordinator from .climate import CCM15Coordinator
from .const import DEFAULT_INTERVAL, DOMAIN from .const import DEFAULT_INTERVAL, DOMAIN
@@ -20,7 +21,7 @@ _LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema( STEP_USER_DATA_SCHEMA = vol.Schema(
{ {
vol.Required(CONF_HOST): str, vol.Required(CONF_HOST): str,
vol.Optional(CONF_PORT, default=80): int, vol.Optional(CONF_PORT, default=80): cv.port,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_INTERVAL): int, vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_INTERVAL): int,
} }
) )

View File

@@ -2,6 +2,7 @@
import unittest import unittest
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from ccm15 import CCM15DeviceState, CCM15SlaveDevice
import pytest import pytest
from homeassistant.components.ccm15 import CCM15Coordinator from homeassistant.components.ccm15 import CCM15Coordinator
@@ -17,9 +18,7 @@ from homeassistant.components.climate import (
ClimateEntityFeature, ClimateEntityFeature,
HVACMode, HVACMode,
) )
from homeassistant.const import ( from homeassistant.const import UnitOfTemperature
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
pytestmark = pytest.mark.usefixtures("mock_setup_entry") pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@@ -27,21 +26,32 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_coordinator(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: async def test_coordinator(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test the coordinator.""" """Test the coordinator."""
# Create a dictionary of CCM15SlaveDevice objects
ccm15_devices = {
0: CCM15SlaveDevice(bytes.fromhex("000000b0b8001b")),
1: CCM15SlaveDevice(bytes.fromhex("00000041c0001a")),
}
# Create an instance of the CCM15DeviceState class
device_state = CCM15DeviceState(devices=ccm15_devices)
with patch( with patch(
"homeassistant.components.ccm15.coordinator.CCM15Coordinator._fetch_xml_data", "ccm15.CCM15Device.CCM15Device.get_status_async",
return_value="<response><a0>000000b0b8001b,</a0><a1>00000041c0001a,</a1><a2>-</a2></response>", return_value=device_state,
): ):
coordinator = CCM15Coordinator("1.1.1.1", "80", 30, hass) coordinator = CCM15Coordinator("1.1.1.1", "80", 30, hass)
await coordinator.async_refresh() await coordinator.async_refresh()
data = coordinator.data
devices = coordinator.get_devices() data = coordinator.data
devices = coordinator.get_devices()
assert len(data.devices) == 2 assert len(data.devices) == 2
first_climate = data.devices[0] assert len(devices) == 2
first_climate = list(devices)[0]
assert first_climate is not None assert first_climate is not None
assert first_climate.temperature == 27 assert first_climate.temperature_unit == UnitOfTemperature.CELSIUS
assert first_climate.temperature_setpoint == 23 assert first_climate.current_temperature == 27
assert first_climate.unit == UnitOfTemperature.CELSIUS assert first_climate.target_temperature == 23
assert len(devices) == 2 assert len(devices) == 2
climate = next(iter(devices)) climate = next(iter(devices))
@@ -79,32 +89,32 @@ async def test_coordinator(hass: HomeAssistant, mock_setup_entry: AsyncMock) ->
assert climate.target_temperature_step == 1 assert climate.target_temperature_step == 1
with patch( with patch(
"homeassistant.components.ccm15.coordinator.CCM15Coordinator.async_send_state", "ccm15.CCM15Device.CCM15Device.async_send_state",
return_value=200, return_value=200,
): ):
await climate.async_set_fan_mode(FAN_HIGH) await climate.async_set_fan_mode(FAN_HIGH)
with patch( with patch(
"homeassistant.components.ccm15.coordinator.CCM15Coordinator.async_send_state", "ccm15.CCM15Device.CCM15Device.async_send_state",
return_value=200, return_value=200,
): ):
await climate.async_set_hvac_mode(HVACMode.COOL) await climate.async_set_hvac_mode(HVACMode.COOL)
with patch( with patch(
"homeassistant.components.ccm15.coordinator.CCM15Coordinator.async_send_state", "ccm15.CCM15Device.CCM15Device.async_send_state",
return_value=200, return_value=200,
): ):
await climate.async_set_temperature(ATTR_TEMPERATURE=25) await climate.async_set_temperature(ATTR_TEMPERATURE=25)
await climate.async_set_temperature(**{ATTR_TEMPERATURE: 25}) await climate.async_set_temperature(**{ATTR_TEMPERATURE: 25})
with patch( with patch(
"homeassistant.components.ccm15.coordinator.CCM15Coordinator.async_send_state", "ccm15.CCM15Device.CCM15Device.async_send_state",
return_value=200, return_value=200,
): ):
await climate.async_turn_off() await climate.async_turn_off()
with patch( with patch(
"homeassistant.components.ccm15.coordinator.CCM15Coordinator.async_send_state", "ccm15.CCM15Device.CCM15Device.async_send_state",
return_value=200, return_value=200,
): ):
await climate.async_turn_on() await climate.async_turn_on()

View File

@@ -1,45 +0,0 @@
"""Unit test for CCM15 climate component."""
import unittest
import homeassistant.components.ccm15 as ccm15
from homeassistant.const import (
UnitOfTemperature,
)
class TestCCM15SlaveDevice(unittest.TestCase):
"""Test the CCM15SlaveDevice class."""
def test_swing_mode_on(self) -> None:
"""Test that the swing mode is on."""
data = bytes.fromhex("00000041d2001a")
device = ccm15.data_model.CCM15SlaveDevice(data)
self.assertTrue(device.is_swing_on)
def test_swing_mode_off(self) -> None:
"""Test that the swing mode is off."""
data = bytes.fromhex("00000041d0001a")
device = ccm15.data_model.CCM15SlaveDevice(data)
self.assertFalse(device.is_swing_on)
def test_temp_fan_mode(self) -> None:
"""Test that the swing mode is on."""
data = bytes.fromhex("00000041d2001a")
device = ccm15.data_model.CCM15SlaveDevice(data)
self.assertEqual(26, device.temperature)
self.assertEqual(2, device.fan_mode)
self.assertEqual(0, device.ac_mode)
def test_fahrenheit(self) -> None:
"""Test that farenheith bit."""
data = bytearray.fromhex("81000041d2001a")
device = ccm15.data_model.CCM15SlaveDevice(data)
self.assertEqual(UnitOfTemperature.FAHRENHEIT, device.unit)
self.assertEqual(88, device.temperature_setpoint)
self.assertEqual(0, device.locked_cool_temperature)
self.assertEqual(0, device.locked_heat_temperature)
if __name__ == "__main__":
unittest.main()