Skip to content

Commit 2da7a93

Browse files
authored
Add switch platform to local_slide (#133369)
1 parent 6a54edc commit 2da7a93

File tree

5 files changed

+171
-1
lines changed

5 files changed

+171
-1
lines changed

homeassistant/components/slide_local/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .coordinator import SlideCoordinator
1010

11-
PLATFORMS = [Platform.BUTTON, Platform.COVER]
11+
PLATFORMS = [Platform.BUTTON, Platform.COVER, Platform.SWITCH]
1212
type SlideConfigEntry = ConfigEntry[SlideCoordinator]
1313

1414

homeassistant/components/slide_local/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
"calibrate": {
4747
"name": "Calibrate"
4848
}
49+
},
50+
"switch": {
51+
"touchgo": {
52+
"name": "TouchGo"
53+
}
4954
}
5055
},
5156
"exceptions": {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Support for Slide switch."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Any
6+
7+
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
8+
from homeassistant.const import EntityCategory
9+
from homeassistant.core import HomeAssistant
10+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
11+
12+
from . import SlideConfigEntry
13+
from .coordinator import SlideCoordinator
14+
from .entity import SlideEntity
15+
16+
PARALLEL_UPDATES = 0
17+
18+
19+
async def async_setup_entry(
20+
hass: HomeAssistant,
21+
entry: SlideConfigEntry,
22+
async_add_entities: AddEntitiesCallback,
23+
) -> None:
24+
"""Set up switch for Slide platform."""
25+
26+
coordinator = entry.runtime_data
27+
28+
async_add_entities([SlideSwitch(coordinator)])
29+
30+
31+
class SlideSwitch(SlideEntity, SwitchEntity):
32+
"""Defines a Slide switch."""
33+
34+
_attr_entity_category = EntityCategory.CONFIG
35+
_attr_translation_key = "touchgo"
36+
_attr_device_class = SwitchDeviceClass.SWITCH
37+
38+
def __init__(self, coordinator: SlideCoordinator) -> None:
39+
"""Initialize the slide switch."""
40+
super().__init__(coordinator)
41+
self._attr_unique_id = f"{coordinator.data["mac"]}-touchgo"
42+
43+
@property
44+
def is_on(self) -> bool:
45+
"""Return if switch is on."""
46+
return self.coordinator.data["touch_go"]
47+
48+
async def async_turn_off(self, **kwargs: Any) -> None:
49+
"""Turn off touchgo."""
50+
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, False)
51+
await self.coordinator.async_request_refresh()
52+
53+
async def async_turn_on(self, **kwargs: Any) -> None:
54+
"""Turn on touchgo."""
55+
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, True)
56+
await self.coordinator.async_request_refresh()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# serializer version: 1
2+
# name: test_all_entities[switch.slide_bedroom_touchgo-entry]
3+
EntityRegistryEntrySnapshot({
4+
'aliases': set({
5+
}),
6+
'area_id': None,
7+
'capabilities': None,
8+
'config_entry_id': <ANY>,
9+
'device_class': None,
10+
'device_id': <ANY>,
11+
'disabled_by': None,
12+
'domain': 'switch',
13+
'entity_category': <EntityCategory.CONFIG: 'config'>,
14+
'entity_id': 'switch.slide_bedroom_touchgo',
15+
'has_entity_name': True,
16+
'hidden_by': None,
17+
'icon': None,
18+
'id': <ANY>,
19+
'labels': set({
20+
}),
21+
'name': None,
22+
'options': dict({
23+
}),
24+
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
25+
'original_icon': None,
26+
'original_name': 'TouchGo',
27+
'platform': 'slide_local',
28+
'previous_unique_id': None,
29+
'supported_features': 0,
30+
'translation_key': 'touchgo',
31+
'unique_id': '1234567890ab-touchgo',
32+
'unit_of_measurement': None,
33+
})
34+
# ---
35+
# name: test_all_entities[switch.slide_bedroom_touchgo-state]
36+
StateSnapshot({
37+
'attributes': ReadOnlyDict({
38+
'device_class': 'switch',
39+
'friendly_name': 'slide bedroom TouchGo',
40+
}),
41+
'context': <ANY>,
42+
'entity_id': 'switch.slide_bedroom_touchgo',
43+
'last_changed': <ANY>,
44+
'last_reported': <ANY>,
45+
'last_updated': <ANY>,
46+
'state': 'on',
47+
})
48+
# ---
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Tests for the Slide Local switch platform."""
2+
3+
from unittest.mock import AsyncMock
4+
5+
import pytest
6+
from syrupy import SnapshotAssertion
7+
8+
from homeassistant.components.switch import (
9+
DOMAIN as SWITCH_DOMAIN,
10+
SERVICE_TOGGLE,
11+
SERVICE_TURN_OFF,
12+
SERVICE_TURN_ON,
13+
)
14+
from homeassistant.const import ATTR_ENTITY_ID, Platform
15+
from homeassistant.core import HomeAssistant
16+
from homeassistant.helpers import entity_registry as er
17+
18+
from . import setup_platform
19+
20+
from tests.common import MockConfigEntry, snapshot_platform
21+
22+
23+
async def test_all_entities(
24+
hass: HomeAssistant,
25+
snapshot: SnapshotAssertion,
26+
mock_slide_api: AsyncMock,
27+
mock_config_entry: MockConfigEntry,
28+
entity_registry: er.EntityRegistry,
29+
) -> None:
30+
"""Test all entities."""
31+
await setup_platform(hass, mock_config_entry, [Platform.SWITCH])
32+
33+
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
34+
35+
36+
@pytest.mark.parametrize(
37+
("service"),
38+
[
39+
SERVICE_TURN_OFF,
40+
SERVICE_TURN_ON,
41+
SERVICE_TOGGLE,
42+
],
43+
)
44+
async def test_services(
45+
hass: HomeAssistant,
46+
service: str,
47+
mock_slide_api: AsyncMock,
48+
mock_config_entry: MockConfigEntry,
49+
) -> None:
50+
"""Test switch."""
51+
await setup_platform(hass, mock_config_entry, [Platform.SWITCH])
52+
53+
await hass.services.async_call(
54+
SWITCH_DOMAIN,
55+
service,
56+
{
57+
ATTR_ENTITY_ID: "switch.slide_bedroom_touchgo",
58+
},
59+
blocking=True,
60+
)
61+
mock_slide_api.slide_set_touchgo.assert_called_once()

0 commit comments

Comments
 (0)