Skip to content

Commit 29d49da

Browse files
author
Daniel Mason
committed
initial test
1 parent d02ee25 commit 29d49da

File tree

5 files changed

+144
-1
lines changed

5 files changed

+144
-1
lines changed

custom_components/__init__.py

Whitespace-only changes.

custom_components/processor/mqtt_code.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def process(self, payload):
331331

332332
def message_received(self, message):
333333
"""Handle new MQTT messages."""
334-
334+
print("Processing message:", message.payload)
335335
# self.log.debug("Message received: " + str(message))
336336

337337
self.process(message.payload)

pytest.ini

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[pytest]
2+
filterwarnings =
3+
ignore::DeprecationWarning:pkg_resources
4+
markers =
5+
asyncio: mark an async test that should run with asyncio
6+
7+
asyncio_mode = auto

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest
2+
pytest-asyncio
3+
4+
homeassistant

tests/test_mqtt_code.py

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import pytest
2+
import asyncio
3+
from unittest.mock import AsyncMock, MagicMock
4+
from custom_components.processor.mqtt_code import DeviceEntity, MqttButton
5+
from homeassistant.core import HomeAssistant
6+
7+
# Example device configuration from your YAML
8+
TEST_CONFIG = {
9+
"name": "Office Wall Panel",
10+
"type": "panel",
11+
"mappings": {
12+
"wp-spare-btn-1": {
13+
"type": "button",
14+
"payload": 13053618,
15+
"callback": True,
16+
"actions": {
17+
"default": [{"service_template": "switch.toggle", "entity_id": "switch.office_standby_power_switch"}]
18+
}
19+
},
20+
"wp-spare-btn-2": {
21+
"type": "button",
22+
"payload": 13053624,
23+
"callback": False,
24+
"actions": {
25+
"default": [{"service_template": "script.turn_on_silvia"}]
26+
}
27+
},
28+
"wp-spare-btn-3": {
29+
"type": "button",
30+
"payload": 13053620,
31+
"callback": True,
32+
"actions": {
33+
"default": [{"service_template": "script.unmapped_function"}]
34+
}
35+
},
36+
"wp-spare-btn-1-2": {
37+
"type": "button",
38+
"payload": 13053626,
39+
"callback": True,
40+
"actions": {
41+
"default": [{"service_template": "script.unmapped_function"}]
42+
}
43+
}
44+
}
45+
}
46+
47+
# -------------------
48+
# 🔧 Test Fixtures
49+
# -------------------
50+
51+
@pytest.fixture
52+
def hass():
53+
"""Mock Home Assistant instance."""
54+
return MagicMock(spec=HomeAssistant)
55+
56+
@pytest.fixture
57+
async def device_entity(hass):
58+
"""Fixture to create a DeviceEntity with test config."""
59+
entity = DeviceEntity(hass, TEST_CONFIG)
60+
await entity.async_added_to_hass()
61+
return entity
62+
63+
@pytest.fixture
64+
async def mqtt_button(hass, device_entity):
65+
"""Fixture to create an MqttButton inside the DeviceEntity."""
66+
mapping_name = "wp-spare-btn-1"
67+
button_config = TEST_CONFIG["mappings"][mapping_name]
68+
button = MqttButton(hass, mapping_name, button_config, device_entity)
69+
await button.async_added_to_hass()
70+
return button
71+
72+
# -------------------
73+
# 🔍 DeviceEntity Tests
74+
# -------------------
75+
76+
@pytest.mark.asyncio
77+
async def test_device_entity_creation(device_entity):
78+
"""Test that the DeviceEntity is created with correct attributes."""
79+
assert device_entity.name == "Office Wall Panel"
80+
assert device_entity.state == "ready"
81+
assert device_entity.available
82+
assert isinstance(device_entity._mappings, list)
83+
assert len(device_entity._mappings) == 4 # Ensure all mappings are added
84+
85+
@pytest.mark.asyncio
86+
async def test_device_async_update(device_entity):
87+
"""Test async update method of DeviceEntity."""
88+
await device_entity.async_update()
89+
assert device_entity.state == "online"
90+
91+
@pytest.mark.asyncio
92+
async def test_device_added_to_hass(device_entity):
93+
"""Test that async_added_to_hass is called properly."""
94+
await device_entity.async_added_to_hass()
95+
assert device_entity.state == "ready"
96+
97+
# -------------------
98+
# 🔍 MqttButton Tests
99+
# -------------------
100+
101+
@pytest.mark.asyncio
102+
async def test_mqtt_button_creation(mqtt_button):
103+
"""Test that the MqttButton is initialized correctly."""
104+
assert mqtt_button.name == "wp-spare-btn-1"
105+
assert mqtt_button.state == "setting up"
106+
assert isinstance(mqtt_button.payloads_on, list)
107+
assert mqtt_button.payloads_on == [13053618]
108+
109+
@pytest.mark.asyncio
110+
async def test_mqtt_button_message_handling(mqtt_button):
111+
"""Test that the MqttButton handles MQTT messages correctly."""
112+
message = MagicMock()
113+
message.payload = "13053618"
114+
115+
await asyncio.wait_for(mqtt_button.message_received(message), timeout=5)
116+
117+
assert mqtt_button.last_payload == "13053618"
118+
assert mqtt_button.last_action == "on"
119+
assert mqtt_button.extra_state_attributes["payload"] == "13053618"
120+
121+
@pytest.mark.asyncio
122+
async def test_mqtt_button_update_state(mqtt_button):
123+
"""Test update_state method to ensure attributes update properly."""
124+
mqtt_button.update_state("13053618", "on")
125+
assert mqtt_button.last_action == "on"
126+
assert mqtt_button.extra_state_attributes["payload"] == "13053618"
127+
128+
@pytest.mark.asyncio
129+
async def test_mqtt_button_added_to_hass(mqtt_button):
130+
"""Test that async_added_to_hass is properly handled."""
131+
await mqtt_button.async_added_to_hass()
132+
assert mqtt_button.state == "ready"

0 commit comments

Comments
 (0)