diff --git a/.coveragerc b/.coveragerc index 9a1a3125740..3393e4f624c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -343,6 +343,7 @@ omit = homeassistant/components/sensor/openweathermap.py homeassistant/components/sensor/pi_hole.py homeassistant/components/sensor/plex.py + homeassistant/components/sensor/pocketcasts.py homeassistant/components/sensor/pvoutput.py homeassistant/components/sensor/qnap.py homeassistant/components/sensor/sabnzbd.py diff --git a/homeassistant/components/sensor/pocketcasts.py b/homeassistant/components/sensor/pocketcasts.py new file mode 100644 index 00000000000..5ccd082ef89 --- /dev/null +++ b/homeassistant/components/sensor/pocketcasts.py @@ -0,0 +1,81 @@ +""" +Support for Pocket Casts. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.pocketcasts/ +""" +import logging + +from datetime import timedelta + +import voluptuous as vol + +from homeassistant.helpers.entity import Entity +import homeassistant.helpers.config_validation as cv +from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD) +from homeassistant.components.sensor import (PLATFORM_SCHEMA) + +_LOGGER = logging.getLogger(__name__) + +COMMIT = '9f61ff00c77c7c98ffa0af9dd3540df3dce4a836' +REQUIREMENTS = ['https://github.com/molobrakos/python-pocketcasts/' + 'archive/%s.zip#python-pocketcasts==0.0.1' % COMMIT] + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_USERNAME): cv.string, + vol.Required(CONF_PASSWORD): cv.string, +}) + +ICON = 'mdi:rss' +SENSOR_NAME = 'Pocketcasts unlistened episodes' +SCAN_INTERVAL = timedelta(minutes=5) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the pocketcasts platform for sensors.""" + import pocketcasts + try: + api = pocketcasts.Api( + config.get(CONF_USERNAME), + config.get(CONF_PASSWORD)) + _LOGGER.debug('Found %d podcasts', + len(api.my_podcasts())) + add_devices([PocketCastsSensor(api)]) + return True + except OSError as err: + _LOGGER.error('Failed to contact server ' + '(wrong credentials?): %s', err) + return False + + +class PocketCastsSensor(Entity): + """Representation of a pocket casts sensor.""" + + def __init__(self, api): + """Initialize the sensor.""" + self._api = api + self._state = None + self.update() + + def update(self): + """Update sensor values.""" + try: + self._state = len(self._api.new_episodes_released()) + _LOGGER.debug('Found %d new episodes', self._state) + except OSError as err: + _LOGGER.warning('Failed to contact server: %s', err) + + @property + def name(self): + """Return the name of the sensor.""" + return SENSOR_NAME + + @property + def state(self): + """Return the sensor state.""" + return self._state + + @property + def icon(self): + """Return the icon for the sensor.""" + return ICON diff --git a/requirements_all.txt b/requirements_all.txt index bfdeabfe51f..53ab49d56a7 100755 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -247,6 +247,9 @@ https://github.com/joopert/nad_receiver/archive/0.0.3.zip#nad_receiver==0.0.3 # homeassistant.components.media_player.russound_rnet https://github.com/laf/russound/archive/0.1.7.zip#russound==0.1.7 +# homeassistant.components.sensor.pocketcasts +https://github.com/molobrakos/python-pocketcasts/archive/9f61ff00c77c7c98ffa0af9dd3540df3dce4a836.zip#python-pocketcasts==0.0.1 + # homeassistant.components.switch.anel_pwrctrl https://github.com/mweinelt/anel-pwrctrl/archive/ed26e8830e28a2bfa4260a9002db23ce3e7e63d7.zip#anel_pwrctrl==0.0.1