fix review comments and more

This commit is contained in:
Taraman17 2025-02-07 10:06:36 +00:00
parent 526b344456
commit 625ecdda6d
6 changed files with 75 additions and 278 deletions

View File

@ -156,29 +156,20 @@
"external_binary_input": {
"name": "Child Lock"
},
"identification_mode": {
"name": "Identification Mode"
},
"manual_operation": {
"name": "Manual Operation"
},
"motor_rotation": {
"name": "Motor rotation direction"
"on_off_instance": {
"name": "Switch {instance}"
},
"restore_last_known_state": {
"name": "Restore last known state"
},
"switch_type": {
"name": "Switch Type"
},
"watchdog_on_off": {
"watchdog": {
"name": "Watchdog"
}
}
},
"exceptions": {
"connection_closed": {
"message": "Could not connect to Homee while setting attribute"
"message": "Could not connect to Homee while setting attribute."
}
}
}

View File

@ -50,26 +50,14 @@ SWITCH_DESCRIPTIONS: dict[AttributeType, HomeeSwitchEntityDescription] = {
AttributeType.EXTERNAL_BINARY_INPUT: HomeeSwitchEntityDescription(
key="external_binary_input", entity_category=EntityCategory.CONFIG
),
AttributeType.IDENTIFICATION_MODE: HomeeSwitchEntityDescription(
key="identification_mode", entity_category=EntityCategory.DIAGNOSTIC
),
AttributeType.MANUAL_OPERATION: HomeeSwitchEntityDescription(
key="manual_operation"
),
AttributeType.MOTOR_ROTATION: HomeeSwitchEntityDescription(
key="motor_rotation", entity_category=EntityCategory.CONFIG
),
AttributeType.ON_OFF: HomeeSwitchEntityDescription(
key="on_off", device_class_fn=get_device_class, name=None
),
AttributeType.RESTORE_LAST_KNOWN_STATE: HomeeSwitchEntityDescription(
key="restore_last_known_state", entity_category=EntityCategory.CONFIG
),
AttributeType.SWITCH_TYPE: HomeeSwitchEntityDescription(
key="switch_type", entity_category=EntityCategory.CONFIG
),
AttributeType.WATCHDOG_ON_OFF: HomeeSwitchEntityDescription(
key="watchdog_on_off", entity_category=EntityCategory.CONFIG
key="watchdog", entity_category=EntityCategory.CONFIG
),
}
@ -86,12 +74,7 @@ async def async_setup_entry(
devices.extend(
HomeeSwitch(attribute, config_entry, SWITCH_DESCRIPTIONS[attribute.type])
for attribute in node.attributes
if (
attribute.type in SWITCH_DESCRIPTIONS
and attribute.editable
and attribute.maximum == 1
and attribute.step_value == 1
)
if (attribute.type in SWITCH_DESCRIPTIONS and attribute.editable)
and not (
attribute.type == AttributeType.ON_OFF
and node.profile in LIGHT_PROFILES
@ -123,9 +106,14 @@ class HomeeSwitch(HomeeEntity, SwitchEntity):
if not ((attribute.type == AttributeType.ON_OFF) and (attribute.instance == 0)):
self._attr_translation_key = description.key
if attribute.instance > 0:
self._attr_translation_key = f"{self._attr_translation_key}_instance"
self._attr_translation_key = f"{description.key}_instance"
self._attr_translation_placeholders = {"instance": str(attribute.instance)}
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
return bool(self._attribute.current_value)
@property
def device_class(self) -> SwitchDeviceClass:
"""Return the device class of the switch."""

View File

@ -67,6 +67,5 @@ def mock_homee() -> Generator[AsyncMock]:
homee.connected = True
homee.get_access_token.return_value = "test_token"
homee.get_node_by_id.return_value = None
yield homee

View File

@ -19,7 +19,7 @@
"security": 0,
"attributes": [
{
"id": 3,
"id": 1,
"node_id": 1,
"instance": 0,
"minimum": 0,
@ -40,49 +40,7 @@
"name": ""
},
{
"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": 8,
"id": 2,
"node_id": 1,
"instance": 0,
"minimum": 0,
@ -103,30 +61,9 @@
"name": ""
},
{
"id": 9,
"id": 3,
"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": 11,
"node_id": 1,
"instance": 0,
"instance": 1,
"minimum": 0,
"maximum": 1,
"current_value": 0.0,
@ -145,9 +82,9 @@
"name": ""
},
{
"id": 14,
"id": 4,
"node_id": 1,
"instance": 0,
"instance": 2,
"minimum": 0,
"maximum": 1,
"current_value": 1.0,
@ -156,9 +93,9 @@
"unit": "n/a",
"step_value": 1.0,
"editable": 1,
"type": 133,
"type": 1,
"state": 1,
"last_changed": 1711299215,
"last_changed": 1736743294,
"changed_by": 1,
"changed_by_id": 0,
"based_on": 1,
@ -166,49 +103,7 @@
"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": 18,
"id": 5,
"node_id": 1,
"instance": 0,
"minimum": 0,

View File

@ -1,51 +1,4 @@
# serializer version: 1
# name: test_switch_snapshot[switch.test_switch-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.test_switch',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': None,
'platform': 'homee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '00055511EECC-1-11',
'unit_of_measurement': None,
})
# ---
# name: test_switch_snapshot[switch.test_switch-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'outlet',
'friendly_name': 'Test Switch',
}),
'context': <ANY>,
'entity_id': 'switch.test_switch',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_switch_snapshot[switch.test_switch_child_lock-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
@ -75,7 +28,7 @@
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'external_binary_input',
'unique_id': '00055511EECC-1-3',
'unique_id': '00055511EECC-1-1',
'unit_of_measurement': None,
})
# ---
@ -93,53 +46,6 @@
'state': 'off',
})
# ---
# name: test_switch_snapshot[switch.test_switch_identification_mode-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'switch.test_switch_identification_mode',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
'original_icon': None,
'original_name': 'Identification Mode',
'platform': 'homee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'identification_mode',
'unique_id': '00055511EECC-1-4',
'unit_of_measurement': None,
})
# ---
# name: test_switch_snapshot[switch.test_switch_identification_mode-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'switch',
'friendly_name': 'Test Switch Identification Mode',
}),
'context': <ANY>,
'entity_id': 'switch.test_switch_identification_mode',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_switch_snapshot[switch.test_switch_manual_operation-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
@ -169,7 +75,7 @@
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'manual_operation',
'unique_id': '00055511EECC-1-8',
'unique_id': '00055511EECC-1-2',
'unit_of_measurement': None,
})
# ---
@ -187,7 +93,7 @@
'state': 'off',
})
# ---
# name: test_switch_snapshot[switch.test_switch_motor_rotation_direction-entry]
# name: test_switch_snapshot[switch.test_switch_switch_1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -198,8 +104,8 @@
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.test_switch_motor_rotation_direction',
'entity_category': None,
'entity_id': 'switch.test_switch_switch_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
@ -209,32 +115,32 @@
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Motor rotation direction',
'original_name': 'Switch 1',
'platform': 'homee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'motor_rotation',
'unique_id': '00055511EECC-1-9',
'translation_key': 'on_off_instance',
'unique_id': '00055511EECC-1-3',
'unit_of_measurement': None,
})
# ---
# name: test_switch_snapshot[switch.test_switch_motor_rotation_direction-state]
# name: test_switch_snapshot[switch.test_switch_switch_1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'switch',
'friendly_name': 'Test Switch Motor rotation direction',
'device_class': 'outlet',
'friendly_name': 'Test Switch Switch 1',
}),
'context': <ANY>,
'entity_id': 'switch.test_switch_motor_rotation_direction',
'entity_id': 'switch.test_switch_switch_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_switch_snapshot[switch.test_switch_restore_last_known_state-entry]
# name: test_switch_snapshot[switch.test_switch_switch_2-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
@ -245,8 +151,8 @@
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.test_switch_restore_last_known_state',
'entity_category': None,
'entity_id': 'switch.test_switch_switch_2',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
@ -256,25 +162,25 @@
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Restore last known state',
'original_name': 'Switch 2',
'platform': 'homee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'restore_last_known_state',
'unique_id': '00055511EECC-1-14',
'translation_key': 'on_off_instance',
'unique_id': '00055511EECC-1-4',
'unit_of_measurement': None,
})
# ---
# name: test_switch_snapshot[switch.test_switch_restore_last_known_state-state]
# name: test_switch_snapshot[switch.test_switch_switch_2-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'switch',
'friendly_name': 'Test Switch Restore last known state',
'device_class': 'outlet',
'friendly_name': 'Test Switch Switch 2',
}),
'context': <ANY>,
'entity_id': 'switch.test_switch_restore_last_known_state',
'entity_id': 'switch.test_switch_switch_2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
@ -309,8 +215,8 @@
'platform': 'homee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'watchdog_on_off',
'unique_id': '00055511EECC-1-18',
'translation_key': 'watchdog',
'unique_id': '00055511EECC-1-5',
'unit_of_measurement': None,
})
# ---

View File

@ -20,7 +20,7 @@ from . import build_mock_node, setup_integration
from tests.common import MockConfigEntry, snapshot_platform
async def test_switch_on(
async def test_switch_turn_on(
hass: HomeAssistant,
mock_homee: MagicMock,
mock_config_entry: MockConfigEntry,
@ -30,18 +30,18 @@ async def test_switch_on(
mock_homee.get_node_by_id.return_value = mock_homee.nodes[0]
await setup_integration(hass, mock_config_entry)
assert hass.states.get("switch.test_switch").state is not STATE_ON
assert hass.states.get("switch.test_switch_switch_1").state is not STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test_switch"},
{ATTR_ENTITY_ID: "switch.test_switch_switch_1"},
blocking=True,
)
mock_homee.set_value.assert_called_once_with(1, 11, 1)
mock_homee.set_value.assert_called_once_with(1, 3, 1)
async def test_switch_off(
async def test_switch_turn_off(
hass: HomeAssistant,
mock_homee: MagicMock,
mock_config_entry: MockConfigEntry,
@ -51,14 +51,14 @@ async def test_switch_off(
mock_homee.get_node_by_id.return_value = mock_homee.nodes[0]
await setup_integration(hass, mock_config_entry)
assert hass.states.get("switch.test_switch_identification_mode").state is STATE_ON
assert hass.states.get("switch.test_switch_watchdog").state is STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.test_switch_identification_mode"},
{ATTR_ENTITY_ID: "switch.test_switch_watchdog"},
blocking=True,
)
mock_homee.set_value.assert_called_once_with(1, 4, 0)
mock_homee.set_value.assert_called_once_with(1, 5, 0)
async def test_switch_device_class(
@ -72,7 +72,7 @@ async def test_switch_device_class(
await setup_integration(hass, mock_config_entry)
assert (
hass.states.get("switch.test_switch").attributes["device_class"]
hass.states.get("switch.test_switch_switch_1").attributes["device_class"]
== SwitchDeviceClass.OUTLET
)
assert (
@ -93,7 +93,7 @@ async def test_switch_device_class_no_outlet(
await setup_integration(hass, mock_config_entry)
assert (
hass.states.get("switch.test_switch").attributes["device_class"]
hass.states.get("switch.test_switch_switch_1").attributes["device_class"]
== SwitchDeviceClass.SWITCH
)
@ -112,3 +112,21 @@ async def test_switch_snapshot(
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_switch_state(
hass: HomeAssistant,
mock_homee: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test if the correct state is returned."""
mock_homee.nodes = [build_mock_node("switches.json")]
mock_homee.get_node_by_id.return_value = mock_homee.nodes[0]
await setup_integration(hass, mock_config_entry)
assert hass.states.get("switch.test_switch_switch_1").state is not STATE_ON
switch = mock_homee.nodes[0].attributes[2]
switch.current_value = 1
switch.add_on_changed_listener.call_args_list[0][0][0](switch)
await hass.async_block_till_done()
assert hass.states.get("switch.test_switch_switch_1").state is STATE_ON