Add velbus light and sensor platform testcases (#134485)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/134587/head
Maikel Punie 2025-01-03 20:42:01 +01:00 committed by GitHub
parent d4f38099ae
commit e6da6d9612
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 631 additions and 1 deletions

View File

@ -4,7 +4,16 @@ from collections.abc import Generator
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,

View File

@ -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([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.dimmer',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Dimmer',
'platform': 'velbus',
'previous_unique_id': None,
'supported_features': <LightEntityFeature: 32>,
'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([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 32>,
}),
'context': <ANY>,
'entity_id': 'light.dimmer',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_entities[light.led_buttonon-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'light.led_buttonon',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <LightEntityFeature: 8>,
'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([
<ColorMode.ONOFF: 'onoff'>,
]),
'supported_features': <LightEntityFeature: 8>,
}),
'context': <ANY>,
'entity_id': 'light.led_buttonon',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -0,0 +1,255 @@
# serializer version: 1
# name: test_entities[sensor.buttoncounter-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.buttoncounter',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.POWER: 'power'>,
'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': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': 'W',
}),
'context': <ANY>,
'entity_id': 'sensor.buttoncounter',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '100.0',
})
# ---
# name: test_entities[sensor.buttoncounter_counter-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.buttoncounter_counter',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'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': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
'unit_of_measurement': 'kWh',
}),
'context': <ANY>,
'entity_id': 'sensor.buttoncounter_counter',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '100.0',
})
# ---
# name: test_entities[sensor.lightsensor-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.lightsensor',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': 'illuminance',
}),
'context': <ANY>,
'entity_id': 'sensor.lightsensor',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '250.0',
})
# ---
# name: test_entities[sensor.sensornumber-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.sensornumber',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': 'm',
}),
'context': <ANY>,
'entity_id': 'sensor.sensornumber',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '10.0',
})
# ---
# name: test_entities[sensor.temperature-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.temperature',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
'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': <UnitOfTemperature.CELSIUS: '°C'>,
})
# ---
# name: test_entities[sensor.temperature-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'temperature',
'friendly_name': 'Temperature',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
}),
'context': <ANY>,
'entity_id': 'sensor.temperature',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '20.0',
})
# ---

View File

@ -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

View File

@ -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)