2019-01-31 01:31:59 +00:00
|
|
|
"""
|
|
|
|
Test for the SmartThings switch platform.
|
|
|
|
|
|
|
|
The only mocking required is of the underlying SmartThings API object so
|
|
|
|
real HTTP calls are not initiated during testing.
|
|
|
|
"""
|
|
|
|
from pysmartthings import Attribute, Capability
|
|
|
|
|
2019-02-22 19:35:12 +00:00
|
|
|
from homeassistant.components.smartthings import switch
|
2019-01-31 01:31:59 +00:00
|
|
|
from homeassistant.components.smartthings.const import (
|
2019-02-22 19:35:12 +00:00
|
|
|
DOMAIN, SIGNAL_SMARTTHINGS_UPDATE)
|
2019-02-25 14:50:16 +00:00
|
|
|
from homeassistant.components.switch import (
|
|
|
|
ATTR_CURRENT_POWER_W, ATTR_TODAY_ENERGY_KWH, DOMAIN as SWITCH_DOMAIN)
|
2019-01-31 01:31:59 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
|
2019-02-22 19:35:12 +00:00
|
|
|
from .conftest import setup_platform
|
2019-01-31 01:31:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_async_setup_platform():
|
|
|
|
"""Test setup platform does nothing (it uses config entries)."""
|
|
|
|
await switch.async_setup_platform(None, None, None)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_and_device_attributes(hass, device_factory):
|
|
|
|
"""Test the attributes of the entity are correct."""
|
|
|
|
# Arrange
|
|
|
|
device = device_factory('Switch_1', [Capability.switch],
|
|
|
|
{Attribute.switch: 'on'})
|
|
|
|
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
|
|
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
|
|
|
# Act
|
2019-02-22 19:35:12 +00:00
|
|
|
await setup_platform(hass, SWITCH_DOMAIN, device)
|
2019-01-31 01:31:59 +00:00
|
|
|
# Assert
|
2019-02-02 15:12:24 +00:00
|
|
|
entry = entity_registry.async_get('switch.switch_1')
|
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == device.device_id
|
|
|
|
|
|
|
|
entry = device_registry.async_get_device(
|
2019-01-31 01:31:59 +00:00
|
|
|
{(DOMAIN, device.device_id)}, [])
|
2019-02-02 15:12:24 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.name == device.label
|
|
|
|
assert entry.model == device.device_type_name
|
|
|
|
assert entry.manufacturer == 'Unavailable'
|
2019-01-31 01:31:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_turn_off(hass, device_factory):
|
|
|
|
"""Test the switch turns of successfully."""
|
|
|
|
# Arrange
|
|
|
|
device = device_factory('Switch_1', [Capability.switch],
|
|
|
|
{Attribute.switch: 'on'})
|
2019-02-22 19:35:12 +00:00
|
|
|
await setup_platform(hass, SWITCH_DOMAIN, device)
|
2019-01-31 01:31:59 +00:00
|
|
|
# Act
|
|
|
|
await hass.services.async_call(
|
|
|
|
'switch', 'turn_off', {'entity_id': 'switch.switch_1'},
|
|
|
|
blocking=True)
|
|
|
|
# Assert
|
|
|
|
state = hass.states.get('switch.switch_1')
|
|
|
|
assert state is not None
|
|
|
|
assert state.state == 'off'
|
|
|
|
|
|
|
|
|
|
|
|
async def test_turn_on(hass, device_factory):
|
|
|
|
"""Test the switch turns of successfully."""
|
|
|
|
# Arrange
|
2019-02-25 14:50:16 +00:00
|
|
|
device = device_factory('Switch_1',
|
|
|
|
[Capability.switch,
|
|
|
|
Capability.power_meter,
|
|
|
|
Capability.energy_meter],
|
|
|
|
{Attribute.switch: 'off',
|
|
|
|
Attribute.power: 355,
|
|
|
|
Attribute.energy: 11.422})
|
2019-02-22 19:35:12 +00:00
|
|
|
await setup_platform(hass, SWITCH_DOMAIN, device)
|
2019-01-31 01:31:59 +00:00
|
|
|
# Act
|
|
|
|
await hass.services.async_call(
|
|
|
|
'switch', 'turn_on', {'entity_id': 'switch.switch_1'},
|
|
|
|
blocking=True)
|
|
|
|
# Assert
|
|
|
|
state = hass.states.get('switch.switch_1')
|
|
|
|
assert state is not None
|
|
|
|
assert state.state == 'on'
|
2019-02-25 14:50:16 +00:00
|
|
|
assert state.attributes[ATTR_CURRENT_POWER_W] == 355
|
|
|
|
assert state.attributes[ATTR_TODAY_ENERGY_KWH] == 11.422
|
2019-01-31 01:31:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_from_signal(hass, device_factory):
|
|
|
|
"""Test the switch updates when receiving a signal."""
|
|
|
|
# Arrange
|
|
|
|
device = device_factory('Switch_1', [Capability.switch],
|
|
|
|
{Attribute.switch: 'off'})
|
2019-02-22 19:35:12 +00:00
|
|
|
await setup_platform(hass, SWITCH_DOMAIN, device)
|
2019-01-31 01:31:59 +00:00
|
|
|
await device.switch_on(True)
|
|
|
|
# Act
|
|
|
|
async_dispatcher_send(hass, SIGNAL_SMARTTHINGS_UPDATE,
|
|
|
|
[device.device_id])
|
|
|
|
# Assert
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get('switch.switch_1')
|
|
|
|
assert state is not None
|
|
|
|
assert state.state == 'on'
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_config_entry(hass, device_factory):
|
|
|
|
"""Test the switch is removed when the config entry is unloaded."""
|
|
|
|
# Arrange
|
2019-02-14 00:36:49 +00:00
|
|
|
device = device_factory('Switch 1', [Capability.switch],
|
2019-01-31 01:31:59 +00:00
|
|
|
{Attribute.switch: 'on'})
|
2019-02-22 19:35:12 +00:00
|
|
|
config_entry = await setup_platform(hass, SWITCH_DOMAIN, device)
|
2019-01-31 01:31:59 +00:00
|
|
|
# Act
|
|
|
|
await hass.config_entries.async_forward_entry_unload(
|
|
|
|
config_entry, 'switch')
|
|
|
|
# Assert
|
|
|
|
assert not hass.states.get('switch.switch_1')
|