mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Add switch platform to local_slide (#133369)
This commit is contained in:
parent
6a54edce19
commit
2da7a93139
@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import SlideCoordinator
|
||||
|
||||
PLATFORMS = [Platform.BUTTON, Platform.COVER]
|
||||
PLATFORMS = [Platform.BUTTON, Platform.COVER, Platform.SWITCH]
|
||||
type SlideConfigEntry = ConfigEntry[SlideCoordinator]
|
||||
|
||||
|
||||
|
@ -46,6 +46,11 @@
|
||||
"calibrate": {
|
||||
"name": "Calibrate"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"touchgo": {
|
||||
"name": "TouchGo"
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
|
56
homeassistant/components/slide_local/switch.py
Normal file
56
homeassistant/components/slide_local/switch.py
Normal file
@ -0,0 +1,56 @@
|
||||
"""Support for Slide switch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import SlideConfigEntry
|
||||
from .coordinator import SlideCoordinator
|
||||
from .entity import SlideEntity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: SlideConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up switch for Slide platform."""
|
||||
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities([SlideSwitch(coordinator)])
|
||||
|
||||
|
||||
class SlideSwitch(SlideEntity, SwitchEntity):
|
||||
"""Defines a Slide switch."""
|
||||
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "touchgo"
|
||||
_attr_device_class = SwitchDeviceClass.SWITCH
|
||||
|
||||
def __init__(self, coordinator: SlideCoordinator) -> None:
|
||||
"""Initialize the slide switch."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = f"{coordinator.data["mac"]}-touchgo"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return if switch is on."""
|
||||
return self.coordinator.data["touch_go"]
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off touchgo."""
|
||||
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, False)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on touchgo."""
|
||||
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, True)
|
||||
await self.coordinator.async_request_refresh()
|
48
tests/components/slide_local/snapshots/test_switch.ambr
Normal file
48
tests/components/slide_local/snapshots/test_switch.ambr
Normal file
@ -0,0 +1,48 @@
|
||||
# serializer version: 1
|
||||
# name: test_all_entities[switch.slide_bedroom_touchgo-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.CONFIG: 'config'>,
|
||||
'entity_id': 'switch.slide_bedroom_touchgo',
|
||||
'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': 'TouchGo',
|
||||
'platform': 'slide_local',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'touchgo',
|
||||
'unique_id': '1234567890ab-touchgo',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[switch.slide_bedroom_touchgo-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'switch',
|
||||
'friendly_name': 'slide bedroom TouchGo',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.slide_bedroom_touchgo',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
61
tests/components/slide_local/test_switch.py
Normal file
61
tests/components/slide_local/test_switch.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""Tests for the Slide Local switch platform."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
DOMAIN as SWITCH_DOMAIN,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_platform
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_slide_api: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
await setup_platform(hass, mock_config_entry, [Platform.SWITCH])
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service"),
|
||||
[
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
SERVICE_TOGGLE,
|
||||
],
|
||||
)
|
||||
async def test_services(
|
||||
hass: HomeAssistant,
|
||||
service: str,
|
||||
mock_slide_api: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test switch."""
|
||||
await setup_platform(hass, mock_config_entry, [Platform.SWITCH])
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
service,
|
||||
{
|
||||
ATTR_ENTITY_ID: "switch.slide_bedroom_touchgo",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
mock_slide_api.slide_set_touchgo.assert_called_once()
|
Loading…
x
Reference in New Issue
Block a user