Skip to content

Commit

Permalink
Added Xbox Live component (home-assistant#3013)
Browse files Browse the repository at this point in the history
* Added Xbox Live component

* Added Xbox Live sensor to coveralls

* Added init success checks

* Added entity id
  • Loading branch information
mKeRix authored and robbiet480 committed Sep 3, 2016
1 parent 68ef55a commit 269e97c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ omit =
homeassistant/components/sensor/twitch.py
homeassistant/components/sensor/uber.py
homeassistant/components/sensor/worldclock.py
homeassistant/components/sensor/xbox_live.py
homeassistant/components/sensor/yweather.py
homeassistant/components/switch/acer_projector.py
homeassistant/components/switch/arest.py
Expand Down
112 changes: 112 additions & 0 deletions homeassistant/components/sensor/xbox_live.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""
Sensor for Xbox Live account status.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.xbox_live/
"""
import logging
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_API_KEY, STATE_UNKNOWN)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity

_LOGGER = logging.getLogger(__name__)

ICON = 'mdi:xbox'

REQUIREMENTS = ['xboxapi==0.1.1']

CONF_XUID = 'xuid'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_XUID): vol.All(cv.ensure_list, [cv.string])
})


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Xbox platform."""
from xboxapi import xbox_api
api = xbox_api.XboxApi(config.get(CONF_API_KEY))
devices = []

for xuid in config.get(CONF_XUID):
new_device = XboxSensor(hass, api, xuid)
if new_device.success_init:
devices.append(new_device)

if len(devices) > 0:
add_devices(devices)
else:
return False


# pylint: disable=too-many-instance-attributes
class XboxSensor(Entity):
"""A class for the Xbox account."""

def __init__(self, hass, api, xuid):
"""Initialize the sensor."""
self._hass = hass
self._state = STATE_UNKNOWN
self._presence = {}
self._xuid = xuid
self._api = api

# get profile info
profile = self._api.get_user_profile(self._xuid)

if profile.get('success', True) \
and profile.get('code', 0) != 28:
self.success_init = True
self._gamertag = profile.get('Gamertag')
self._picture = profile.get('GameDisplayPicRaw')
else:
self.success_init = False

@property
def name(self):
"""Return the name of the sensor."""
return self._gamertag

@property
def entity_id(self):
"""Return the entity ID."""
return 'sensor.xbox_' + self._gamertag

@property
def state(self):
"""Return the state of the sensor."""
return self._state

@property
def device_state_attributes(self):
"""Return the state attributes."""
attributes = {}
for device in self._presence:
for title in device.get('titles'):
attributes[
'{} {}'.format(device.get('type'),
title.get('placement'))
] = title.get('name')

return attributes

@property
def entity_picture(self):
"""Avatar of the account."""
return self._picture

@property
def icon(self):
"""Return the icon to use in the frontend."""
return ICON

def update(self):
"""Update state data from Xbox API."""
presence = self._api.get_user_presence(self._xuid)
self._state = presence.get('state', STATE_UNKNOWN)
self._presence = presence.get('devices', {})
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ websocket-client==0.37.0
# homeassistant.components.zigbee
xbee-helper==0.0.7

# homeassistant.components.sensor.xbox_live
xboxapi==0.1.1

# homeassistant.components.sensor.swiss_hydrological_data
# homeassistant.components.sensor.yr
xmltodict==0.10.2
Expand Down

0 comments on commit 269e97c

Please sign in to comment.