Move Twitch constants to separate file (#92605)

* Move Twitch constants to separate file

* Move Twitch constants to separate file

* Move Twitch constants to separate file

* Update branch
This commit is contained in:
Joost Lekkerkerker 2023-05-24 09:18:20 +02:00 committed by GitHub
parent 9363fa6561
commit 326d80f4f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 22 deletions

View File

@ -0,0 +1,10 @@
"""Const for Twitch."""
import logging
from twitchAPI.twitch import AuthScope
LOGGER = logging.getLogger(__package__)
CONF_CHANNELS = "channels"
OAUTH_SCOPES = [AuthScope.USER_READ_SUBSCRIPTIONS]

View File

@ -1,11 +1,8 @@
"""Support for the Twitch stream status.""" """Support for the Twitch stream status."""
from __future__ import annotations from __future__ import annotations
import logging
from twitchAPI.helper import first from twitchAPI.helper import first
from twitchAPI.twitch import ( from twitchAPI.twitch import (
AuthScope,
AuthType, AuthType,
InvalidTokenException, InvalidTokenException,
MissingScopeException, MissingScopeException,
@ -24,7 +21,17 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__) from .const import CONF_CHANNELS, LOGGER, OAUTH_SCOPES
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_CLIENT_ID): cv.string,
vol.Required(CONF_CLIENT_SECRET): cv.string,
vol.Required(CONF_CHANNELS): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_TOKEN): cv.string,
}
)
ATTR_GAME = "game" ATTR_GAME = "game"
ATTR_TITLE = "title" ATTR_TITLE = "title"
@ -36,24 +43,11 @@ ATTR_FOLLOW_SINCE = "following_since"
ATTR_FOLLOWING = "followers" ATTR_FOLLOWING = "followers"
ATTR_VIEWS = "views" ATTR_VIEWS = "views"
CONF_CHANNELS = "channels"
ICON = "mdi:twitch" ICON = "mdi:twitch"
STATE_OFFLINE = "offline" STATE_OFFLINE = "offline"
STATE_STREAMING = "streaming" STATE_STREAMING = "streaming"
OAUTH_SCOPES = [AuthScope.USER_READ_SUBSCRIPTIONS]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_CLIENT_ID): cv.string,
vol.Required(CONF_CLIENT_SECRET): cv.string,
vol.Required(CONF_CHANNELS): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_TOKEN): cv.string,
}
)
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -75,7 +69,7 @@ async def async_setup_platform(
) )
client.auto_refresh_auth = False client.auto_refresh_auth = False
except TwitchAuthorizationException: except TwitchAuthorizationException:
_LOGGER.error("Invalid client ID or client secret") LOGGER.error("Invalid client ID or client secret")
return return
if oauth_token: if oauth_token:
@ -84,10 +78,10 @@ async def async_setup_platform(
token=oauth_token, scope=OAUTH_SCOPES, validate=True token=oauth_token, scope=OAUTH_SCOPES, validate=True
) )
except MissingScopeException: except MissingScopeException:
_LOGGER.error("OAuth token is missing required scope") LOGGER.error("OAuth token is missing required scope")
return return
except InvalidTokenException: except InvalidTokenException:
_LOGGER.error("OAuth token is invalid") LOGGER.error("OAuth token is invalid")
return return
twitch_users: list[TwitchUser] = [] twitch_users: list[TwitchUser] = []
@ -151,9 +145,9 @@ class TwitchSensor(SensorEntity):
self._attr_extra_state_attributes[ATTR_SUBSCRIPTION] = True self._attr_extra_state_attributes[ATTR_SUBSCRIPTION] = True
self._attr_extra_state_attributes[ATTR_SUBSCRIPTION_GIFTED] = sub.is_gift self._attr_extra_state_attributes[ATTR_SUBSCRIPTION_GIFTED] = sub.is_gift
except TwitchResourceNotFound: except TwitchResourceNotFound:
_LOGGER.debug("User is not subscribed") LOGGER.debug("User is not subscribed")
except TwitchAPIException as exc: except TwitchAPIException as exc:
_LOGGER.error("Error response on check_user_subscription: %s", exc) LOGGER.error("Error response on check_user_subscription: %s", exc)
follows = ( follows = (
await self._client.get_users_follows( await self._client.get_users_follows(