<s> """<STR_LIT>""" <EOL> from datetime import timedelta <EOL> import logging <EOL> import sys <EOL> from homeassistant . components import ( <EOL> zone as zone_cmp , sun as sun_cmp ) <EOL> from homeassistant . const import ( <EOL> ATTR_GPS_ACCURACY , ATTR_LATITUDE , ATTR_LONGITUDE , <EOL> CONF_ENTITY_ID , CONF_VALUE_TEMPLATE , CONF_CONDITION , <EOL> WEEKDAYS ) <EOL> from homeassistant . exceptions import TemplateError , HomeAssistantError <EOL> import homeassistant . helpers . config_validation as cv <EOL> from homeassistant . helpers . template import render <EOL> import homeassistant . util . dt as dt_util <EOL> FROM_CONFIG_FORMAT = '<STR_LIT>' <EOL> CONF_BELOW = '<STR_LIT>' <EOL> CONF_ABOVE = '<STR_LIT>' <EOL> CONF_STATE = '<STR_LIT:state>' <EOL> CONF_ZONE = '<STR_LIT>' <EOL> EVENT_SUNRISE = '<STR_LIT>' <EOL> EVENT_SUNSET = '<STR_LIT>' <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> def from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> factory = getattr ( <EOL> sys . modules [ __name__ ] , <EOL> FROM_CONFIG_FORMAT . format ( config . get ( CONF_CONDITION ) ) , None ) <EOL> if factory is None : <EOL> raise HomeAssistantError ( '<STR_LIT>' . format ( <EOL> config . get ( CONF_CONDITION ) , config ) ) <EOL> return factory ( config , config_validation ) <EOL> def and_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . AND_CONDITION_SCHEMA ( config ) <EOL> checks = [ from_config ( entry ) for entry in config [ '<STR_LIT>' ] ] <EOL> def if_and_condition ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return all ( check ( hass , variables ) for check in checks ) <EOL> return if_and_condition <EOL> def or_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . OR_CONDITION_SCHEMA ( config ) <EOL> checks = [ from_config ( entry ) for entry in config [ '<STR_LIT>' ] ] <EOL> def if_or_condition ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return any ( check ( hass , variables ) for check in checks ) <EOL> return if_or_condition <EOL> def numeric_state ( hass , entity , below = None , above = None , value_template = None , <EOL> variables = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( entity , str ) : <EOL> entity = hass . states . get ( entity ) <EOL> if entity is None : <EOL> return False <EOL> if value_template is None : <EOL> value = entity . state <EOL> else : <EOL> variables = dict ( variables or { } ) <EOL> variables [ '<STR_LIT:state>' ] = entity <EOL> try : <EOL> value = render ( hass , value_template , variables ) <EOL> except TemplateError as ex : <EOL> _LOGGER . error ( ex ) <EOL> return False <EOL> try : <EOL> value = float ( value ) <EOL> except ValueError : <EOL> _LOGGER . warning ( "<STR_LIT>" , value ) <EOL> return False <EOL> print ( below , value , above ) <EOL> if below is not None and value > below : <EOL> return False <EOL> if above is not None and value < above : <EOL> return False <EOL> return True <EOL> def numeric_state_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . NUMERIC_STATE_CONDITION_SCHEMA ( config ) <EOL> entity_id = config . get ( CONF_ENTITY_ID ) <EOL> below = config . get ( CONF_BELOW ) <EOL> above = config . get ( CONF_ABOVE ) <EOL> value_template = config . get ( CONF_VALUE_TEMPLATE ) <EOL> def if_numeric_state ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return numeric_state ( hass , entity_id , below , above , value_template , <EOL> variables ) <EOL> return if_numeric_state <EOL> def state ( hass , entity , req_state , for_period = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( entity , str ) : <EOL> entity = hass . states . get ( entity ) <EOL> if entity is None : <EOL> return False <EOL> is_state = entity . state == req_state <EOL> if for_period is None or not is_state : <EOL> return is_state <EOL> return dt_util . utcnow ( ) - for_period > entity . last_changed <EOL> def state_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . STATE_CONDITION_SCHEMA ( config ) <EOL> entity_id = config . get ( CONF_ENTITY_ID ) <EOL> req_state = config . get ( CONF_STATE ) <EOL> for_period = config . get ( '<STR_LIT>' ) <EOL> def if_state ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return state ( hass , entity_id , req_state , for_period ) <EOL> return if_state <EOL> def sun ( hass , before = None , after = None , before_offset = None , after_offset = None ) : <EOL> """<STR_LIT>""" <EOL> now = dt_util . now ( ) . time ( ) <EOL> before_offset = before_offset or timedelta ( <NUM_LIT:0> ) <EOL> after_offset = after_offset or timedelta ( <NUM_LIT:0> ) <EOL> if before == EVENT_SUNRISE and now > ( sun_cmp . next_rising ( hass ) + <EOL> before_offset ) . time ( ) : <EOL> return False <EOL> elif before == EVENT_SUNSET and now > ( sun_cmp . next_setting ( hass ) + <EOL> before_offset ) . time ( ) : <EOL> return False <EOL> if after == EVENT_SUNRISE and now < ( sun_cmp . next_rising ( hass ) + <EOL> after_offset ) . time ( ) : <EOL> return False <EOL> elif after == EVENT_SUNSET and now < ( sun_cmp . next_setting ( hass ) + <EOL> after_offset ) . time ( ) : <EOL> return False <EOL> return True <EOL> def sun_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . SUN_CONDITION_SCHEMA ( config ) <EOL> before = config . get ( '<STR_LIT>' ) <EOL> after = config . get ( '<STR_LIT>' ) <EOL> before_offset = config . get ( '<STR_LIT>' ) <EOL> after_offset = config . get ( '<STR_LIT>' ) <EOL> def time_if ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return sun ( hass , before , after , before_offset , after_offset ) <EOL> return time_if <EOL> def template ( hass , value_template , variables = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = render ( hass , value_template , variables ) <EOL> except TemplateError as ex : <EOL> _LOGGER . error ( '<STR_LIT>' , ex ) <EOL> return False <EOL> return value . lower ( ) == '<STR_LIT:true>' <EOL> def template_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . TEMPLATE_CONDITION_SCHEMA ( config ) <EOL> value_template = config . get ( CONF_VALUE_TEMPLATE ) <EOL> def template_if ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return template ( hass , value_template , variables ) <EOL> return template_if <EOL> def time ( before = None , after = None , weekday = None ) : <EOL> """<STR_LIT>""" <EOL> now = dt_util . now ( ) <EOL> now_time = now . time ( ) <EOL> if before is not None and now_time > before : <EOL> return False <EOL> if after is not None and now_time < after : <EOL> return False <EOL> if weekday is not None : <EOL> now_weekday = WEEKDAYS [ now . weekday ( ) ] <EOL> if isinstance ( weekday , str ) and weekday != now_weekday or now_weekday not in weekday : <EOL> return False <EOL> return True <EOL> def time_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . TIME_CONDITION_SCHEMA ( config ) <EOL> before = config . get ( '<STR_LIT>' ) <EOL> after = config . get ( '<STR_LIT>' ) <EOL> weekday = config . get ( '<STR_LIT>' ) <EOL> def time_if ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return time ( before , after , weekday ) <EOL> return time_if <EOL> def zone ( hass , zone_ent , entity ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( zone_ent , str ) : <EOL> zone_ent = hass . states . get ( zone_ent ) <EOL> if zone_ent is None : <EOL> return False <EOL> if isinstance ( entity , str ) : <EOL> entity = hass . states . get ( entity ) <EOL> if entity is None : <EOL> return False <EOL> latitude = entity . attributes . get ( ATTR_LATITUDE ) <EOL> longitude = entity . attributes . get ( ATTR_LONGITUDE ) <EOL> if latitude is None or longitude is None : <EOL> return False <EOL> return zone_cmp . in_zone ( zone_ent , latitude , longitude , <EOL> entity . attributes . get ( ATTR_GPS_ACCURACY , <NUM_LIT:0> ) ) <EOL> def zone_from_config ( config , config_validation = True ) : <EOL> """<STR_LIT>""" <EOL> if config_validation : <EOL> config = cv . ZONE_CONDITION_SCHEMA ( config ) <EOL> entity_id = config . get ( CONF_ENTITY_ID ) <EOL> zone_entity_id = config . get ( '<STR_LIT>' ) <EOL> def if_in_zone ( hass , variables = None ) : <EOL> """<STR_LIT>""" <EOL> return zone ( hass , zone_entity_id , entity_id ) <EOL> return if_in_zone </s>
