Skip to content

Commit

Permalink
Add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Dec 18, 2018
1 parent 228970b commit 9d5d20f
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 14 deletions.
39 changes: 37 additions & 2 deletions tests/components/binary_sensor/test_mqtt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The tests for the MQTT binary sensor platform."""
import json
import unittest
from unittest.mock import Mock, patch
from unittest.mock import ANY, Mock, patch
from datetime import timedelta

import homeassistant.core as ha
Expand All @@ -16,7 +16,7 @@

from tests.common import (
get_test_home_assistant, fire_mqtt_message, async_fire_mqtt_message,
fire_time_changed, mock_component, mock_mqtt_component,
fire_time_changed, mock_component, mock_mqtt_component, mock_registry,
async_mock_mqtt_component, MockConfigEntry)


Expand Down Expand Up @@ -457,3 +457,38 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'


async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'state_topic': 'test-topic',
'availability_topic': 'availability-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})

state = hass.states.get('binary_sensor.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()

registry.async_update_entity('binary_sensor.beer', new_entity_id='binary_sensor.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('binary_sensor.beer')
assert state is None

state = hass.states.get('binary_sensor.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
39 changes: 38 additions & 1 deletion tests/components/cover/test_mqtt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The tests for the MQTT cover platform."""
import json
import unittest
from unittest.mock import ANY

from homeassistant.components import cover, mqtt
from homeassistant.components.cover import (ATTR_POSITION, ATTR_TILT_POSITION)
Expand All @@ -16,7 +17,8 @@

from tests.common import (
get_test_home_assistant, mock_mqtt_component, async_fire_mqtt_message,
fire_mqtt_message, MockConfigEntry, async_mock_mqtt_component)
fire_mqtt_message, MockConfigEntry, async_mock_mqtt_component,
mock_registry)


class TestCoverMQTT(unittest.TestCase):
Expand Down Expand Up @@ -1156,3 +1158,38 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'


async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, cover.DOMAIN, {
cover.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'state_topic': 'test-topic',
'availability_topic': 'availability-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})

state = hass.states.get('cover.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()

registry.async_update_entity('cover.beer', new_entity_id='cover.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('cover.beer')
assert state is None

state = hass.states.get('cover.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
39 changes: 38 additions & 1 deletion tests/components/fan/test_mqtt.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Test MQTT fans."""
import json
from unittest.mock import ANY

from homeassistant.setup import async_setup_component
from homeassistant.components import fan
from homeassistant.components.mqtt.discovery import async_start
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_UNAVAILABLE

from tests.common import async_fire_mqtt_message, MockConfigEntry, \
async_mock_mqtt_component
async_mock_mqtt_component, mock_registry


async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
Expand Down Expand Up @@ -224,3 +225,39 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'


async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, fan.DOMAIN, {
fan.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'availability_topic': 'availability-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})

state = hass.states.get('fan.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()

registry.async_update_entity('fan.beer', new_entity_id='fan.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('fan.beer')
assert state is None

state = hass.states.get('fan.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
42 changes: 39 additions & 3 deletions tests/components/light/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
"""
import json
from unittest import mock
from unittest.mock import patch
from unittest.mock import ANY, patch

from homeassistant.setup import async_setup_component
from homeassistant.const import (
Expand All @@ -165,8 +165,8 @@
import homeassistant.core as ha

from tests.common import (
assert_setup_component, async_fire_mqtt_message,
async_mock_mqtt_component, mock_coro, MockConfigEntry)
assert_setup_component, async_fire_mqtt_message, async_mock_mqtt_component,
mock_coro, MockConfigEntry, mock_registry)
from tests.components.light import common


Expand Down Expand Up @@ -1180,3 +1180,39 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'


async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'availability_topic': 'availability-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})

state = hass.states.get('light.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()

registry.async_update_entity('light.beer', new_entity_id='light.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('light.beer')
assert state is None

state = hass.states.get('light.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
41 changes: 39 additions & 2 deletions tests/components/light/test_mqtt_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
brightness_scale: 99
"""
import json
from unittest.mock import patch
from unittest.mock import ANY, patch

from homeassistant.setup import async_setup_component
from homeassistant.const import (
Expand All @@ -100,7 +100,7 @@

from tests.common import (
mock_coro, async_fire_mqtt_message, async_mock_mqtt_component,
MockConfigEntry)
MockConfigEntry, mock_registry)


async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
Expand Down Expand Up @@ -677,3 +677,40 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'


async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'schema': 'json',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'availability_topic': 'availability-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})

state = hass.states.get('light.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()

registry.async_update_entity('light.beer', new_entity_id='light.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('light.beer')
assert state is None

state = hass.states.get('light.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
43 changes: 41 additions & 2 deletions tests/components/light/test_mqtt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
"""
import json
from unittest.mock import patch
from unittest.mock import ANY, patch

from homeassistant.setup import async_setup_component
from homeassistant.const import (
Expand All @@ -38,7 +38,7 @@

from tests.common import (
async_fire_mqtt_message, assert_setup_component, mock_coro,
async_mock_mqtt_component, MockConfigEntry)
async_mock_mqtt_component, MockConfigEntry, mock_registry)


async def test_setup_fails(hass, mqtt_mock):
Expand Down Expand Up @@ -632,3 +632,42 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
assert device.name == 'Beer'
assert device.model == 'Glass'
assert device.sw_version == '0.1-beta'


async def test_entity_id_update(hass, mqtt_mock):
"""Test MQTT subscriptions are managed when entity_id is updated."""
registry = mock_registry(hass, {})
mock_mqtt = await async_mock_mqtt_component(hass)
assert await async_setup_component(hass, light.DOMAIN, {
light.DOMAIN: [{
'platform': 'mqtt',
'name': 'beer',
'schema': 'template',
'state_topic': 'test-topic',
'command_topic': 'command-topic',
'command_on_template': 'on,{{ transition }}',
'command_off_template': 'off,{{ transition|d }}',
'availability_topic': 'availability-topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
})

state = hass.states.get('light.beer')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.reset_mock()

registry.async_update_entity('light.beer', new_entity_id='light.milk')
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('light.beer')
assert state is None

state = hass.states.get('light.milk')
assert state is not None
assert mock_mqtt.async_subscribe.call_count == 2
mock_mqtt.async_subscribe.assert_any_call('test-topic', ANY, 0, 'utf-8')
mock_mqtt.async_subscribe.assert_any_call('availability-topic', ANY, 0, 'utf-8')
Loading

0 comments on commit 9d5d20f

Please sign in to comment.