From fdc8830dd362e6d0b08b3ea1e139cf83f82d853c Mon Sep 17 00:00:00 2001 From: Christopher Bailey Date: Sat, 14 May 2022 12:07:17 -0400 Subject: [PATCH] Remove ssh switch from unsupported devices for UniFi Protect (#71859) --- .../components/unifiprotect/switch.py | 28 +++++++++++++++---- tests/components/unifiprotect/test_switch.py | 25 +++++++++-------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/unifiprotect/switch.py b/homeassistant/components/unifiprotect/switch.py index 85a089994f8..6051e4e596f 100644 --- a/homeassistant/components/unifiprotect/switch.py +++ b/homeassistant/components/unifiprotect/switch.py @@ -43,7 +43,7 @@ async def _set_highfps(obj: Camera, value: bool) -> None: await obj.set_video_mode(VideoMode.DEFAULT) -ALL_DEVICES_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( +CAMERA_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( ProtectSwitchEntityDescription( key="ssh", name="SSH Enabled", @@ -53,9 +53,6 @@ ALL_DEVICES_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( ufp_value="is_ssh_enabled", ufp_set_method="set_ssh", ), -) - -CAMERA_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( ProtectSwitchEntityDescription( key="status_light", name="Status Light On", @@ -222,6 +219,15 @@ SENSE_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( LIGHT_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( + ProtectSwitchEntityDescription( + key="ssh", + name="SSH Enabled", + icon="mdi:lock", + entity_registry_enabled_default=False, + entity_category=EntityCategory.CONFIG, + ufp_value="is_ssh_enabled", + ufp_set_method="set_ssh", + ), ProtectSwitchEntityDescription( key="status_light", name="Status Light On", @@ -243,6 +249,18 @@ DOORLOCK_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( ), ) +VIEWER_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = ( + ProtectSwitchEntityDescription( + key="ssh", + name="SSH Enabled", + icon="mdi:lock", + entity_registry_enabled_default=False, + entity_category=EntityCategory.CONFIG, + ufp_value="is_ssh_enabled", + ufp_set_method="set_ssh", + ), +) + async def async_setup_entry( hass: HomeAssistant, @@ -254,11 +272,11 @@ async def async_setup_entry( entities: list[ProtectDeviceEntity] = async_all_device_entities( data, ProtectSwitch, - all_descs=ALL_DEVICES_SWITCHES, camera_descs=CAMERA_SWITCHES, light_descs=LIGHT_SWITCHES, sense_descs=SENSE_SWITCHES, lock_descs=DOORLOCK_SWITCHES, + viewer_descs=VIEWER_SWITCHES, ) async_add_entities(entities) diff --git a/tests/components/unifiprotect/test_switch.py b/tests/components/unifiprotect/test_switch.py index c54d04a8cb7..498f4c6b3b7 100644 --- a/tests/components/unifiprotect/test_switch.py +++ b/tests/components/unifiprotect/test_switch.py @@ -10,7 +10,6 @@ from pyunifiprotect.data.types import RecordingMode, SmartDetectObjectType, Vide from homeassistant.components.unifiprotect.const import DEFAULT_ATTRIBUTION from homeassistant.components.unifiprotect.switch import ( - ALL_DEVICES_SWITCHES, CAMERA_SWITCHES, LIGHT_SWITCHES, ProtectSwitchEntityDescription, @@ -29,7 +28,9 @@ from .conftest import ( CAMERA_SWITCHES_BASIC = [ d for d in CAMERA_SWITCHES - if d.name != "Detections: Face" and d.name != "Detections: Package" + if d.name != "Detections: Face" + and d.name != "Detections: Package" + and d.name != "SSH Enabled" ] CAMERA_SWITCHES_NO_EXTRA = [ d for d in CAMERA_SWITCHES_BASIC if d.name not in ("High FPS", "Privacy Mode") @@ -215,7 +216,7 @@ async def test_switch_setup_light( entity_registry = er.async_get(hass) - description = LIGHT_SWITCHES[0] + description = LIGHT_SWITCHES[1] unique_id, entity_id = ids_from_device_description( Platform.SWITCH, light, description @@ -230,7 +231,7 @@ async def test_switch_setup_light( assert state.state == STATE_OFF assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION - description = ALL_DEVICES_SWITCHES[0] + description = LIGHT_SWITCHES[0] unique_id = f"{light.id}_{description.key}" entity_id = f"switch.test_light_{description.name.lower().replace(' ', '_')}" @@ -271,7 +272,7 @@ async def test_switch_setup_camera_all( assert state.state == STATE_OFF assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION - description = ALL_DEVICES_SWITCHES[0] + description = CAMERA_SWITCHES[0] description_entity_name = ( description.name.lower().replace(":", "").replace(" ", "_") @@ -301,7 +302,7 @@ async def test_switch_setup_camera_none( entity_registry = er.async_get(hass) - for description in CAMERA_SWITCHES: + for description in CAMERA_SWITCHES_BASIC: if description.ufp_required_field is not None: continue @@ -318,7 +319,7 @@ async def test_switch_setup_camera_none( assert state.state == STATE_OFF assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION - description = ALL_DEVICES_SWITCHES[0] + description = CAMERA_SWITCHES[0] description_entity_name = ( description.name.lower().replace(":", "").replace(" ", "_") @@ -342,7 +343,7 @@ async def test_switch_setup_camera_none( async def test_switch_light_status(hass: HomeAssistant, light: Light): """Tests status light switch for lights.""" - description = LIGHT_SWITCHES[0] + description = LIGHT_SWITCHES[1] light.__fields__["set_status_light"] = Mock() light.set_status_light = AsyncMock() @@ -367,7 +368,7 @@ async def test_switch_camera_ssh( ): """Tests SSH switch for cameras.""" - description = ALL_DEVICES_SWITCHES[0] + description = CAMERA_SWITCHES[0] camera.__fields__["set_ssh"] = Mock() camera.set_ssh = AsyncMock() @@ -418,7 +419,7 @@ async def test_switch_camera_simple( async def test_switch_camera_highfps(hass: HomeAssistant, camera: Camera): """Tests High FPS switch for cameras.""" - description = CAMERA_SWITCHES[2] + description = CAMERA_SWITCHES[3] camera.__fields__["set_video_mode"] = Mock() camera.set_video_mode = AsyncMock() @@ -441,7 +442,7 @@ async def test_switch_camera_highfps(hass: HomeAssistant, camera: Camera): async def test_switch_camera_privacy(hass: HomeAssistant, camera: Camera): """Tests Privacy Mode switch for cameras.""" - description = CAMERA_SWITCHES[3] + description = CAMERA_SWITCHES[4] camera.__fields__["set_privacy"] = Mock() camera.set_privacy = AsyncMock() @@ -468,7 +469,7 @@ async def test_switch_camera_privacy_already_on( ): """Tests Privacy Mode switch for cameras with privacy mode defaulted on.""" - description = CAMERA_SWITCHES[3] + description = CAMERA_SWITCHES[4] camera_privacy.__fields__["set_privacy"] = Mock() camera_privacy.set_privacy = AsyncMock()