<s> """<STR_LIT>""" <EOL> import logging <EOL> from homeassistant . components . binary_sensor import ( BinarySensorDevice , <EOL> ENTITY_ID_FORMAT , <EOL> SENSOR_CLASSES ) <EOL> from homeassistant . const import ATTR_FRIENDLY_NAME , CONF_VALUE_TEMPLATE <EOL> from homeassistant . core import EVENT_STATE_CHANGED <EOL> from homeassistant . exceptions import TemplateError <EOL> from homeassistant . helpers . entity import generate_entity_id <EOL> from homeassistant . helpers import template <EOL> from homeassistant . util import slugify <EOL> CONF_SENSORS = '<STR_LIT>' <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> sensors = [ ] <EOL> if config . get ( CONF_SENSORS ) is None : <EOL> _LOGGER . error ( '<STR_LIT>' ) <EOL> return False <EOL> for device , device_config in config [ CONF_SENSORS ] . items ( ) : <EOL> if device != slugify ( device ) : <EOL> _LOGGER . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , device , slugify ( device ) ) <EOL> continue <EOL> if not isinstance ( device_config , dict ) : <EOL> _LOGGER . error ( '<STR_LIT>' , <EOL> device ) <EOL> continue <EOL> friendly_name = device_config . get ( ATTR_FRIENDLY_NAME , device ) <EOL> sensor_class = device_config . get ( '<STR_LIT>' ) <EOL> value_template = device_config . get ( CONF_VALUE_TEMPLATE ) <EOL> if sensor_class not in SENSOR_CLASSES : <EOL> _LOGGER . error ( '<STR_LIT>' ) <EOL> continue <EOL> if value_template is None : <EOL> _LOGGER . error ( <EOL> '<STR_LIT>' , CONF_VALUE_TEMPLATE , device ) <EOL> continue <EOL> sensors . append ( <EOL> BinarySensorTemplate ( <EOL> hass , <EOL> device , <EOL> friendly_name , <EOL> sensor_class , <EOL> value_template ) <EOL> ) <EOL> if not sensors : <EOL> _LOGGER . error ( '<STR_LIT>' ) <EOL> return False <EOL> add_devices ( sensors ) <EOL> return True <EOL> class BinarySensorTemplate ( BinarySensorDevice ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hass , device , friendly_name , sensor_class , <EOL> value_template ) : <EOL> """<STR_LIT>""" <EOL> self . hass = hass <EOL> self . entity_id = generate_entity_id ( ENTITY_ID_FORMAT , device , <EOL> hass = hass ) <EOL> self . _name = friendly_name <EOL> self . _sensor_class = sensor_class <EOL> self . _template = value_template <EOL> self . _state = None <EOL> self . update ( ) <EOL> def template_bsensor_event_listener ( event ) : <EOL> """<STR_LIT>""" <EOL> self . update_ha_state ( True ) <EOL> hass . bus . listen ( EVENT_STATE_CHANGED , <EOL> template_bsensor_event_listener ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _name <EOL> @ property <EOL> def is_on ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state <EOL> @ property <EOL> def sensor_class ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sensor_class <EOL> @ property <EOL> def should_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _state = template . render ( self . hass , <EOL> self . _template ) . lower ( ) == '<STR_LIT:true>' <EOL> except TemplateError as ex : <EOL> if ex . args and ex . args [ <NUM_LIT:0> ] . startswith ( <EOL> "<STR_LIT>" ) : <EOL> _LOGGER . warning ( ex ) <EOL> return <EOL> _LOGGER . error ( ex ) <EOL> self . _state = False </s>
