diff --git a/tests/components/velbus/conftest.py b/tests/components/velbus/conftest.py index 0413a2d1af764..5092210dfba54 100644 --- a/tests/components/velbus/conftest.py +++ b/tests/components/velbus/conftest.py @@ -4,7 +4,16 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest -from velbusaio.channels import Button, Relay, SelectedProgram, Temperature +from velbusaio.channels import ( + Button, + ButtonCounter, + Dimmer, + LightSensor, + Relay, + SelectedProgram, + SensorNumber, + Temperature, +) from homeassistant.components.velbus import VelbusConfigEntry from homeassistant.components.velbus.const import DOMAIN @@ -22,6 +31,10 @@ def mock_controller( mock_relay: AsyncMock, mock_temperature: AsyncMock, mock_select: AsyncMock, + mock_buttoncounter: AsyncMock, + mock_sensornumber: AsyncMock, + mock_lightsensor: AsyncMock, + mock_dimmer: AsyncMock, ) -> Generator[AsyncMock]: """Mock a successful velbus controller.""" with ( @@ -37,6 +50,14 @@ def mock_controller( cont.get_all_switch.return_value = [mock_relay] cont.get_all_climate.return_value = [mock_temperature] cont.get_all_select.return_value = [mock_select] + cont.get_all_sensor.return_value = [ + mock_buttoncounter, + mock_temperature, + mock_sensornumber, + mock_lightsensor, + ] + cont.get_all_light.return_value = [mock_dimmer] + cont.get_all_led.return_value = [mock_button] yield controller @@ -53,6 +74,7 @@ def mock_button() -> AsyncMock: channel.get_module_sw_version.return_value = "1.0.0" channel.get_module_serial.return_value = "a1b2c3d4e5f6" channel.is_closed.return_value = True + channel.is_on.return_value = False return channel @@ -68,6 +90,7 @@ def mock_temperature() -> AsyncMock: channel.get_full_name.return_value = "Channel full name" channel.get_module_sw_version.return_value = "3.0.0" channel.get_module_serial.return_value = "asdfghjk" + channel.is_counter_channel.return_value = False channel.get_class.return_value = "temperature" channel.get_unit.return_value = "°C" channel.get_state.return_value = 20.0 @@ -114,6 +137,82 @@ def mock_select() -> AsyncMock: return channel +@pytest.fixture +def mock_buttoncounter() -> AsyncMock: + """Mock a successful velbus channel.""" + channel = AsyncMock(spec=ButtonCounter) + channel.get_categories.return_value = ["sensor"] + channel.get_name.return_value = "ButtonCounter" + channel.get_module_address.return_value = 2 + channel.get_channel_number.return_value = 2 + channel.get_module_type_name.return_value = "VMB7IN" + channel.get_full_name.return_value = "Channel full name" + channel.get_module_sw_version.return_value = "1.0.0" + channel.get_module_serial.return_value = "a1b2c3d4e5f6" + channel.is_counter_channel.return_value = True + channel.is_temperature.return_value = False + channel.get_state.return_value = 100 + channel.get_unit.return_value = "W" + channel.get_counter_state.return_value = 100 + channel.get_counter_unit.return_value = "kWh" + return channel + + +@pytest.fixture +def mock_sensornumber() -> AsyncMock: + """Mock a successful velbus channel.""" + channel = AsyncMock(spec=SensorNumber) + channel.get_categories.return_value = ["sensor"] + channel.get_name.return_value = "SensorNumber" + channel.get_module_address.return_value = 2 + channel.get_channel_number.return_value = 3 + channel.get_module_type_name.return_value = "VMB7IN" + channel.get_full_name.return_value = "Channel full name" + channel.get_module_sw_version.return_value = "1.0.0" + channel.get_module_serial.return_value = "a1b2c3d4e5f6" + channel.is_counter_channel.return_value = False + channel.is_temperature.return_value = False + channel.get_unit.return_value = "m" + channel.get_state.return_value = 10 + return channel + + +@pytest.fixture +def mock_lightsensor() -> AsyncMock: + """Mock a successful velbus channel.""" + channel = AsyncMock(spec=LightSensor) + channel.get_categories.return_value = ["sensor"] + channel.get_name.return_value = "LightSensor" + channel.get_module_address.return_value = 2 + channel.get_channel_number.return_value = 4 + channel.get_module_type_name.return_value = "VMB7IN" + channel.get_full_name.return_value = "Channel full name" + channel.get_module_sw_version.return_value = "1.0.0" + channel.get_module_serial.return_value = "a1b2c3d4e5f6" + channel.is_counter_channel.return_value = False + channel.is_temperature.return_value = False + channel.get_unit.return_value = "illuminance" + channel.get_state.return_value = 250 + return channel + + +@pytest.fixture +def mock_dimmer() -> AsyncMock: + """Mock a successful velbus channel.""" + channel = AsyncMock(spec=Dimmer) + channel.get_categories.return_value = ["light"] + channel.get_name.return_value = "Dimmer" + channel.get_module_address.return_value = 3 + channel.get_channel_number.return_value = 1 + channel.get_module_type_name.return_value = "VMBDN1" + channel.get_full_name.return_value = "Dimmer full name" + channel.get_module_sw_version.return_value = "1.0.0" + channel.get_module_serial.return_value = "a1b2c3d4e5f6g7" + channel.is_on.return_value = False + channel.get_dimmer_state.return_value = 33 + return channel + + @pytest.fixture(name="config_entry") async def mock_config_entry( hass: HomeAssistant, diff --git a/tests/components/velbus/snapshots/test_light.ambr b/tests/components/velbus/snapshots/test_light.ambr new file mode 100644 index 0000000000000..a4574f1b33936 --- /dev/null +++ b/tests/components/velbus/snapshots/test_light.ambr @@ -0,0 +1,112 @@ +# serializer version: 1 +# name: test_entities[light.dimmer-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'supported_color_modes': list([ + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': None, + 'entity_id': 'light.dimmer', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Dimmer', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6g7-1', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[light.dimmer-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'brightness': None, + 'color_mode': None, + 'friendly_name': 'Dimmer', + 'supported_color_modes': list([ + , + ]), + 'supported_features': , + }), + 'context': , + 'entity_id': 'light.dimmer', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[light.led_buttonon-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'supported_color_modes': list([ + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': , + 'entity_id': 'light.led_buttonon', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'LED ButtonOn', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6-1', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[light.led_buttonon-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'color_mode': None, + 'friendly_name': 'LED ButtonOn', + 'supported_color_modes': list([ + , + ]), + 'supported_features': , + }), + 'context': , + 'entity_id': 'light.led_buttonon', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/velbus/snapshots/test_sensor.ambr b/tests/components/velbus/snapshots/test_sensor.ambr new file mode 100644 index 0000000000000..132f4c7a05980 --- /dev/null +++ b/tests/components/velbus/snapshots/test_sensor.ambr @@ -0,0 +1,255 @@ +# serializer version: 1 +# name: test_entities[sensor.buttoncounter-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.buttoncounter', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'ButtonCounter', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6-2', + 'unit_of_measurement': 'W', + }) +# --- +# name: test_entities[sensor.buttoncounter-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'ButtonCounter', + 'state_class': , + 'unit_of_measurement': 'W', + }), + 'context': , + 'entity_id': 'sensor.buttoncounter', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100.0', + }) +# --- +# name: test_entities[sensor.buttoncounter_counter-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.buttoncounter_counter', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:counter', + 'original_name': 'ButtonCounter-counter', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6-2-counter', + 'unit_of_measurement': 'kWh', + }) +# --- +# name: test_entities[sensor.buttoncounter_counter-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'ButtonCounter-counter', + 'icon': 'mdi:counter', + 'state_class': , + 'unit_of_measurement': 'kWh', + }), + 'context': , + 'entity_id': 'sensor.buttoncounter_counter', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100.0', + }) +# --- +# name: test_entities[sensor.lightsensor-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.lightsensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'LightSensor', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6-4', + 'unit_of_measurement': 'illuminance', + }) +# --- +# name: test_entities[sensor.lightsensor-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'LightSensor', + 'state_class': , + 'unit_of_measurement': 'illuminance', + }), + 'context': , + 'entity_id': 'sensor.lightsensor', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '250.0', + }) +# --- +# name: test_entities[sensor.sensornumber-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sensornumber', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'SensorNumber', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6-3', + 'unit_of_measurement': 'm', + }) +# --- +# name: test_entities[sensor.sensornumber-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SensorNumber', + 'state_class': , + 'unit_of_measurement': 'm', + }), + 'context': , + 'entity_id': 'sensor.sensornumber', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '10.0', + }) +# --- +# name: test_entities[sensor.temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.temperature', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'asdfghjk-3', + 'unit_of_measurement': , + }) +# --- +# name: test_entities[sensor.temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '20.0', + }) +# --- diff --git a/tests/components/velbus/test_light.py b/tests/components/velbus/test_light.py new file mode 100644 index 0000000000000..344d1626bbd98 --- /dev/null +++ b/tests/components/velbus/test_light.py @@ -0,0 +1,138 @@ +"""Velbus light platform tests.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + ATTR_FLASH, + ATTR_TRANSITION, + DOMAIN as LIGHT_DOMAIN, + FLASH_LONG, + FLASH_SHORT, +) +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + Platform, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch("homeassistant.components.velbus.PLATFORMS", [Platform.LIGHT]): + await init_integration(hass, config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) + + +async def test_dimmer_actions( + hass: HomeAssistant, + mock_dimmer: AsyncMock, + config_entry: MockConfigEntry, +) -> None: + """Test every supported dimmer action.""" + await init_integration(hass, config_entry) + # turn off + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "light.dimmer"}, + blocking=True, + ) + mock_dimmer.set_dimmer_state.assert_called_once_with(0, 0) + # turn on without brightness == restore previous brightness + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.dimmer", ATTR_TRANSITION: 1}, + blocking=True, + ) + mock_dimmer.restore_dimmer_state.assert_called_once_with(1) + # turn on with brightness == 0 + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.dimmer", ATTR_BRIGHTNESS: 0, ATTR_TRANSITION: 1}, + blocking=True, + ) + mock_dimmer.set_dimmer_state.assert_called_with(0, 1) + assert mock_dimmer.set_dimmer_state.call_count == 2 + # turn on with brightness == 33 + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.dimmer", ATTR_BRIGHTNESS: 33}, + blocking=True, + ) + mock_dimmer.set_dimmer_state.assert_called_with(12, 0) + assert mock_dimmer.set_dimmer_state.call_count == 3 + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_led_actions( + hass: HomeAssistant, + mock_button: AsyncMock, + config_entry: MockConfigEntry, +) -> None: + """Test every supported button led action.""" + await init_integration(hass, config_entry) + # turn off + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "light.led_buttonon"}, + blocking=True, + ) + mock_button.set_led_state.assert_called_once_with("off") + # turn on + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.led_buttonon"}, + blocking=True, + ) + mock_button.set_led_state.assert_called_with("on") + assert mock_button.set_led_state.call_count == 2 + # turn on with FLASH_LONG + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.led_buttonon", ATTR_FLASH: FLASH_LONG}, + blocking=True, + ) + mock_button.set_led_state.assert_called_with("slow") + assert mock_button.set_led_state.call_count == 3 + # turn on with FLASH_SHORT + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.led_buttonon", ATTR_FLASH: FLASH_SHORT}, + blocking=True, + ) + mock_button.set_led_state.assert_called_with("fast") + assert mock_button.set_led_state.call_count == 4 + # turn on with UNKNOWN flash option + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.led_buttonon", ATTR_FLASH: FLASH_SHORT}, + blocking=True, + ) + mock_button.set_led_state.assert_called_with("fast") + assert mock_button.set_led_state.call_count == 5 diff --git a/tests/components/velbus/test_sensor.py b/tests/components/velbus/test_sensor.py new file mode 100644 index 0000000000000..d89d2de59db6e --- /dev/null +++ b/tests/components/velbus/test_sensor.py @@ -0,0 +1,26 @@ +"""Velbus sensor platform tests.""" + +from unittest.mock import patch + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +async def test_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch("homeassistant.components.velbus.PLATFORMS", [Platform.SENSOR]): + await init_integration(hass, config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)