2023-09-27 15:45:52 +02:00
|
|
|
"""The tests for an update of the Twitch component."""
|
2024-03-08 14:44:56 +01:00
|
|
|
|
2023-09-27 15:45:52 +02:00
|
|
|
from datetime import datetime
|
2024-05-27 10:09:12 +02:00
|
|
|
from unittest.mock import AsyncMock
|
2023-09-27 15:45:52 +02:00
|
|
|
|
2024-07-05 17:33:04 -03:00
|
|
|
from dateutil.tz import tzutc
|
2024-05-27 10:09:12 +02:00
|
|
|
from twitchAPI.object.api import FollowedChannel, Stream, UserSubscription
|
|
|
|
|
from twitchAPI.type import TwitchResourceNotFound
|
2023-09-27 15:45:52 +02:00
|
|
|
|
2024-12-16 09:54:01 +01:00
|
|
|
from homeassistant.components.twitch.const import DOMAIN
|
2023-09-27 15:45:52 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
2024-05-27 10:09:12 +02:00
|
|
|
from . import TwitchIterObject, get_generator_from_data, setup_integration
|
|
|
|
|
|
2025-05-28 12:09:05 +02:00
|
|
|
from tests.common import MockConfigEntry, async_load_json_object_fixture
|
2023-09-27 15:45:52 +02:00
|
|
|
|
|
|
|
|
ENTITY_ID = "sensor.channel123"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_offline(
|
2024-05-27 10:09:12 +02:00
|
|
|
hass: HomeAssistant, twitch_mock: AsyncMock, config_entry: MockConfigEntry
|
2023-09-27 15:45:52 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Test offline state."""
|
2024-10-24 13:51:19 -04:00
|
|
|
twitch_mock.return_value.get_followed_streams.return_value = (
|
|
|
|
|
get_generator_from_data([], Stream)
|
2024-05-27 10:09:12 +02:00
|
|
|
)
|
2023-09-27 15:45:52 +02:00
|
|
|
await setup_integration(hass, config_entry)
|
|
|
|
|
|
|
|
|
|
sensor_state = hass.states.get(ENTITY_ID)
|
|
|
|
|
assert sensor_state.state == "offline"
|
|
|
|
|
assert sensor_state.attributes["entity_picture"] == "logo.png"
|
2025-11-23 10:29:06 -05:00
|
|
|
assert sensor_state.attributes["channel_picture"] == "logo.png"
|
2023-09-27 15:45:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_streaming(
|
2024-05-27 10:09:12 +02:00
|
|
|
hass: HomeAssistant, twitch_mock: AsyncMock, config_entry: MockConfigEntry
|
2023-09-27 15:45:52 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Test streaming state."""
|
|
|
|
|
await setup_integration(hass, config_entry)
|
|
|
|
|
|
|
|
|
|
sensor_state = hass.states.get(ENTITY_ID)
|
|
|
|
|
assert sensor_state.state == "streaming"
|
|
|
|
|
assert sensor_state.attributes["entity_picture"] == "stream-medium.png"
|
2025-11-23 10:29:06 -05:00
|
|
|
assert sensor_state.attributes["channel_picture"] == "logo.png"
|
2023-09-27 15:45:52 +02:00
|
|
|
assert sensor_state.attributes["game"] == "Good game"
|
|
|
|
|
assert sensor_state.attributes["title"] == "Title"
|
2024-07-05 17:33:04 -03:00
|
|
|
assert sensor_state.attributes["started_at"] == datetime(
|
|
|
|
|
year=2021, month=3, day=10, hour=3, minute=18, second=11, tzinfo=tzutc()
|
|
|
|
|
)
|
2024-10-20 16:07:43 +02:00
|
|
|
assert sensor_state.attributes["viewers"] == 42
|
2023-09-27 15:45:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_oauth_without_sub_and_follow(
|
2024-05-27 10:09:12 +02:00
|
|
|
hass: HomeAssistant, twitch_mock: AsyncMock, config_entry: MockConfigEntry
|
2023-09-27 15:45:52 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Test state with oauth."""
|
2024-05-27 10:09:12 +02:00
|
|
|
twitch_mock.return_value.get_followed_channels.return_value = TwitchIterObject(
|
2025-06-02 09:27:53 +02:00
|
|
|
hass, "empty_response.json", FollowedChannel
|
2024-05-27 10:09:12 +02:00
|
|
|
)
|
|
|
|
|
twitch_mock.return_value.check_user_subscription.side_effect = (
|
|
|
|
|
TwitchResourceNotFound
|
|
|
|
|
)
|
2023-09-27 15:45:52 +02:00
|
|
|
await setup_integration(hass, config_entry)
|
|
|
|
|
|
|
|
|
|
sensor_state = hass.states.get(ENTITY_ID)
|
|
|
|
|
assert sensor_state.attributes["subscribed"] is False
|
|
|
|
|
assert sensor_state.attributes["following"] is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_oauth_with_sub(
|
2024-05-27 10:09:12 +02:00
|
|
|
hass: HomeAssistant, twitch_mock: AsyncMock, config_entry: MockConfigEntry
|
2023-09-27 15:45:52 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Test state with oauth and sub."""
|
2024-05-27 10:09:12 +02:00
|
|
|
twitch_mock.return_value.get_followed_channels.return_value = TwitchIterObject(
|
2025-06-02 09:27:53 +02:00
|
|
|
hass, "empty_response.json", FollowedChannel
|
2024-05-27 10:09:12 +02:00
|
|
|
)
|
2025-05-28 12:09:05 +02:00
|
|
|
subscription = await async_load_json_object_fixture(
|
|
|
|
|
hass, "check_user_subscription_2.json", DOMAIN
|
|
|
|
|
)
|
2024-05-27 10:09:12 +02:00
|
|
|
twitch_mock.return_value.check_user_subscription.return_value = UserSubscription(
|
2025-05-28 12:09:05 +02:00
|
|
|
**subscription
|
2024-05-27 10:09:12 +02:00
|
|
|
)
|
2023-09-27 15:45:52 +02:00
|
|
|
await setup_integration(hass, config_entry)
|
|
|
|
|
|
|
|
|
|
sensor_state = hass.states.get(ENTITY_ID)
|
|
|
|
|
assert sensor_state.attributes["subscribed"] is True
|
|
|
|
|
assert sensor_state.attributes["subscription_is_gifted"] is False
|
2024-10-21 15:54:10 -04:00
|
|
|
assert sensor_state.attributes["subscription_tier"] == 1
|
2023-09-27 15:45:52 +02:00
|
|
|
assert sensor_state.attributes["following"] is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_oauth_with_follow(
|
2024-05-27 10:09:12 +02:00
|
|
|
hass: HomeAssistant, twitch_mock: AsyncMock, config_entry: MockConfigEntry
|
2023-09-27 15:45:52 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Test state with oauth and follow."""
|
|
|
|
|
await setup_integration(hass, config_entry)
|
|
|
|
|
|
|
|
|
|
sensor_state = hass.states.get(ENTITY_ID)
|
|
|
|
|
assert sensor_state.attributes["following"] is True
|
|
|
|
|
assert sensor_state.attributes["following_since"] == datetime(
|
|
|
|
|
year=2023, month=8, day=1
|
|
|
|
|
)
|