<commit_before>"""
Objects for interfacing with the Wink API
"""
import logging
import json
import time
import requests

BASE_URL = "https://winkapi.quirky.com"

HEADERS = {}


class WinkDevice(object):

    @staticmethod
    def factory(device_state_as_json):
        if "light_bulb_id" in device_state_as_json:
            return WinkBulb(device_state_as_json)
        elif "sensor_pod_id" in device_state_as_json:
            return WinkSensorPod(device_state_as_json)
        elif "binary_switch_id" in device_state_as_json:
            return WinkBinarySwitch(device_state_as_json)
        elif "lock_id" in device_state_as_json:
            return WinkLock(device_state_as_json)
        elif "eggtray_id" in device_state_as_json:
            return WinkEggTray(device_state_as_json)
        # elif "thermostat_id" in aJSonObj:
        # elif "remote_id" in aJSonObj:
        return WinkDevice(device_state_as_json)

    def __init__(self, device_state_as_json, objectprefix=None):
        self.objectprefix = objectprefix
        self.json_state = device_state_as_json

    def __str__(self):
        return "%s %s %s" % (self.name(), self.device_id(), self.state())

    def __repr__(self):
        return "<Wink object %s %s %s>" % (self.name(), self.device_id(), self.state())

    def name(self):
        return self.json_state.get('name', "Unknown Name")

    def state(self):
        raise NotImplementedError("Must implement state")

    def device_id(self):
        raise NotImplementedError("Must implement device_id")

    @property
    def _last_reading(self):
        return self.json_state.get('last_reading') or {}

    def _update_state_from_response(self, response_json):
        """
        :param response_json: the json obj returned from query
        :return:
        """
        self.json_state = response_json.get('data')

    def update_state(self):
        """ Update state with latest info from Wink API. """
        url_string = "{}/{}/{}".format(BASE_URL, self.objectprefix, self.device_id())
        arequest = requests.get(url_string, headers=HEADERS)
        self._update_state_from_response(arequest.json())

    def refresh_state_at_hub(self):
        """
        Tell hub to query latest status from device and upload to Wink.
        PS: Not sure if this even works..
        """
        url_string = "{}/{}/{}/refresh".format(BASE_URL, self.objectprefix, self.device_id())
        requests.get(url_string, headers=HEADERS)


class WinkEggTray(WinkDevice):
    """ represents a wink.py egg tray
    json_obj holds the json stat at init (and if there is a refresh it's updated
    it's the native format for this objects methods
    and looks like so:
{
    "data": {
        "last_reading": {
            "connection": true,
            "connection_updated_at": 1417823487.490747,
            "battery": 0.83,
            "battery_updated_at": 1417823487.490747,
            "inventory": 3,
            "inventory_updated_at": 1449705551.7313306,
            "freshness_remaining": 2419191,
            "freshness_remaining_updated_at": 1449705551.7313495,
            "age_updated_at": 1449705551.7313418,
            "age": 1449705542,
            "connection_changed_at": 1449705443.6858568,
            "next_trigger_at_updated_at": None,
            "next_trigger_at": None,
            "egg_1_timestamp_updated_at": 1449753143.8631344,
            "egg_1_timestamp_changed_at": 1449705534.0782206,
            "egg_1_timestamp": 1449705545.0,
            "egg_2_timestamp_updated_at": 1449753143.8631344,
            "egg_2_timestamp_changed_at": 1449705534.0782206,
            "egg_2_timestamp": 1449705545.0,
            "egg_3_timestamp_updated_at": 1449753143.8631344,
            "egg_3_timestamp_changed_at": 1449705534.0782206,
            "egg_3_timestamp": 1449705545.0,
            "egg_4_timestamp_updated_at": 1449753143.8631344,
            "egg_4_timestamp_changed_at": 1449705534.0782206,
            "egg_4_timestamp": 1449705545.0,
            "egg_5_timestamp_updated_at": 1449753143.8631344,
            "egg_5_timestamp_changed_at": 1449705534.0782206,
            "egg_5_timestamp": 1449705545.0,
            "egg_6_timestamp_updated_at": 1449753143.8631344,
            "egg_6_timestamp_changed_at": 1449705534.0782206,
            "egg_6_timestamp": 1449705545.0,
            "egg_7_timestamp_updated_at": 1449753143.8631344,
            "egg_7_timestamp_changed_at": 1449705534.0782206,
            "egg_7_timestamp": 1449705545.0,
            "egg_8_timestamp_updated_at": 1449753143.8631344,
            "egg_8_timestamp_changed_at": 1449705534.0782206,
            "egg_8_timestamp": 1449705545.0,
            "egg_9_timestamp_updated_at": 1449753143.8631344,
            "egg_9_timestamp_changed_at": 1449705534.0782206,
            "egg_9_timestamp": 1449705545.0,
            "egg_10_timestamp_updated_at": 1449753143.8631344,
            "egg_10_timestamp_changed_at": 1449705534.0782206,
            "egg_10_timestamp": 1449705545.0,
            "egg_11_timestamp_updated_at": 1449753143.8631344,
            "egg_11_timestamp_changed_at": 1449705534.0782206,
            "egg_11_timestamp": 1449705545.0,
            "egg_12_timestamp_updated_at": 1449753143.8631344,
            "egg_12_timestamp_changed_at": 1449705534.0782206,
            "egg_12_timestamp": 1449705545.0,
            "egg_13_timestamp_updated_at": 1449753143.8631344,
            "egg_13_timestamp_changed_at": 1449705534.0782206,
            "egg_13_timestamp": 1449705545.0,
            "egg_14_timestamp_updated_at": 1449753143.8631344,
            "egg_14_timestamp_changed_at": 1449705534.0782206,
            "egg_14_timestamp": 1449705545.0,
          },
          "eggtray_id": "153869",
          "name": "<NAME>",
          "freshness_period": 2419200,
          "locale": "en_us",
          "units": {},
          "created_at": 1417823382,
          "hidden_at": null,
          "capabilities": {},
          "triggers": [],
          "device_manufacturer": "quirky_ge",
          "model_name": "Egg Minder",
          "upc_id": "23",
          "upc_code": "814434017233",
          "lat_lng": [38.429962, -122.653715],
          "location": ""
        },
  "errors": [],
  "pagination": {
    "count": 1
  }
}

"""
    def __init__(self, device_state_as_json, objectprefix="eggtrays"):
        super(WinkEggTray, self).__init__(device_state_as_json,
                                          objectprefix=objectprefix)
    def __repr__(self):
        return "<Wink eggtray Name:%s Device_id:%s state:%s>" % (self.name(), self.device_id(), self.state())

    def state(self):
        if 'inventory' in self._last_reading:
            return self._last_reading['inventory']
        return False

    def device_id(self):
        return self.json_state.get('eggtray_id', self.name())<commit_msg>Fix style issue<commit_after>"""
Objects for interfacing with the Wink API
"""
import logging
import json
import time
import requests

BASE_URL = "https://winkapi.quirky.com"

HEADERS = {}


class WinkDevice(object):

    @staticmethod
    def factory(device_state_as_json):
        if "light_bulb_id" in device_state_as_json:
            return WinkBulb(device_state_as_json)
        elif "sensor_pod_id" in device_state_as_json:
            return WinkSensorPod(device_state_as_json)
        elif "binary_switch_id" in device_state_as_json:
            return WinkBinarySwitch(device_state_as_json)
        elif "lock_id" in device_state_as_json:
            return WinkLock(device_state_as_json)
        elif "eggtray_id" in device_state_as_json:
            return WinkEggTray(device_state_as_json)
        # elif "thermostat_id" in aJSonObj:
        # elif "remote_id" in aJSonObj:
        return WinkDevice(device_state_as_json)

    def __init__(self, device_state_as_json, objectprefix=None):
        self.objectprefix = objectprefix
        self.json_state = device_state_as_json

    def __str__(self):
        return "%s %s %s" % (self.name(), self.device_id(), self.state())

    def __repr__(self):
        return "<Wink object %s %s %s>" % (self.name(), self.device_id(), self.state())

    def name(self):
        return self.json_state.get('name', "Unknown Name")

    def state(self):
        raise NotImplementedError("Must implement state")

    def device_id(self):
        raise NotImplementedError("Must implement device_id")

    @property
    def _last_reading(self):
        return self.json_state.get('last_reading') or {}

    def _update_state_from_response(self, response_json):
        """
        :param response_json: the json obj returned from query
        :return:
        """
        self.json_state = response_json.get('data')

    def update_state(self):
        """ Update state with latest info from Wink API. """
        url_string = "{}/{}/{}".format(BASE_URL, self.objectprefix, self.device_id())
        arequest = requests.get(url_string, headers=HEADERS)
        self._update_state_from_response(arequest.json())

    def refresh_state_at_hub(self):
        """
        Tell hub to query latest status from device and upload to Wink.
        PS: Not sure if this even works..
        """
        url_string = "{}/{}/{}/refresh".format(BASE_URL, self.objectprefix, self.device_id())
        requests.get(url_string, headers=HEADERS)


class WinkEggTray(WinkDevice):
    """ represents a wink.py egg tray
    json_obj holds the json stat at init (and if there is a refresh it's updated
    it's the native format for this objects methods
    and looks like so:
{
    "data": {
        "last_reading": {
            "connection": true,
            "connection_updated_at": 1417823487.490747,
            "battery": 0.83,
            "battery_updated_at": 1417823487.490747,
            "inventory": 3,
            "inventory_updated_at": 1449705551.7313306,
            "freshness_remaining": 2419191,
            "freshness_remaining_updated_at": 1449705551.7313495,
            "age_updated_at": 1449705551.7313418,
            "age": 1449705542,
            "connection_changed_at": 1449705443.6858568,
            "next_trigger_at_updated_at": None,
            "next_trigger_at": None,
            "egg_1_timestamp_updated_at": 1449753143.8631344,
            "egg_1_timestamp_changed_at": 1449705534.0782206,
            "egg_1_timestamp": 1449705545.0,
            "egg_2_timestamp_updated_at": 1449753143.8631344,
            "egg_2_timestamp_changed_at": 1449705534.0782206,
            "egg_2_timestamp": 1449705545.0,
            "egg_3_timestamp_updated_at": 1449753143.8631344,
            "egg_3_timestamp_changed_at": 1449705534.0782206,
            "egg_3_timestamp": 1449705545.0,
            "egg_4_timestamp_updated_at": 1449753143.8631344,
            "egg_4_timestamp_changed_at": 1449705534.0782206,
            "egg_4_timestamp": 1449705545.0,
            "egg_5_timestamp_updated_at": 1449753143.8631344,
            "egg_5_timestamp_changed_at": 1449705534.0782206,
            "egg_5_timestamp": 1449705545.0,
            "egg_6_timestamp_updated_at": 1449753143.8631344,
            "egg_6_timestamp_changed_at": 1449705534.0782206,
            "egg_6_timestamp": 1449705545.0,
            "egg_7_timestamp_updated_at": 1449753143.8631344,
            "egg_7_timestamp_changed_at": 1449705534.0782206,
            "egg_7_timestamp": 1449705545.0,
            "egg_8_timestamp_updated_at": 1449753143.8631344,
            "egg_8_timestamp_changed_at": 1449705534.0782206,
            "egg_8_timestamp": 1449705545.0,
            "egg_9_timestamp_updated_at": 1449753143.8631344,
            "egg_9_timestamp_changed_at": 1449705534.0782206,
            "egg_9_timestamp": 1449705545.0,
            "egg_10_timestamp_updated_at": 1449753143.8631344,
            "egg_10_timestamp_changed_at": 1449705534.0782206,
            "egg_10_timestamp": 1449705545.0,
            "egg_11_timestamp_updated_at": 1449753143.8631344,
            "egg_11_timestamp_changed_at": 1449705534.0782206,
            "egg_11_timestamp": 1449705545.0,
            "egg_12_timestamp_updated_at": 1449753143.8631344,
            "egg_12_timestamp_changed_at": 1449705534.0782206,
            "egg_12_timestamp": 1449705545.0,
            "egg_13_timestamp_updated_at": 1449753143.8631344,
            "egg_13_timestamp_changed_at": 1449705534.0782206,
            "egg_13_timestamp": 1449705545.0,
            "egg_14_timestamp_updated_at": 1449753143.8631344,
            "egg_14_timestamp_changed_at": 1449705534.0782206,
            "egg_14_timestamp": 1449705545.0,
          },
          "eggtray_id": "153869",
          "name": "<NAME>",
          "freshness_period": 2419200,
          "locale": "en_us",
          "units": {},
          "created_at": 1417823382,
          "hidden_at": null,
          "capabilities": {},
          "triggers": [],
          "device_manufacturer": "quirky_ge",
          "model_name": "Egg Minder",
          "upc_id": "23",
          "upc_code": "814434017233",
          "lat_lng": [38.429962, -122.653715],
          "location": ""
        },
  "errors": [],
  "pagination": {
    "count": 1
  }
}

"""
    def __init__(self, device_state_as_json, objectprefix="eggtrays"):
        super(WinkEggTray, self).__init__(device_state_as_json,
                                          objectprefix=objectprefix)

    def __repr__(self):
        return "<Wink eggtray Name:%s Device_id:%s state:%s>" % (self.name(), self.device_id(), self.state())

    def state(self):
        if 'inventory' in self._last_reading:
            return self._last_reading['inventory']
        return False

    def device_id(self):
        return self.json_state.get('eggtray_id', self.name())
{'fraction_non_alphanumeric': 0.10586948596427269, 'fraction_numerical': 0.12584761210353626, 'mean_word_length': 2.649813730707823, 'pattern_counts': {'":': 150, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3782334', 'n_tokens_mistral': 5682, 'n_tokens_neox': 4474, 'n_words': 773}