core/homeassistant/components/socialblade/sensor.py

86 lines
2.2 KiB
Python
Raw Normal View History

"""Support for Social Blade."""
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
from datetime import timedelta
import logging
import socialbladeclient
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CHANNEL_ID = "channel_id"
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
DEFAULT_NAME = "Social Blade"
MIN_TIME_BETWEEN_UPDATES = timedelta(hours=2)
2019-07-31 19:25:30 +00:00
SUBSCRIBERS = "subscribers"
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
2019-07-31 19:25:30 +00:00
TOTAL_VIEWS = "total_views"
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CHANNEL_ID): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
"""Set up the Social Blade sensor."""
2019-07-31 19:25:30 +00:00
social_blade = SocialBladeSensor(config[CHANNEL_ID], config[CONF_NAME])
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
social_blade.update()
if social_blade.valid_channel_id is False:
return
add_entities([social_blade])
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
class SocialBladeSensor(Entity):
"""Representation of a Social Blade Sensor."""
def __init__(self, case, name):
"""Initialize the Social Blade sensor."""
self._state = None
self.channel_id = case
self._attributes = None
self.valid_channel_id = None
self._name = name
@property
def name(self):
"""Return the name."""
return self._name
@property
def state(self):
"""Return the state."""
return self._state
@property
def device_state_attributes(self):
"""Return the state attributes."""
if self._attributes:
return self._attributes
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data from Social Blade."""
2019-07-31 19:25:30 +00:00
Add Social Blade Sensor (#14060) * Adding Dominion Energy Sensor * Update : remove white spacves and set the update time to be daily * Update : update spacing as per hound suggestions, Move imports * Update : Fix Travis CI build errors * Update Documentations on method levels * Update Documentations on method levels * Update Documentations on method levels * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Add Exception Handeling if login failed, add PLATFORM_SCHEMA * Update dominionenergy.py * Adding Selenium to requirements_all.txt * Checking the username/password while setup * Checking the username/password while setup * removing extra white space * Update : Adding the Platform only if credentials works * Update : Add PlatformNotReady exception * Update : Add PlatformNotReady exception * Update .coveragerc * Remove change * Adding USCIS component * Adding Line after the class DOC * Update : Extract USCIS logic code to Component * Update : Extract USCIS logic code to Component * Adding CURRENT_STATUS * Change Error handling, remove date from attributes * Update the Version for USCIS * Add Social Blade Sensor * Update class documentation * Update coverage and requirements_all * Update : houndci error with intent * Update : Add coverage * Update uscis.py * Add comments * Add comments * Delete dominionenergy.py * Update requirements_all.txt * Update .coveragerc * Update .coveragerc * Update .coveragerc * Update : update after code review * Fix remaining issues
2018-05-01 20:27:20 +00:00
try:
data = socialbladeclient.get_data(self.channel_id)
self._attributes = {TOTAL_VIEWS: data[TOTAL_VIEWS]}
self._state = data[SUBSCRIBERS]
self.valid_channel_id = True
except (ValueError, IndexError):
_LOGGER.error("Unable to find valid channel ID")
self.valid_channel_id = False
self._attributes = None