<commit_before>"""
homeassistant.components.sensor.nest
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Nest Thermostat Sensors.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.nest/
"""
import logging
import socket
import homeassistant.components.nest as nest

from homeassistant.helpers.entity import Entity
from homeassistant.const import TEMP_CELCIUS

DEPENDENCIES = ['nest']
SENSOR_TYPES = ['humidity',
                'mode',
                'last_ip',
                'local_ip',
                'last_connection',
                'battery_level']

SENSOR_UNITS = {'humidity': '%', 'battery_level': 'V'}

SENSOR_TEMP_TYPES = ['temperature',
                     'target',
                     'away_temperature[0]',
                     'away_temperature[1]']


def setup_platform(hass, config, add_devices, discovery_info=None):
    """ Setup Nest Sensor. """

    logger = logging.getLogger(__name__)
    try:
        for structure in nest.NEST.structures:
            for device in structure.devices:
                for variable in config['monitored_conditions']:
                    if variable in SENSOR_TYPES:
                        add_devices([NestBasicSensor(structure,
                                                     device,
                                                     variable)])
                    elif variable in SENSOR_TEMP_TYPES:
                        add_devices([NestTempSensor(structure,
                                                    device,
                                                    variable)])
                    else:
                        logger.error('Nest sensor type: "%s" does not exist',
                                     variable)
    except socket.error:
        logger.error(
            "Connection error logging into the nest web service."
        )


class NestSensor(Entity):
    """ Represents a Nest sensor. """

    def __init__(self, structure, device, variable):
        self.structure = structure
        self.device = device
        self.variable = variable

    @property
    def name(self):
        """ Returns the name of the nest, if any. """

        location = self.device.where
        name = self.device.name
        if location is None:
            return "{} {}".format(name, self.variable)
        else:
            if name == '':
                return "{} {}".format(location.capitalize(), self.variable)
            else:
                return "{}({}){}".format(location.capitalize(),
                                         name,
                                         self.variable)


class NestBasicSensor(NestSensor):
    """ Represents a basic Nest sensor with state. """

    @property
    def state(self):
        """ Returns the state of the sensor. """
        return getattr(self.device, self.variable)

    @property
    def unit_of_measurement(self):
        """ Unit the value is expressed in. """
        return SENSOR_UNITS.get(self.variable, None)


class NestTempSensor(NestSensor):
    """ Represents a Nest Temperature sensor. """

    @property
    def unit_of_measurement(self):
        """ Unit the value is expressed in. """
        return TEMP_CELCIUS

    @property
    def state(self):
        """ Returns the state of the sensor. """
        temp = getattr(self.device, self.variable)
        if temp is None:
            return None

        return round(temp, 1)<commit_msg>Added support for nest current weather conditions<commit_after>"""
homeassistant.components.sensor.nest
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Nest Thermostat Sensors.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.nest/
"""
import logging
import socket
import homeassistant.components.nest as nest

from homeassistant.helpers.entity import Entity
from homeassistant.const import TEMP_CELCIUS

DEPENDENCIES = ['nest']
SENSOR_TYPES = ['humidity',
                'mode',
                'last_ip',
                'local_ip',
                'last_connection',
                'battery_level']

WEATHER_VARIABLES = ['weather_condition', 'weather_temperature',
                     'weather_humidity',
                     'wind_speed', 'wind_direction']

JSON_VARIABLE_NAMES = {'weather_humidity': 'humidity',
                       'weather_temperature': 'temperature',
                       'weather_condition': 'condition',
                       'wind_speed': 'kph',
                       'wind_direction': 'direction'}

SENSOR_UNITS = {'humidity': '%', 'battery_level': 'V',
                'kph': 'kph', 'temperature': '°C'}

SENSOR_TEMP_TYPES = ['temperature',
                     'target',
                     'away_temperature[0]',
                     'away_temperature[1]']


def setup_platform(hass, config, add_devices, discovery_info=None):
    """ Setup Nest Sensor. """

    logger = logging.getLogger(__name__)
    try:
        for structure in nest.NEST.structures:
            for device in structure.devices:
                for variable in config['monitored_conditions']:
                    if variable in SENSOR_TYPES:
                        add_devices([NestBasicSensor(structure,
                                                     device,
                                                     variable)])
                    elif variable in SENSOR_TEMP_TYPES:
                        add_devices([NestTempSensor(structure,
                                                    device,
                                                    variable)])
                    elif variable in WEATHER_VARIABLES:
                        json_variable = JSON_VARIABLE_NAMES.get(variable, None)
                        add_devices([NestWeatherSensor(structure,
                                                       device,
                                                       json_variable)])
                    else:
                        for test in WEATHER_VARIABLES:
                            logger.error('NEST VAR: %s', test)
                        logger.error('Nest sensor type: "%s" does not exist',
                                     variable)
    except socket.error:
        logger.error(
            "Connection error logging into the nest web service."
        )


class NestSensor(Entity):
    """ Represents a Nest sensor. """

    def __init__(self, structure, device, variable):
        self.structure = structure
        self.device = device
        self.variable = variable

    @property
    def name(self):
        """ Returns the name of the nest, if any. """

        location = self.device.where
        name = self.device.name
        if location is None:
            return "{} {}".format(name, self.variable)
        else:
            if name == '':
                return "{} {}".format(location.capitalize(), self.variable)
            else:
                return "{}({}){}".format(location.capitalize(),
                                         name,
                                         self.variable)


class NestBasicSensor(NestSensor):
    """ Represents a basic Nest sensor with state. """

    @property
    def state(self):
        """ Returns the state of the sensor. """
        return getattr(self.device, self.variable)

    @property
    def unit_of_measurement(self):
        """ Unit the value is expressed in. """
        return SENSOR_UNITS.get(self.variable, None)


class NestTempSensor(NestSensor):
    """ Represents a Nest Temperature sensor. """

    @property
    def unit_of_measurement(self):
        """ Unit the value is expressed in. """
        return TEMP_CELCIUS

    @property
    def state(self):
        """ Returns the state of the sensor. """
        temp = getattr(self.device, self.variable)
        if temp is None:
            return None

        return round(temp, 1)


class NestWeatherSensor(NestSensor):
    """ Represents a basic Nest Weather Conditions sensor. """

    @property
    def state(self):
        """ Returns the state of the sensor. """
        if self.variable == 'kph' or self.variable == 'direction':
            return getattr(self.structure.weather.current.wind, self.variable)
        else:
            return getattr(self.structure.weather.current, self.variable)

    @property
    def unit_of_measurement(self):
        """ Unit the value is expressed in. """
        return SENSOR_UNITS.get(self.variable, None)
{'fraction_non_alphanumeric': 0.10300681230913789, 'fraction_numerical': 0.0007047216349541931, 'mean_word_length': 1.6543017456359101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6946092', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2051, 'n_words': 553}