Add coverage to verify tplink unique ids (#56746)

pull/56791/head
J. Nick Koston 2021-09-28 19:57:22 -05:00 committed by GitHub
parent f7d95588f8
commit cf36d0966d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 6 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.components.light import (
from homeassistant.components.tplink.const import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from . import MAC_ADDRESS, _mocked_bulb, _patch_discovery, _patch_single_discovery
@ -25,6 +26,23 @@ from . import MAC_ADDRESS, _mocked_bulb, _patch_discovery, _patch_single_discove
from tests.common import MockConfigEntry
async def test_light_unique_id(hass: HomeAssistant) -> None:
"""Test a light unique id."""
already_migrated_config_entry = MockConfigEntry(
domain=DOMAIN, data={}, unique_id=MAC_ADDRESS
)
already_migrated_config_entry.add_to_hass(hass)
bulb = _mocked_bulb()
bulb.color_temp = None
with _patch_discovery(device=bulb), _patch_single_discovery(device=bulb):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
entity_id = "light.my_bulb"
entity_registry = er.async_get(hass)
assert entity_registry.async_get(entity_id).unique_id == "AABBCCDDEEFF"
async def test_color_light(hass: HomeAssistant) -> None:
"""Test a light."""
already_migrated_config_entry = MockConfigEntry(

View File

@ -5,6 +5,7 @@ from unittest.mock import Mock
from homeassistant.components import tplink
from homeassistant.components.tplink.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
from . import (
@ -120,3 +121,35 @@ async def test_color_light_no_emeter(hass: HomeAssistant) -> None:
]
for sensor_entity_id in not_expected:
assert hass.states.get(sensor_entity_id) is None
async def test_sensor_unique_id(hass: HomeAssistant) -> None:
"""Test a sensor unique ids."""
already_migrated_config_entry = MockConfigEntry(
domain=DOMAIN, data={}, unique_id=MAC_ADDRESS
)
already_migrated_config_entry.add_to_hass(hass)
plug = _mocked_plug()
plug.color_temp = None
plug.has_emeter = True
plug.emeter_realtime = Mock(
power=100,
total=30,
voltage=121,
current=5,
)
plug.emeter_today = None
with _patch_discovery(device=plug), _patch_single_discovery(device=plug):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
expected = {
"sensor.my_plug_current_consumption": "aa:bb:cc:dd:ee:ff_current_power_w",
"sensor.my_plug_total_consumption": "aa:bb:cc:dd:ee:ff_total_energy_kwh",
"sensor.my_plug_today_s_consumption": "aa:bb:cc:dd:ee:ff_today_energy_kwh",
"sensor.my_plug_voltage": "aa:bb:cc:dd:ee:ff_voltage",
"sensor.my_plug_current": "aa:bb:cc:dd:ee:ff_current_a",
}
entity_registry = er.async_get(hass)
for sensor_entity_id, value in expected.items():
assert entity_registry.async_get(sensor_entity_id).unique_id == value

View File

@ -10,6 +10,7 @@ from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.tplink.const import DOMAIN
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
@ -52,6 +53,22 @@ async def test_plug(hass: HomeAssistant) -> None:
plug.turn_on.reset_mock()
async def test_plug_unique_id(hass: HomeAssistant) -> None:
"""Test a plug unique id."""
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):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
entity_id = "switch.my_plug"
entity_registry = er.async_get(hass)
assert entity_registry.async_get(entity_id).unique_id == "aa:bb:cc:dd:ee:ff"
async def test_plug_update_fails(hass: HomeAssistant) -> None:
"""Test a smart plug update failure."""
already_migrated_config_entry = MockConfigEntry(
@ -80,8 +97,8 @@ async def test_strip(hass: HomeAssistant) -> None:
domain=DOMAIN, data={}, unique_id=MAC_ADDRESS
)
already_migrated_config_entry.add_to_hass(hass)
plug = _mocked_strip()
with _patch_discovery(device=plug), _patch_single_discovery(device=plug):
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()
@ -97,11 +114,30 @@ async def test_strip(hass: HomeAssistant) -> None:
await hass.services.async_call(
SWITCH_DOMAIN, "turn_off", {ATTR_ENTITY_ID: entity_id}, blocking=True
)
plug.children[plug_id].turn_off.assert_called_once()
plug.children[plug_id].turn_off.reset_mock()
strip.children[plug_id].turn_off.assert_called_once()
strip.children[plug_id].turn_off.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True
)
plug.children[plug_id].turn_on.assert_called_once()
plug.children[plug_id].turn_on.reset_mock()
strip.children[plug_id].turn_on.assert_called_once()
strip.children[plug_id].turn_on.reset_mock()
async def test_strip_unique_ids(hass: HomeAssistant) -> None:
"""Test a strip unique id."""
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()
for plug_id in range(2):
entity_id = f"switch.plug{plug_id}"
entity_registry = er.async_get(hass)
assert (
entity_registry.async_get(entity_id).unique_id == f"PLUG{plug_id}DEVICEID"
)