-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gearbest sensor #10556
Gearbest sensor #10556
Changes from 2 commits
7271f9f
2b6a206
2342155
d1e095d
ad39cda
6298219
e8753b4
1f7d785
085d4b9
c9f33c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
""" | ||
Parse prices of a item from gearbest. | ||
For more details about this platform, please refer to the documentation at | ||
https://home-assistant.io/components/sensor.gearbest/ | ||
""" | ||
import logging | ||
import asyncio | ||
from datetime import timedelta | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.util import Throttle | ||
from homeassistant.helpers.entity import Entity | ||
|
||
REQUIREMENTS = ['gearbest_parser==1.0.4'] | ||
_LOGGER = logging.getLogger(__name__) | ||
|
||
CONF_ITEMS = 'items' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to factor these out. However, at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out! What do you think about using CONF_ENTITIES instead of items? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the context of home assistant, |
||
CONF_CURRENCY = 'currency' | ||
|
||
ICON = 'mdi:coin' | ||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=2*60*60) #2h * 60min * 60sec | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at least two spaces before inline comment |
||
|
||
_ITEM_SCHEMA = vol.Schema({ | ||
vol.Optional("url"): cv.string, | ||
vol.Optional("id"): cv.string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all items can be optional, at least something should be required or else how can one represent an item? |
||
vol.Optional("name"): cv.string, | ||
vol.Optional("currency"): cv.string | ||
}) | ||
|
||
_ITEMS_SCHEMA = vol.Schema([_ITEM_SCHEMA]) | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
vol.Required(CONF_ITEMS): _ITEMS_SCHEMA, | ||
vol.Required(CONF_CURRENCY): cv.string, | ||
}) | ||
|
||
|
||
@asyncio.coroutine | ||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | ||
"""Set up the Gearbest sensor.""" | ||
|
||
del discovery_info #unused | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at least two spaces before inline comment |
||
|
||
hass.loop.run_in_executor(None, _add_items, hass, config, async_add_devices) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (80 > 79 characters) |
||
|
||
def _add_items(hass, config, async_add_devices): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 2 blank lines, found 1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can just be your |
||
currency = config.get(CONF_CURRENCY) | ||
|
||
sensors = [] | ||
items = config.get(CONF_ITEMS) | ||
for item in items: | ||
try: | ||
sensor = GearbestSensor(hass, item, currency) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sensor = GearbestSensor(converter, item, currency) |
||
if sensor is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can't happen. |
||
sensors.append(sensor) | ||
except AttributeError as exc: | ||
_LOGGER.error(exc) | ||
|
||
async_add_devices(sensors) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't call async methods from sync context |
||
|
||
|
||
class GearbestSensor(Entity): | ||
"""Implementation of the sensor.""" | ||
|
||
def __init__(self, hass, item, currency): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't pass in def __init__(self, converter, item, currency): |
||
"""Initialize the sensor.""" | ||
|
||
from gearbest_parser import GearbestParser | ||
|
||
self._hass = hass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. |
||
self._name = item.get("name", None) | ||
self._parser = GearbestParser() | ||
self._parser.update_conversion_list() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parser is updating this list once per entity. Instead can it be shared among entities? |
||
self._item = self._parser.load(item.get("id", None), | ||
item.get("url", None), | ||
item.get("currency", currency)) | ||
if self._item is None: | ||
raise AttributeError("id and url could not be resolved") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AttributeError? A ValueError maybe. |
||
self._item.update() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this, and instead call add_devices(sensors, True) |
||
|
||
@property | ||
def name(self): | ||
"""Return the name of the item.""" | ||
return self._name if self._name is not None else self._item.name | ||
|
||
@property | ||
def icon(self): | ||
"""Return the icon for the frontend.""" | ||
return ICON | ||
|
||
@property | ||
def state(self): | ||
"""Return the price of the selected product.""" | ||
return self._item.price | ||
|
||
@property | ||
def unit_of_measurement(self): | ||
"""Return the currency """ | ||
return self._item.currency | ||
|
||
@property | ||
def device_state_attributes(self): | ||
"""Return the state attributes.""" | ||
attrs = {'name': self._item.name, | ||
'description': self._item.description, | ||
'image': self._item.image, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't make this an attribute, instead make it the |
||
'price': self._item.price, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Price is the state, so you don't need that here. |
||
'currency': self._item.currency, | ||
'url': self._item.url} | ||
return attrs | ||
|
||
@Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
def update(self): | ||
"""Get the latest price from gearbest and updates the state.""" | ||
self._hass.loop.run_in_executor(None, self._parser.update_conversion_list) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (82 > 79 characters) |
||
self._hass.loop.run_in_executor(None, self._item.update) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'asyncio' imported but unused