class GearbestSensor(Entity): """Implementation of the sensor.""" def __init__(self, hass, item, currency): """Initialize the sensor.""" from gearbest_parser import GearbestParser self._hass = hass self._name = item.get(CONF_NAME, None) self._parser = GearbestParser() self._parser.set_currency_converter(hass.data[DOMAIN]) self._item = self._parser.load(item.get(CONF_ID, None), item.get(CONF_URL, None), item.get(CONF_CURRENCY, currency)) if self._item is None: raise ValueError("id and url could not be resolved") self._item.update() @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 entity_picture(self): """Return the image.""" return self._item.image @property def device_state_attributes(self): """Return the state attributes.""" attrs = { 'name': self._item.name, 'description': self._item.description, 'price': self._item.price, '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._item.update()
class GearbestSensor(Entity): """Implementation of the sensor.""" def __init__(self, hass, item, currency): """Initialize the sensor.""" from gearbest_parser import GearbestParser self._hass = hass self._name = item.get("name", None) self._parser = GearbestParser() self._parser.update_conversion_list() 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") self._item.update() @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, 'price': self._item.price, '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) self._hass.loop.run_in_executor(None, self._item.update)
class GearbestSensor(Entity): """Implementation of the sensor.""" def __init__(self, converter, item, currency): """Initialize the sensor.""" from gearbest_parser import GearbestParser self._name = item.get(CONF_NAME) self._parser = GearbestParser() self._parser.set_currency_converter(converter) self._item = self._parser.load(item.get(CONF_ID), item.get(CONF_URL), item.get(CONF_CURRENCY, currency)) if self._item is None: raise ValueError("id and url could not be resolved") @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 entity_picture(self): """Return the image.""" return self._item.image @property def device_state_attributes(self): """Return the state attributes.""" attrs = {'name': self._item.name, 'description': self._item.description, '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._item.update()