2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Pocket Casts."""
|
2017-02-18 21:52:37 +00:00
|
|
|
from datetime import timedelta
|
2019-10-22 05:36:46 +00:00
|
|
|
import logging
|
2017-02-18 21:52:37 +00:00
|
|
|
|
2020-12-15 03:10:10 +00:00
|
|
|
from pycketcasts import pocketcasts
|
2017-02-18 21:52:37 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2019-10-22 05:36:46 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-02-18 21:52:37 +00:00
|
|
|
|
2017-08-06 08:07:45 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-02-18 21:52:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:rss"
|
2017-08-06 08:07:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_NAME = "Pocketcasts unlistened episodes"
|
2017-08-06 08:07:45 +00:00
|
|
|
|
2017-02-18 21:52:37 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=5)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_USERNAME): cv.string}
|
|
|
|
)
|
2017-08-06 08:07:45 +00:00
|
|
|
|
2017-02-18 21:52:37 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the pocketcasts platform for sensors."""
|
2017-08-06 08:07:45 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
2017-02-18 21:52:37 +00:00
|
|
|
try:
|
2020-12-15 03:10:10 +00:00
|
|
|
api = pocketcasts.PocketCast(email=username, password=password)
|
|
|
|
_LOGGER.debug("Found %d podcasts", len(api.subscriptions))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([PocketCastsSensor(api)], True)
|
2017-02-18 21:52:37 +00:00
|
|
|
except OSError as err:
|
2017-08-06 08:07:45 +00:00
|
|
|
_LOGGER.error("Connection to server failed: %s", err)
|
2017-02-18 21:52:37 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class PocketCastsSensor(SensorEntity):
|
2017-02-18 21:52:37 +00:00
|
|
|
"""Representation of a pocket casts sensor."""
|
|
|
|
|
|
|
|
def __init__(self, api):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._api = api
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@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
|
2017-08-06 08:07:45 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update sensor values."""
|
|
|
|
try:
|
2020-12-15 03:10:10 +00:00
|
|
|
self._state = len(self._api.new_releases)
|
2017-08-06 08:07:45 +00:00
|
|
|
_LOGGER.debug("Found %d new episodes", self._state)
|
|
|
|
except OSError as err:
|
|
|
|
_LOGGER.warning("Failed to contact server: %s", err)
|