<s> """<STR_LIT>""" <EOL> import re <EOL> from collections import defaultdict <EOL> from homeassistant . const import ( <EOL> ATTR_ASSUMED_STATE , ATTR_FRIENDLY_NAME , ATTR_HIDDEN , ATTR_ICON , <EOL> ATTR_UNIT_OF_MEASUREMENT , DEVICE_DEFAULT_NAME , STATE_OFF , STATE_ON , <EOL> STATE_UNAVAILABLE , STATE_UNKNOWN , TEMP_CELSIUS , TEMP_FAHRENHEIT , <EOL> ATTR_ENTITY_PICTURE ) <EOL> from homeassistant . exceptions import NoEntitySpecifiedError <EOL> from homeassistant . util import ensure_unique_string , slugify <EOL> _OVERWRITE = defaultdict ( dict ) <EOL> ENTITY_ID_PATTERN = re . compile ( r"<STR_LIT>" ) <EOL> def generate_entity_id ( entity_id_format , name , current_ids = None , hass = None ) : <EOL> """<STR_LIT>""" <EOL> name = ( name or DEVICE_DEFAULT_NAME ) . lower ( ) <EOL> if current_ids is None : <EOL> if hass is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> current_ids = hass . states . entity_ids ( ) <EOL> return ensure_unique_string ( <EOL> entity_id_format . format ( slugify ( name ) ) , current_ids ) <EOL> def split_entity_id ( entity_id ) : <EOL> """<STR_LIT>""" <EOL> return entity_id . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> def valid_entity_id ( entity_id ) : <EOL> """<STR_LIT>""" <EOL> return ENTITY_ID_PATTERN . match ( entity_id ) is not None <EOL> class Entity ( object ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def should_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> @ property <EOL> def unique_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( self . __class__ , id ( self ) ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> return STATE_UNKNOWN <EOL> @ property <EOL> def state_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def device_state_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def unit_of_measurement ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def icon ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def entity_picture ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ property <EOL> def hidden ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ property <EOL> def available ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> @ property <EOL> def assumed_state ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> entity_id = None <EOL> hass = None <EOL> def update_ha_state ( self , force_refresh = False ) : <EOL> """<STR_LIT>""" <EOL> if self . hass is None : <EOL> raise RuntimeError ( "<STR_LIT>" . format ( self ) ) <EOL> if self . entity_id is None : <EOL> raise NoEntitySpecifiedError ( <EOL> "<STR_LIT>" . format ( self . name ) ) <EOL> if force_refresh : <EOL> self . update ( ) <EOL> state = STATE_UNKNOWN if self . state is None else str ( self . state ) <EOL> attr = self . state_attributes or { } <EOL> device_attr = self . device_state_attributes <EOL> if device_attr is not None : <EOL> attr . update ( device_attr ) <EOL> self . _attr_setter ( '<STR_LIT>' , str , ATTR_UNIT_OF_MEASUREMENT , <EOL> attr ) <EOL> if not self . available : <EOL> state = STATE_UNAVAILABLE <EOL> attr = { } <EOL> self . _attr_setter ( '<STR_LIT:name>' , str , ATTR_FRIENDLY_NAME , attr ) <EOL> self . _attr_setter ( '<STR_LIT>' , str , ATTR_ICON , attr ) <EOL> self . _attr_setter ( '<STR_LIT>' , str , ATTR_ENTITY_PICTURE , attr ) <EOL> self . _attr_setter ( '<STR_LIT>' , bool , ATTR_HIDDEN , attr ) <EOL> self . _attr_setter ( '<STR_LIT>' , bool , ATTR_ASSUMED_STATE , attr ) <EOL> attr . update ( _OVERWRITE . get ( self . entity_id , { } ) ) <EOL> if not attr . get ( ATTR_HIDDEN , True ) : <EOL> attr . pop ( ATTR_HIDDEN ) <EOL> if attr . get ( ATTR_UNIT_OF_MEASUREMENT ) in ( TEMP_CELSIUS , <EOL> TEMP_FAHRENHEIT ) : <EOL> state , attr [ ATTR_UNIT_OF_MEASUREMENT ] = self . hass . config . temperature ( <EOL> state , attr [ ATTR_UNIT_OF_MEASUREMENT ] ) <EOL> state = str ( state ) <EOL> return self . hass . states . set ( self . entity_id , state , attr ) <EOL> def _attr_setter ( self , name , typ , attr , attrs ) : <EOL> """<STR_LIT>""" <EOL> if attr in attrs : <EOL> return <EOL> value = getattr ( self , name ) <EOL> if not value : <EOL> return <EOL> try : <EOL> attrs [ attr ] = typ ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( other , Entity ) and <EOL> other . unique_id == self . unique_id ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( self . name , self . state ) <EOL> @ staticmethod <EOL> def overwrite_attribute ( entity_id , attrs , vals ) : <EOL> """<STR_LIT>""" <EOL> for attr , val in zip ( attrs , vals ) : <EOL> if val is None : <EOL> _OVERWRITE [ entity_id . lower ( ) ] . pop ( attr , None ) <EOL> else : <EOL> _OVERWRITE [ entity_id . lower ( ) ] [ attr ] = val <EOL> class ToggleEntity ( Entity ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> return STATE_ON if self . is_on else STATE_OFF <EOL> @ property <EOL> def is_on ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def turn_on ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def turn_off ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def toggle ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . is_on : <EOL> self . turn_off ( ** kwargs ) <EOL> else : <EOL> self . turn_on ( ** kwargs ) </s>
