<commit_before>"""
Support for exposing Concord232 elements as sensors.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.concord232/
"""
import datetime
import logging

import requests
import voluptuous as vol

from homeassistant.components.binary_sensor import (
    BinarySensorDevice, PLATFORM_SCHEMA, DEVICE_CLASSES)
from homeassistant.const import (CONF_HOST, CONF_PORT)
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['concord232==0.14']

_LOGGER = logging.getLogger(__name__)

CONF_EXCLUDE_ZONES = 'exclude_zones'
CONF_ZONE_TYPES = 'zone_types'

DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'Alarm'
DEFAULT_PORT = '5007'
DEFAULT_SSL = False

SCAN_INTERVAL = datetime.timedelta(seconds=1)

ZONE_TYPES_SCHEMA = vol.Schema({
    cv.positive_int: vol.In(DEVICE_CLASSES),
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Optional(CONF_EXCLUDE_ZONES, default=[]):
        vol.All(cv.ensure_list, [cv.positive_int]),
    vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
    vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
    vol.Optional(CONF_ZONE_TYPES, default={}): ZONE_TYPES_SCHEMA,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Concord232 binary sensor platform."""
    from concord232 import client as concord232_client

    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    exclude = config.get(CONF_EXCLUDE_ZONES)
    zone_types = config.get(CONF_ZONE_TYPES)
    sensors = []

    try:
        _LOGGER.debug("Initializing Client")
        client = concord232_client.Client('http://{}:{}'.format(host, port))
        client.zones = client.list_zones()
        client.last_zone_update = datetime.datetime.now()

    except requests.exceptions.ConnectionError as ex:
        _LOGGER.error("Unable to connect to Concord232: %s", str(ex))
        return False

    for zone in client.zones:
        _LOGGER.info("Loading Zone found: %s", zone['name'])
        if zone['number'] not in exclude:
            sensors.append(
                Concord232ZoneSensor(
                    hass, client, zone, zone_types.get(
                        zone['number'], get_opening_type(zone))
                )
            )

        add_devices(sensors, True)


def get_opening_type(zone):
    """Return the result of the type guessing from name."""
    if 'MOTION' in zone['name']:
        return 'motion'
    if 'KEY' in zone['name']:
        return 'safety'
    if 'SMOKE' in zone['name']:
        return 'smoke'
    if 'WATER' in zone['name']:
        return 'water'
    return 'opening'


class Concord232ZoneSensor(BinarySensorDevice):
    """Representation of a Concord232 zone as a sensor."""

    def __init__(self, hass, client, zone, zone_type):
        """Initialize the Concord232 binary sensor."""
        self._hass = hass
        self._client = client
        self._zone = zone
        self._number = zone['number']
        self._zone_type = zone_type

    @property
    def device_class(self):
        """Return the class of this sensor, from DEVICE_CLASSES."""
        return self._zone_type

    @property
    def should_poll(self):
        """No polling needed."""
        return True

    @property
    def name(self):
        """Return the name of the binary sensor."""
        return self._zone['name']

    @property
    def is_on(self):
        """Return true if the binary sensor is on."""
        # True means "faulted" or "open" or "abnormal state"
        return bool(self._zone['state'] == 'Normal')

    def update(self):
        """Get updated stats from API."""
        last_update = datetime.datetime.now() - self._client.last_zone_update
        _LOGGER.debug("Zone: %s ", self._zone)
        if last_update > datetime.timedelta(seconds=1):
            self._client.zones = self._client.list_zones()<commit_msg>Fix inverted sensors on the concord232 binary sensor component (#11261)<commit_after>"""
Support for exposing Concord232 elements as sensors.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.concord232/
"""
import datetime
import logging

import requests
import voluptuous as vol

from homeassistant.components.binary_sensor import (
    BinarySensorDevice, PLATFORM_SCHEMA, DEVICE_CLASSES)
from homeassistant.const import (CONF_HOST, CONF_PORT)
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['concord232==0.14']

_LOGGER = logging.getLogger(__name__)

CONF_EXCLUDE_ZONES = 'exclude_zones'
CONF_ZONE_TYPES = 'zone_types'

DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'Alarm'
DEFAULT_PORT = '5007'
DEFAULT_SSL = False

SCAN_INTERVAL = datetime.timedelta(seconds=1)

ZONE_TYPES_SCHEMA = vol.Schema({
    cv.positive_int: vol.In(DEVICE_CLASSES),
})

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Optional(CONF_EXCLUDE_ZONES, default=[]):
        vol.All(cv.ensure_list, [cv.positive_int]),
    vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
    vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
    vol.Optional(CONF_ZONE_TYPES, default={}): ZONE_TYPES_SCHEMA,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Concord232 binary sensor platform."""
    from concord232 import client as concord232_client

    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    exclude = config.get(CONF_EXCLUDE_ZONES)
    zone_types = config.get(CONF_ZONE_TYPES)
    sensors = []

    try:
        _LOGGER.debug("Initializing Client")
        client = concord232_client.Client('http://{}:{}'.format(host, port))
        client.zones = client.list_zones()
        client.last_zone_update = datetime.datetime.now()

    except requests.exceptions.ConnectionError as ex:
        _LOGGER.error("Unable to connect to Concord232: %s", str(ex))
        return False

    for zone in client.zones:
        _LOGGER.info("Loading Zone found: %s", zone['name'])
        if zone['number'] not in exclude:
            sensors.append(
                Concord232ZoneSensor(
                    hass, client, zone, zone_types.get(
                        zone['number'], get_opening_type(zone))
                )
            )

        add_devices(sensors, True)


def get_opening_type(zone):
    """Return the result of the type guessing from name."""
    if 'MOTION' in zone['name']:
        return 'motion'
    if 'KEY' in zone['name']:
        return 'safety'
    if 'SMOKE' in zone['name']:
        return 'smoke'
    if 'WATER' in zone['name']:
        return 'water'
    return 'opening'


class Concord232ZoneSensor(BinarySensorDevice):
    """Representation of a Concord232 zone as a sensor."""

    def __init__(self, hass, client, zone, zone_type):
        """Initialize the Concord232 binary sensor."""
        self._hass = hass
        self._client = client
        self._zone = zone
        self._number = zone['number']
        self._zone_type = zone_type

    @property
    def device_class(self):
        """Return the class of this sensor, from DEVICE_CLASSES."""
        return self._zone_type

    @property
    def should_poll(self):
        """No polling needed."""
        return True

    @property
    def name(self):
        """Return the name of the binary sensor."""
        return self._zone['name']

    @property
    def is_on(self):
        """Return true if the binary sensor is on."""
        # True means "faulted" or "open" or "abnormal state"
        return bool(self._zone['state'] != 'Normal')

    def update(self):
        """Get updated stats from API."""
        last_update = datetime.datetime.now() - self._client.last_zone_update
        _LOGGER.debug("Zone: %s ", self._zone)
        if last_update > datetime.timedelta(seconds=1):
            self._client.zones = self._client.list_zones()
{'fraction_non_alphanumeric': 0.11208206686930092, 'fraction_numerical': 0.012411347517730497, 'mean_word_length': 3.1345549738219893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584649', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2398, 'n_words': 604}