Start switch tests

This commit is contained in:
Taraman17 2025-02-04 21:17:47 +00:00
parent a0a653e89b
commit ea4f165a29
3 changed files with 381 additions and 3 deletions

View File

@ -71,7 +71,7 @@ SWITCH_DESCRIPTIONS: dict[AttributeType, HomeeSwitchEntityDescription] = {
key="open_partial_impulse" key="open_partial_impulse"
), ),
AttributeType.ON_OFF: HomeeSwitchEntityDescription( AttributeType.ON_OFF: HomeeSwitchEntityDescription(
key="on_off", device_class_fn=get_device_class key="on_off", device_class_fn=get_device_class, name=None
), ),
AttributeType.PERMANENTLY_OPEN_IMPULSE: HomeeSwitchEntityDescription( AttributeType.PERMANENTLY_OPEN_IMPULSE: HomeeSwitchEntityDescription(
key="permanently_open_impulse" key="permanently_open_impulse"
@ -106,7 +106,12 @@ async def async_setup_entry(
devices.extend( devices.extend(
HomeeSwitch(attribute, config_entry, SWITCH_DESCRIPTIONS[attribute.type]) HomeeSwitch(attribute, config_entry, SWITCH_DESCRIPTIONS[attribute.type])
for attribute in node.attributes for attribute in node.attributes
if (attribute.type in SWITCH_DESCRIPTIONS and attribute.editable) if (
attribute.type in SWITCH_DESCRIPTIONS
and attribute.editable
and attribute.maximum == 1
and attribute.step_value == 1
)
and not ( and not (
attribute.type == AttributeType.ON_OFF attribute.type == AttributeType.ON_OFF
and node.profile in LIGHT_PROFILES and node.profile in LIGHT_PROFILES
@ -135,7 +140,11 @@ class HomeeSwitch(HomeeEntity, SwitchEntity):
super().__init__(attribute, entry) super().__init__(attribute, entry)
self.entity_description = description self.entity_description = description
self._attr_is_on = bool(attribute.current_value) self._attr_is_on = bool(attribute.current_value)
self._attr_translation_key = description.key if not (
(attribute.type in [AttributeType.ON_OFF, AttributeType.IMPULSE])
and (attribute.instance == 0)
):
self._attr_translation_key = description.key
if attribute.instance > 0: if attribute.instance > 0:
self._attr_translation_key = f"{self._attr_translation_key}_instance" self._attr_translation_key = f"{self._attr_translation_key}_instance"
self._attr_translation_placeholders = {"instance": str(attribute.instance)} self._attr_translation_placeholders = {"instance": str(attribute.instance)}

View File

@ -0,0 +1,316 @@
{
"id": 1,
"name": "Test Switch",
"profile": 10,
"image": "nodeicon_dimmablebulb",
"favorite": 0,
"order": 27,
"protocol": 3,
"routing": 0,
"state": 1,
"state_changed": 1736188706,
"added": 1610308228,
"history": 1,
"cube_type": 3,
"note": "All known switches",
"services": 7,
"phonetic_name": "",
"owner": 2,
"security": 0,
"attributes": [
{
"id": 4,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 1.0,
"target_value": 1.0,
"last_value": 1.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 170,
"state": 1,
"last_changed": 1672169339,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 5,
"node_id": 1,
"instance": 1,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 304,
"state": 1,
"last_changed": 1738679948,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 4,
"data": "",
"name": ""
},
{
"id": 6,
"node_id": 1,
"instance": 2,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 304,
"state": 1,
"last_changed": 1738679948,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 4,
"data": "",
"name": ""
},
{
"id": 7,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 1.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 305,
"state": 1,
"last_changed": 1677692134,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 8,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "",
"step_value": 1.0,
"editable": 1,
"type": 91,
"state": 1,
"last_changed": 1711796633,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 9,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 324,
"state": 1,
"last_changed": 1615396155,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 10,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 306,
"state": 1,
"last_changed": 1677692134,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 11,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 1,
"state": 1,
"last_changed": 1736743294,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 13,
"node_id": 1,
"instance": 1,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "",
"step_value": 1.0,
"editable": 1,
"type": 347,
"state": 1,
"last_changed": 1682166449,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 14,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 1.0,
"target_value": 1.0,
"last_value": 1.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 133,
"state": 1,
"last_changed": 1711299215,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 15,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 2,
"current_value": 1.0,
"target_value": 1.0,
"last_value": 1.0,
"unit": "",
"step_value": 1.0,
"editable": 1,
"type": 63,
"state": 1,
"last_changed": 1694992768,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 16,
"node_id": 1,
"instance": 1,
"minimum": 0,
"maximum": 2,
"current_value": 1.0,
"target_value": 1.0,
"last_value": 1.0,
"unit": "",
"step_value": 1.0,
"editable": 1,
"type": 63,
"state": 1,
"last_changed": 1694992768,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 17,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
"target_value": 0.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 378,
"state": 1,
"last_changed": 1677692134,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
"data": "",
"name": ""
},
{
"id": 18,
"node_id": 1,
"instance": 0,
"minimum": 0,
"maximum": 1,
"current_value": 1.0,
"target_value": 1.0,
"last_value": 0.0,
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 385,
"state": 1,
"last_changed": 1735663169,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 0,
"data": "",
"name": ""
}
]
}

View File

@ -0,0 +1,53 @@
"""Test Homee switches."""
from unittest.mock import MagicMock
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from . import build_mock_node, setup_integration
from tests.common import MockConfigEntry
async def test_switch_on(
hass: HomeAssistant,
mock_homee: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test turn-on service."""
mock_homee.nodes = [build_mock_node("switches.json")]
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test_switch"},
blocking=True,
)
assert mock_homee.set_value.assert_called_once_with(1, 11, 1)
async def test_switch_off(
hass: HomeAssistant,
mock_homee: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test turn-off service."""
mock_homee.nodes = [build_mock_node("switches.json")]
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.test_switch_identification_mode"},
blocking=True,
)
assert mock_homee.set_value.assert_called_once_with(1, 4, 0)