<s> """<STR_LIT>""" <EOL> import logging <EOL> from homeassistant . components . switch import SwitchDevice <EOL> from homeassistant . const import ATTR_BATTERY_LEVEL , STATE_OFF , STATE_ON <EOL> from homeassistant . loader import get_component <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> DEPENDENCIES = [ ] <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> if discovery_info is None : <EOL> return <EOL> mysensors = get_component ( '<STR_LIT>' ) <EOL> for gateway in mysensors . GATEWAYS . values ( ) : <EOL> pres = gateway . const . Presentation <EOL> set_req = gateway . const . SetReq <EOL> map_sv_types = { <EOL> pres . S_DOOR : [ set_req . V_ARMED ] , <EOL> pres . S_MOTION : [ set_req . V_ARMED ] , <EOL> pres . S_SMOKE : [ set_req . V_ARMED ] , <EOL> pres . S_LIGHT : [ set_req . V_LIGHT ] , <EOL> pres . S_LOCK : [ set_req . V_LOCK_STATUS ] , <EOL> } <EOL> if float ( gateway . version ) >= <NUM_LIT> : <EOL> map_sv_types . update ( { <EOL> pres . S_BINARY : [ set_req . V_STATUS , set_req . V_LIGHT ] , <EOL> pres . S_SPRINKLER : [ set_req . V_STATUS ] , <EOL> pres . S_WATER_LEAK : [ set_req . V_ARMED ] , <EOL> pres . S_SOUND : [ set_req . V_ARMED ] , <EOL> pres . S_VIBRATION : [ set_req . V_ARMED ] , <EOL> pres . S_MOISTURE : [ set_req . V_ARMED ] , <EOL> } ) <EOL> map_sv_types [ pres . S_LIGHT ] . append ( set_req . V_STATUS ) <EOL> devices = { } <EOL> gateway . platform_callbacks . append ( mysensors . pf_callback_factory ( <EOL> map_sv_types , devices , add_devices , MySensorsSwitch ) ) <EOL> class MySensorsSwitch ( SwitchDevice ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , gateway , node_id , child_id , name , value_type , child_type ) : <EOL> """<STR_LIT>""" <EOL> self . gateway = gateway <EOL> self . node_id = node_id <EOL> self . child_id = child_id <EOL> self . _name = name <EOL> self . value_type = value_type <EOL> self . battery_level = <NUM_LIT:0> <EOL> self . _values = { } <EOL> self . mysensors = get_component ( '<STR_LIT>' ) <EOL> @ property <EOL> def should_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _name <EOL> @ property <EOL> def device_state_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> address = getattr ( self . gateway , '<STR_LIT>' , None ) <EOL> if address : <EOL> device = '<STR_LIT>' . format ( address [ <NUM_LIT:0> ] , address [ <NUM_LIT:1> ] ) <EOL> else : <EOL> device = self . gateway . port <EOL> attr = { <EOL> self . mysensors . ATTR_DEVICE : device , <EOL> self . mysensors . ATTR_NODE_ID : self . node_id , <EOL> self . mysensors . ATTR_CHILD_ID : self . child_id , <EOL> ATTR_BATTERY_LEVEL : self . battery_level , <EOL> } <EOL> set_req = self . gateway . const . SetReq <EOL> for value_type , value in self . _values . items ( ) : <EOL> if value_type != self . value_type : <EOL> try : <EOL> attr [ set_req ( value_type ) . name ] = value <EOL> except ValueError : <EOL> _LOGGER . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , value_type , <EOL> self . gateway . version ) <EOL> return attr <EOL> @ property <EOL> def is_on ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . value_type in self . _values : <EOL> return self . _values [ self . value_type ] == STATE_ON <EOL> return False <EOL> def turn_on ( self ) : <EOL> """<STR_LIT>""" <EOL> self . gateway . set_child_value ( <EOL> self . node_id , self . child_id , self . value_type , <NUM_LIT:1> ) <EOL> if self . gateway . optimistic : <EOL> self . _values [ self . value_type ] = STATE_ON <EOL> self . update_ha_state ( ) <EOL> def turn_off ( self ) : <EOL> """<STR_LIT>""" <EOL> self . gateway . set_child_value ( <EOL> self . node_id , self . child_id , self . value_type , <NUM_LIT:0> ) <EOL> if self . gateway . optimistic : <EOL> self . _values [ self . value_type ] = STATE_OFF <EOL> self . update_ha_state ( ) <EOL> @ property <EOL> def available ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . value_type in self . _values <EOL> @ property <EOL> def assumed_state ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . gateway . optimistic <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> node = self . gateway . sensors [ self . node_id ] <EOL> child = node . children [ self . child_id ] <EOL> for value_type , value in child . values . items ( ) : <EOL> _LOGGER . debug ( <EOL> "<STR_LIT>" , self . _name , value_type , value ) <EOL> if value_type == self . gateway . const . SetReq . V_ARMED or value_type == self . gateway . const . SetReq . V_LIGHT or value_type == self . gateway . const . SetReq . V_LOCK_STATUS : <EOL> self . _values [ value_type ] = ( <EOL> STATE_ON if int ( value ) == <NUM_LIT:1> else STATE_OFF ) <EOL> else : <EOL> self . _values [ value_type ] = value <EOL> self . battery_level = node . battery_level </s>
