test_media_player.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Tests for the media_player entity."""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  4. from homeassistant.components.media_player import MediaPlayerState
  5. from pytest_homeassistant_custom_component.common import MockConfigEntry
  6. from custom_components.tuya_local.const import (
  7. CONF_DEVICE_ID,
  8. CONF_PROTOCOL_VERSION,
  9. CONF_TYPE,
  10. DOMAIN,
  11. )
  12. from custom_components.tuya_local.media_player import (
  13. TuyaLocalMediaPlayer,
  14. async_setup_entry,
  15. )
  16. from .helpers import mock_device
  17. @pytest.mark.asyncio
  18. async def test_init_entry(hass):
  19. """Test the initialisation."""
  20. entry = MockConfigEntry(
  21. domain=DOMAIN,
  22. data={
  23. CONF_TYPE: "ekaza_minipad_controlpanel",
  24. CONF_DEVICE_ID: "dummy",
  25. CONF_PROTOCOL_VERSION: "auto",
  26. },
  27. )
  28. # although async, the async_add_entities function passed to
  29. # async_setup_entry is called truly asynchronously. If we use
  30. # AsyncMock, it expects us to await the result.
  31. m_add_entities = Mock()
  32. m_device = AsyncMock()
  33. hass.data[DOMAIN] = {"dummy": {"device": m_device}}
  34. await async_setup_entry(hass, entry, m_add_entities)
  35. assert (
  36. type(hass.data[DOMAIN]["dummy"]["media_player_speaker"]) is TuyaLocalMediaPlayer
  37. )
  38. m_add_entities.assert_called_once()
  39. @pytest.mark.asyncio
  40. async def test_init_entry_fails_if_device_has_no_media_player(hass):
  41. """Test initialisation when device has no matching entity"""
  42. entry = MockConfigEntry(
  43. domain=DOMAIN,
  44. data={
  45. CONF_TYPE: "smartplugv1",
  46. CONF_DEVICE_ID: "dummy",
  47. CONF_PROTOCOL_VERSION: "auto",
  48. },
  49. )
  50. # although async, the async_add_entities function passed to
  51. # async_setup_entry is called truly asynchronously. If we use
  52. # AsyncMock, it expects us to await the result.
  53. m_add_entities = Mock()
  54. m_device = AsyncMock()
  55. hass.data[DOMAIN] = {"dummy": {"device": m_device}}
  56. try:
  57. await async_setup_entry(hass, entry, m_add_entities)
  58. assert False, "Expected async_setup_entry to raise a ValueError"
  59. except ValueError:
  60. pass
  61. m_add_entities.assert_not_called()
  62. @pytest.mark.asyncio
  63. async def test_init_entry_fails_if_config_is_missing(hass):
  64. """Test initialisation when device has no matching entity"""
  65. entry = MockConfigEntry(
  66. domain=DOMAIN,
  67. data={
  68. CONF_TYPE: "non_existing",
  69. CONF_DEVICE_ID: "dummy",
  70. CONF_PROTOCOL_VERSION: "auto",
  71. },
  72. )
  73. # although async, the async_add_entities function passed to
  74. # async_setup_entry is called truly asynchronously. If we use
  75. # AsyncMock, it expects us to await the result.
  76. m_add_entities = Mock()
  77. m_device = AsyncMock()
  78. hass.data[DOMAIN] = {"dummy": {"device": m_device}}
  79. try:
  80. await async_setup_entry(hass, entry, m_add_entities)
  81. assert False, "Expected async_setup_entry to raise a ValueError"
  82. except ValueError:
  83. pass
  84. m_add_entities.assert_not_called()
  85. # Most features are simple mappings to dps values, but state can be more complex
  86. class TestMediaPlayerState:
  87. """Test the state property of the media_player entity."""
  88. @pytest.mark.asyncio
  89. async def async_test_state(self, hass, mocker):
  90. """Test the state property."""
  91. dps = {"82": True}
  92. entry = MockConfigEntry(
  93. domain=DOMAIN,
  94. data={
  95. CONF_TYPE: "ekaza_minipad_controlpanel",
  96. CONF_DEVICE_ID: "dummy",
  97. CONF_PROTOCOL_VERSION: "auto",
  98. },
  99. )
  100. m_add_entities = Mock()
  101. m_device = mock_device(dps, mocker)
  102. hass.data[DOMAIN] = {"dummy": {"device": m_device}}
  103. await async_setup_entry(hass, entry, m_add_entities)
  104. media_player = hass.data[DOMAIN]["dummy"]["media_player_speaker"]
  105. # Test that the state is correct when the device is playing
  106. assert media_player.state == MediaPlayerState.PLAYING