Skip to content

Commit

Permalink
Merge pull request #12 from niklaswa/master
Browse files Browse the repository at this point in the history
make static image format configurable, add support for animated avatars
  • Loading branch information
LordBoos authored May 11, 2020
2 parents cc23fbc + caa50b7 commit e4d171d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ It creates a sensor for every configured user with online status and game played
sensor:
- platform: discord_game
token: secretDiscordBotToken
image_format: webp # optional, available formats are: png, webp, jpeg, jpg
members:
- Username1#1234
- Username2#1234
Expand All @@ -26,7 +27,7 @@ To create a bot user and get a token:
6. Uncheck PUBLIC BOT
7. Click Save changes on the bottom of the page
8. Under Token, click on Copy
9. Now paste your token to the yaml configuration of HA replacing "secretDiscordBotToken" in the example above
9. Now paste your token to the yaml configuration of HA replacing `secretDiscordBotToken` in the example above

You also need your own discord server (or some server where you have admin rights) and you need to invite the bot to that server.
To do that follow bellow steps:
Expand All @@ -38,5 +39,7 @@ To do that follow bellow steps:
From now on, you can get status of every user on the same server the bot is in.
For every user you want the sensor for, specify his username (including #XXXX) in the members section of yaml configuration.

If you are using Safari or the iOS Home Assistant app, please set the `image_format` to `png`, because Safari doesn't support the `webp` image format.

Thanks to @descention https://github.com/descention for an original component idea and component itself which I've rewrote for current Discord
API and Home Assistant and integrated it with HACS.
12 changes: 5 additions & 7 deletions custom_components/discord_game/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

CONF_TOKEN = 'token'
CONF_MEMBERS = 'members'
CONF_IMAGE_FORMAT = 'image_format'

DOMAIN = 'sensor'

Expand All @@ -24,13 +25,15 @@
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_TOKEN): cv.string,
vol.Required(CONF_MEMBERS, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_IMAGE_FORMAT, default='webp'): vol.In(['png', 'webp', 'jpeg', 'jpg']),
})


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
import discord
token = config.get(CONF_TOKEN)
image_format = config.get(CONF_IMAGE_FORMAT)
bot = discord.Client(loop=hass.loop)
yield from bot.login(token)

Expand Down Expand Up @@ -142,7 +145,7 @@ def update_discord_entity(watcher: DiscordAsyncMemberState, discord_member: Memb
watcher.async_schedule_update_ha_state()

def update_discord_entity_user(watcher: DiscordAsyncMemberState, discord_user: User):
watcher._avatar_id = discord_user.avatar
watcher._avatar_url = discord_user.avatar_url_as(format=None, static_format=image_format, size=1024).__str__()
watcher._user_id = discord_user.id
watcher.async_schedule_update_ha_state(True)

Expand Down Expand Up @@ -204,7 +207,6 @@ def __init__(self, hass, client, member):
self._watching_url = None
self._watching_details = None
self._avatar_url = None
self._avatar_id = None
self._user_id = None
self._custom_status = None
self._custom_emoji = None
Expand Down Expand Up @@ -256,8 +258,4 @@ def device_state_attributes(self):
'watching_details': self._watching_details,
'custom_status': self._custom_status,
'custom_emoji': self._custom_emoji
}

def update(self):
if self._user_id is not None and self._avatar_id is not None:
self._avatar_url = 'https://cdn.discordapp.com/avatars/' + str(self._user_id) + '/' + str(self._avatar_id) + '.webp?size=1024'
}
5 changes: 4 additions & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ It creates a sensor for every configured user with online status and game played
sensor:
- platform: discord_game
token: secretDiscordBotToken
image_format: webp # optional, available formats are: png, webp, jpeg, jpg
members:
- Username1#1234
- Username2#1234
Expand All @@ -26,7 +27,7 @@ To create a bot user and get a token:
6. Uncheck PUBLIC BOT
7. Click Save changes on the bottom of the page
8. Under Token, click on Copy
9. Now paste your token to the yaml configuration of HA replacing "secretDiscordBotToken" in the example above
9. Now paste your token to the yaml configuration of HA replacing `secretDiscordBotToken` in the example above

You also need your own discord server (or some server where you have admin rights) and you need to invite the bot to that server.
To do that follow bellow steps:
Expand All @@ -38,5 +39,7 @@ To do that follow bellow steps:
From now on, you can get status of every user on the same server the bot is in.
For every user you want the sensor for, specify his username (including #XXXX) in the members section of yaml configuration.

If you are using Safari or the iOS Home Assistant app, please set the `image_format` to `png`, because Safari doesn't support the `webp` image format.

Thanks to @descention https://github.com/descention for an original component idea and component itself which I've rewrote for current Discord
API and Home Assistant and integrated it with HACS.

0 comments on commit e4d171d

Please sign in to comment.