コード例 #1
0
ファイル: sensor.py プロジェクト: Naesstrom/sensor.untappd
class UntappdLastBadgeSensor(Entity):
    def __init__(self, hass, username, api_id, api_secret):
        from pyuntappd import Untappd
        self.hass = hass
        self._untappd = Untappd()
        self._username = username
        self._apiid = api_id
        self._apisecret = api_secret
        self._state = None
        self.update()

    def update(self):
        result = self._untappd.get_badges(self._apiid, self._apisecret,
                                          self._username)
        if not result or len(result) < 1:
            return False
        else:
            current_date = parser.parse(str(
                datetime.now())).replace(tzinfo=None)
            checkin_date = parser.parse(
                result[0]['created_at']).replace(tzinfo=None)
            if (current_date - checkin_date).days > 0:
                relative_checkin_date = str(
                    (current_date - checkin_date).days + 1) + " days ago"
            elif (current_date - checkin_date).days == 0:
                relative_checkin_date = 'Yesterday'
            else:
                relative_checkin_date = 'Today'

            self._state = relative_checkin_date
            self._badge = result[0]['badge_name']
            self._level = result[0]['levels']['count'] if result[0][
                'is_level'] else 1
            self._description = result[0]['badge_description']
            self._picture = result[0]['media']['badge_image_sm']

    @property
    def name(self):
        return 'Untappd Last Badge (' + self._username + ')'

    @property
    def entity_picture(self):
        return self._picture

    @property
    def state(self):
        return self._state

    @property
    def icon(self):
        return ICON

    @property
    def device_state_attributes(self):
        return {
            ATTR_BADGE: self._badge,
            ATTR_LEVEL: self._level,
            ATTR_DESCRIPTION: self._description,
            ATTR_ATTRIBUTION: ATTRIBUTION,
        }
コード例 #2
0
class UntappdLastBadgeSensor(Entity):
    def __init__(self, hass, username, api_id, api_secret):
        from pyuntappd import Untappd

        self.hass = hass
        self._untappd = Untappd()
        self._username = username
        self._apiid = api_id
        self._apisecret = api_secret
        self._state = None
        self.update()

    def update(self):
        result = self._untappd.get_badges(self._apiid, self._apisecret, self._username)
        if not result or len(result) < 1:
            return False
        else:
            current_date = parser.parse(str(datetime.now())).replace(tzinfo=None)
            checkin_date = parser.parse(result[0]["created_at"]).replace(tzinfo=None)
            if (current_date - checkin_date).days > 0:
                relative_checkin_date = (
                    str((current_date - checkin_date).days + 1) + " days ago"
                )
            elif (current_date - checkin_date).days == 0:
                relative_checkin_date = "Yesterday"
            else:
                relative_checkin_date = "Today"

            self._state = relative_checkin_date
            self._badge = result[0]["badge_name"]
            self._level = result[0]["levels"]["count"] if result[0]["is_level"] else 1
            self._description = result[0]["badge_description"]
            self._picture = result[0]["media"]["badge_image_sm"]

    @property
    def name(self):
        return "Untappd Last Badge (" + self._username + ")"

    @property
    def entity_picture(self):
        return self._picture

    @property
    def state(self):
        return self._state

    @property
    def icon(self):
        return ICON

    @property
    def extra_state_attributes(self):
        return {
            ATTR_BADGE: self._badge,
            ATTR_LEVEL: self._level,
            ATTR_DESCRIPTION: self._description,
            ATTR_ATTRIBUTION: ATTRIBUTION,
        }