Create LED switches for tplink dimmers (#66839)

This commit is contained in:
Teemu R 2022-02-21 19:02:11 +01:00 committed by GitHub
parent c6114f2631
commit 16cc2b790b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 49 deletions

View File

@ -28,7 +28,7 @@ async def async_setup_entry(
"""Set up switches."""
coordinator: TPLinkDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
device = cast(SmartPlug, coordinator.device)
if not device.is_plug and not device.is_strip:
if not device.is_plug and not device.is_strip and not device.is_dimmer:
return
entities: list = []
if device.is_strip:
@ -36,7 +36,7 @@ async def async_setup_entry(
_LOGGER.debug("Initializing strip with %s sockets", len(device.children))
for child in device.children:
entities.append(SmartPlugSwitch(child, coordinator))
else:
elif device.is_plug:
entities.append(SmartPlugSwitch(device, coordinator))
entities.append(SmartPlugLedSwitch(device, coordinator))

View File

@ -28,7 +28,7 @@ def _mock_protocol() -> TPLinkSmartHomeProtocol:
def _mocked_bulb() -> SmartBulb:
bulb = MagicMock(auto_spec=SmartBulb)
bulb = MagicMock(auto_spec=SmartBulb, name="Mocked bulb")
bulb.update = AsyncMock()
bulb.mac = MAC_ADDRESS
bulb.alias = ALIAS
@ -55,10 +55,10 @@ def _mocked_bulb() -> SmartBulb:
def _mocked_dimmer() -> SmartDimmer:
dimmer = MagicMock(auto_spec=SmartDimmer)
dimmer = MagicMock(auto_spec=SmartDimmer, name="Mocked dimmer")
dimmer.update = AsyncMock()
dimmer.mac = MAC_ADDRESS
dimmer.alias = ALIAS
dimmer.alias = "My Dimmer"
dimmer.model = MODEL
dimmer.host = IP_ADDRESS
dimmer.brightness = 50
@ -77,12 +77,13 @@ def _mocked_dimmer() -> SmartDimmer:
dimmer.set_brightness = AsyncMock()
dimmer.set_hsv = AsyncMock()
dimmer.set_color_temp = AsyncMock()
dimmer.set_led = AsyncMock()
dimmer.protocol = _mock_protocol()
return dimmer
def _mocked_plug() -> SmartPlug:
plug = MagicMock(auto_spec=SmartPlug)
plug = MagicMock(auto_spec=SmartPlug, name="Mocked plug")
plug.update = AsyncMock()
plug.mac = MAC_ADDRESS
plug.alias = "My Plug"
@ -103,7 +104,7 @@ def _mocked_plug() -> SmartPlug:
def _mocked_strip() -> SmartStrip:
strip = MagicMock(auto_spec=SmartStrip)
strip = MagicMock(auto_spec=SmartStrip, name="Mocked strip")
strip.update = AsyncMock()
strip.mac = MAC_ADDRESS
strip.alias = "My Strip"

View File

@ -4,6 +4,7 @@ from datetime import timedelta
from unittest.mock import AsyncMock
from kasa import SmartDeviceException
import pytest
from homeassistant.components import tplink
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
@ -12,10 +13,11 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util import dt as dt_util, slugify
from . import (
MAC_ADDRESS,
_mocked_dimmer,
_mocked_plug,
_mocked_strip,
_patch_discovery,
@ -53,36 +55,42 @@ async def test_plug(hass: HomeAssistant) -> None:
plug.turn_on.reset_mock()
async def test_plug_led(hass: HomeAssistant) -> None:
"""Test a smart plug LED."""
@pytest.mark.parametrize(
"dev, domain",
[
(_mocked_plug(), "switch"),
(_mocked_strip(), "switch"),
(_mocked_dimmer(), "light"),
],
)
async def test_led_switch(hass: HomeAssistant, dev, domain: str) -> None:
"""Test LED setting for plugs, strips and dimmers."""
already_migrated_config_entry = MockConfigEntry(
domain=DOMAIN, data={}, unique_id=MAC_ADDRESS
)
already_migrated_config_entry.add_to_hass(hass)
plug = _mocked_plug()
with _patch_discovery(device=plug), _patch_single_discovery(device=plug):
with _patch_discovery(device=dev), _patch_single_discovery(device=dev):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
entity_id = "switch.my_plug"
state = hass.states.get(entity_id)
entity_name = slugify(dev.alias)
led_entity_id = f"{entity_id}_led"
led_entity_id = f"switch.{entity_name}_led"
led_state = hass.states.get(led_entity_id)
assert led_state.state == STATE_ON
assert led_state.name == f"{state.name} LED"
assert led_state.name == f"{dev.alias} LED"
await hass.services.async_call(
SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: led_entity_id}, blocking=True
)
plug.set_led.assert_called_once_with(False)
plug.set_led.reset_mock()
dev.set_led.assert_called_once_with(False)
dev.set_led.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: led_entity_id}, blocking=True
)
plug.set_led.assert_called_once_with(True)
plug.set_led.reset_mock()
dev.set_led.assert_called_once_with(True)
dev.set_led.reset_mock()
async def test_plug_unique_id(hass: HomeAssistant) -> None:
@ -156,35 +164,6 @@ async def test_strip(hass: HomeAssistant) -> None:
strip.children[plug_id].turn_on.reset_mock()
async def test_strip_led(hass: HomeAssistant) -> None:
"""Test a smart strip LED."""
already_migrated_config_entry = MockConfigEntry(
domain=DOMAIN, data={}, unique_id=MAC_ADDRESS
)
already_migrated_config_entry.add_to_hass(hass)
strip = _mocked_strip()
with _patch_discovery(device=strip), _patch_single_discovery(device=strip):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
# We should have a LED entity for the strip
led_entity_id = "switch.my_strip_led"
led_state = hass.states.get(led_entity_id)
assert led_state.state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: led_entity_id}, blocking=True
)
strip.set_led.assert_called_once_with(False)
strip.set_led.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: led_entity_id}, blocking=True
)
strip.set_led.assert_called_once_with(True)
strip.set_led.reset_mock()
async def test_strip_unique_ids(hass: HomeAssistant) -> None:
"""Test a strip unique id."""
already_migrated_config_entry = MockConfigEntry(