mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Do not call update() in constructor (#8847)
This commit is contained in:
parent
058deb5be3
commit
569d9764ab
@ -10,39 +10,40 @@ from datetime import timedelta
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD)
|
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD)
|
||||||
from homeassistant.components.sensor import (PLATFORM_SCHEMA)
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
REQUIREMENTS = ['pocketcasts==0.1']
|
REQUIREMENTS = ['pocketcasts==0.1']
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
_LOGGER = logging.getLogger(__name__)
|
||||||
vol.Required(CONF_USERNAME): cv.string,
|
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
|
||||||
})
|
|
||||||
|
|
||||||
ICON = 'mdi:rss'
|
ICON = 'mdi:rss'
|
||||||
|
|
||||||
SENSOR_NAME = 'Pocketcasts unlistened episodes'
|
SENSOR_NAME = 'Pocketcasts unlistened episodes'
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(minutes=5)
|
SCAN_INTERVAL = timedelta(minutes=5)
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
|
vol.Required(CONF_USERNAME): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the pocketcasts platform for sensors."""
|
"""Set up the pocketcasts platform for sensors."""
|
||||||
import pocketcasts
|
import pocketcasts
|
||||||
|
|
||||||
|
username = config.get(CONF_USERNAME)
|
||||||
|
password = config.get(CONF_PASSWORD)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
api = pocketcasts.Api(
|
api = pocketcasts.Api(username, password)
|
||||||
config.get(CONF_USERNAME),
|
_LOGGER.debug("Found %d podcasts", len(api.my_podcasts()))
|
||||||
config.get(CONF_PASSWORD))
|
add_devices([PocketCastsSensor(api)], True)
|
||||||
_LOGGER.debug('Found %d podcasts',
|
|
||||||
len(api.my_podcasts()))
|
|
||||||
add_devices([PocketCastsSensor(api)])
|
|
||||||
return True
|
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
_LOGGER.error('Failed to contact server '
|
_LOGGER.error("Connection to server failed: %s", err)
|
||||||
'(wrong credentials?): %s', err)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -53,15 +54,6 @@ class PocketCastsSensor(Entity):
|
|||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._api = api
|
self._api = api
|
||||||
self._state = None
|
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
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -77,3 +69,11 @@ class PocketCastsSensor(Entity):
|
|||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon for the sensor."""
|
"""Return the icon for the sensor."""
|
||||||
return ICON
|
return ICON
|
||||||
|
|
||||||
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user