<s> """<STR_LIT>""" <EOL> from datetime import timedelta <EOL> import logging <EOL> import os <EOL> import voluptuous as vol <EOL> from homeassistant . config import load_yaml_config_file <EOL> from homeassistant . helpers . entity_component import EntityComponent <EOL> from homeassistant . helpers . entity import Entity <EOL> from homeassistant . helpers . config_validation import PLATFORM_SCHEMA <EOL> import homeassistant . helpers . config_validation as cv <EOL> from homeassistant . const import ( <EOL> ATTR_CODE , ATTR_CODE_FORMAT , ATTR_ENTITY_ID , STATE_LOCKED , STATE_UNLOCKED , <EOL> STATE_UNKNOWN , SERVICE_LOCK , SERVICE_UNLOCK ) <EOL> from homeassistant . components import ( group , verisure , wink ) <EOL> DOMAIN = '<STR_LIT>' <EOL> SCAN_INTERVAL = <NUM_LIT:30> <EOL> GROUP_NAME_ALL_LOCKS = '<STR_LIT>' <EOL> ENTITY_ID_ALL_LOCKS = group . ENTITY_ID_FORMAT . format ( '<STR_LIT>' ) <EOL> ENTITY_ID_FORMAT = DOMAIN + '<STR_LIT>' <EOL> MIN_TIME_BETWEEN_SCANS = timedelta ( seconds = <NUM_LIT:10> ) <EOL> DISCOVERY_PLATFORMS = { <EOL> wink . DISCOVER_LOCKS : '<STR_LIT>' , <EOL> verisure . DISCOVER_LOCKS : '<STR_LIT>' <EOL> } <EOL> LOCK_SERVICE_SCHEMA = vol . Schema ( { <EOL> vol . Optional ( ATTR_ENTITY_ID ) : cv . entity_ids , <EOL> vol . Optional ( ATTR_CODE ) : cv . string , <EOL> } ) <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> def is_locked ( hass , entity_id = None ) : <EOL> """<STR_LIT>""" <EOL> entity_id = entity_id or ENTITY_ID_ALL_LOCKS <EOL> return hass . states . is_state ( entity_id , STATE_LOCKED ) <EOL> def lock ( hass , entity_id = None , code = None ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> if code : <EOL> data [ ATTR_CODE ] = code <EOL> if entity_id : <EOL> data [ ATTR_ENTITY_ID ] = entity_id <EOL> hass . services . call ( DOMAIN , SERVICE_LOCK , data ) <EOL> def unlock ( hass , entity_id = None , code = None ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> if code : <EOL> data [ ATTR_CODE ] = code <EOL> if entity_id : <EOL> data [ ATTR_ENTITY_ID ] = entity_id <EOL> hass . services . call ( DOMAIN , SERVICE_UNLOCK , data ) <EOL> def setup ( hass , config ) : <EOL> """<STR_LIT>""" <EOL> component = EntityComponent ( <EOL> _LOGGER , DOMAIN , hass , SCAN_INTERVAL , DISCOVERY_PLATFORMS , <EOL> GROUP_NAME_ALL_LOCKS ) <EOL> component . setup ( config ) <EOL> def handle_lock_service ( service ) : <EOL> """<STR_LIT>""" <EOL> target_locks = component . extract_from_service ( service ) <EOL> code = service . data . get ( ATTR_CODE ) <EOL> for item in target_locks : <EOL> if service . service == SERVICE_LOCK : <EOL> item . lock ( code = code ) <EOL> else : <EOL> item . unlock ( code = code ) <EOL> if item . should_poll : <EOL> item . update_ha_state ( True ) <EOL> descriptions = load_yaml_config_file ( <EOL> os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> hass . services . register ( DOMAIN , SERVICE_UNLOCK , handle_lock_service , <EOL> descriptions . get ( SERVICE_UNLOCK ) , <EOL> schema = LOCK_SERVICE_SCHEMA ) <EOL> hass . services . register ( DOMAIN , SERVICE_LOCK , handle_lock_service , <EOL> descriptions . get ( SERVICE_LOCK ) , <EOL> schema = LOCK_SERVICE_SCHEMA ) <EOL> return True <EOL> class LockDevice ( Entity ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def code_format ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def is_locked ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def lock ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def unlock ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ property <EOL> def state_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . code_format is None : <EOL> return None <EOL> state_attr = { <EOL> ATTR_CODE_FORMAT : self . code_format , <EOL> } <EOL> return state_attr <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> locked = self . is_locked <EOL> if locked is None : <EOL> return STATE_UNKNOWN <EOL> return STATE_LOCKED if locked else STATE_UNLOCKED </s>
