def get_states(self): # ./set_state.py set-state group.test_view unknown --entity_id=energy_asset.plant_dgf,switch.plant_dgf --view=true --order=3 --friendly_name='Test' --hidden='true' # ./set_state.py set-state energy_asset.xxxxxx not_assigned --lat=48.3 --lon=11 --status=_uninit --schedule=0,0 --max_flex=0,0 --min_flex=0,0 self._print('Result', full=True) states = remote.get_states(api) [print(f'{state}\n') for state in states] for state in states: self._print(f'{state.entity_id}: {state.state}', full=False) pprint(state.attributes)
def get_homeassistant_sensors(self): # retrieve and sort sensors sensors = dict() for state in remote.get_states(self.api): sensortype = state.entity_id.split(".")[0] if sensortype not in sensors: sensors[sensortype] = [] sensors[sensortype].append({ "state": state.state, "id": state.entity_id }) return sensors
def structure_init(server, ha_ip, pwd): api = remote.API(ha_ip, pwd) cfg = remote.get_config(api) # e.g. make version property print(cfg) uri = "http://hassopcua.lvonwedel.net" idx = server.register_namespace(uri) # create state type for HASS types = server.get_node(ua.ObjectIds.BaseObjectType) object_type_to_derive_from = server.get_root_node().get_child( ["0:Types", "0:ObjectTypes", "0:BaseObjectType"]) ha_state_type = types.add_object_type(idx, "HAStateType") ha_state_type.add_variable(idx, "state", 1.0) # create objects objects = server.get_objects_node() loc_name = cfg['location_name'] loc_node = objects.add_folder(idx, loc_name) loc_node.add_property(idx, "location_name", loc_name) loc_node.add_property(idx, "elevation", cfg['elevation']) loc_node.add_property(idx, "latitiude", cfg['latitude']) loc_node.add_property(idx, "longitude", cfg['longitude']) loc_node.add_property(idx, "version", cfg['version']) entities = remote.get_states(api) for entity in entities: # create the node node = loc_node.add_object(idx, entity.entity_id, ha_state_type.nodeid) # set the value for the state child variable state = node.get_child('%d:state' % idx) state.set_value(entity.state) # node.set_attribute(ua.AttributeIds.DisplayName, entity.attributes['friendly_name']) for attr in entity.attributes.keys(): if attr in [ 'latitude', 'longitude', 'unit_of_measurement', 'friendly_name' ]: node.add_property(idx, attr, entity.attributes[attr]) return (idx, loc_name)
def _get_entities(self): self.connections = {} entities = remote.get_states(self.api) for entity in entities: if entity.entity_id.startswith('zwave'): node_id = str(entity.attributes['node_id']) try: neighbors = entity.attributes['neighbors'] except KeyError: neighbors = [] extra = {} try: if 'primaryController' in entity.attributes[ 'capabilities']: # Make any Z-Wave node that is a primaryController stand out. extra['fillcolor'] = 'lightgreen' extra['style'] = "rounded,filled" except KeyError: pass name = entity.attributes['friendly_name'] name += " [%s" % node_id if entity.attributes['is_zwave_plus']: name += "+" else: name += "-" name += "]\n(%s" % entity.attributes['product_name'] try: name += ": %s%%" % entity.attributes['battery_level'] except KeyError: pass name += ")" self.dot.node(node_id, name, extra) for neighbor in neighbors: if node_id not in self.connections: self.connections[node_id] = {} self.connections[node_id][str(neighbor)] = True
def cli(ctx, entry): """List various entries of an instance.""" import homeassistant.remote as remote ctx.log('Available %s', entry) if entry == 'services': services = remote.get_services(ctx.api) for service in services: ctx.log(json_output(service['services'])) if entry == 'events': events = remote.get_event_listeners(ctx.api) for event in events: ctx.log(event) if entry == 'entities': entities = remote.get_states(ctx.api) for entity in entities: ctx.log(entity) ctx.vlog('Details of %s, Created: %s', ctx.host, timestamp())
def refreshStates(self): self.api = remote.API(self.ip) self.__states = remote.get_states(self.api) return self.__states
def test_get_states(self): """ Test Python API get_state_entity_ids. """ self.assertEqual(hass.states.all(), remote.get_states(master_api)) self.assertEqual([], remote.get_states(broken_api))
#Connect to Home Assistant api = remote.API(Config.get('HomeAssistant', 'IP'), Config.get('HomeAssistant', 'PW')) #Create lists for sensor IDs temperature_ids = [] co2_ids = [] humidity_ids = [] light_ids = [] #Global Variable goodRoom = True #Get relevant sensor IDs from Home Assistant entities = remote.get_states(api) #print(entities) # XXX - entities angucken, um zu sehen, wie timer und request counter heißen for entity in entities: if str(entity).__contains__(Config.get( 'Sensors', 'Room')) and not str(entity).__contains__("group"): if str(entity).__contains__("temp"): temperature_ids.append(entity.entity_id) elif str(entity).__contains__("co2"): co2_ids.append(entity.entity_id) elif str(entity).__contains__("hum"): humidity_ids.append(entity.entity_id) elif str(entity).__contains__("light"): light_ids.append(entity.entity_id) #XXX - add motion? #Default values
def _get_entities(self): entities = remote.get_states(self.api) for entity in entities: if entity.entity_id.startswith('zwave'): self.add(entity.attributes)
def xiaomi_print_entities(self): print('\n-- Available entities:') entities = remote.get_states(self._api) for entity in entities: print(entity)
def entities_counter(api): """Count all entities.""" import homeassistant.remote as remote return len(remote.get_states(api))
def get_resource_metadata(self, kind): logger.info("Getting {} resources".format(kind)) response = [] if kind == 'hass_entity': response = remote.get_states(self.api) return response
def test_get_states(self): """ Test Python API get_state_entity_ids. """ self.assertEqual( remote.get_states(master_api), hass.states.all())
def intent_request(session, user, request): if request['intent']['name'] == "LocateIntent": hass_devices = {} user = request['intent']['slots']['User']['value'] allStates=remote.get_states(api) for state in allStates: if get_entity_type(state) == "device_tracker": hass_devices[state.attributes['friendly_name']]=state.state output_speech = user + " is at " + hass_devices[user] output_type = "PlainText" card_type = "Simple" card_title = "Location" card_content = hass_devices[user] print(output_speech) response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':True} return response elif request['intent']['name'] == "LockIntent": matched_lock = False action = request['intent']['slots']['Action']['value'] requested_lock = request['intent']['slots']['LockName']['value'] allStates = remote.get_states(api) for state in allStates: if get_entity_type(state) == "lock": friendly_name = state.attributes['friendly_name'] if friendly_name.lower() == requested_lock: matched_lock = True print(action) if action == "lock": remote.set_state(api, state.entity_id, new_state=STATE_LOCKED) output_speech = "I have locked the " + requested_lock elif action == "unlock": remote.set_state(api, state.entity_id, new_state=STATE_UNLOCKED) output_speech = "I have unlocked the " + requested_lock if matched_lock == False: output_speech = "I'm sorry, I have not found a lock by that name." output_type = "PlainText" response = {"outputSpeech" : {"type":output_type, "text":output_speech}, 'shouldEndSession':True} return response elif request['intent']['name'] == "CurrentEnergyIntent": energy_usage = remote.get_state(api, 'sensor.energy_usage') output_speech = 'Your {} is {} {}.'.format(energy_usage.attributes['friendly_name'], energy_usage.state, energy_usage.attributes['unit_of_measurement']) output_type = "PlainText" card_type = "Simple" card_title = "Energy Usage" card_content = output_speech response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':False} return response elif request['intent']['name'] == "MonthlyEnergyIntent": energy_cost = remote.get_state(api, 'sensor.energy_cost') output_speech = 'Your {} is ${}'.format(energy_cost.attributes['friendly_name'], energy_cost.state) output_type = "PlainText" card_type = "Simple" card_title = "Energy Cost" card_content = output_speech response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':False} return response elif request['intent']['name'] == "HelpIntent": output_speech = "This is the HomeAssistant app. Right now, you can only ask where someone is, or ask about your energy usage. But I have big plans. " output_type = "PlainText" card_type = "Simple" card_title = "HelloWorld - Title" card_content = "HelloWorld - This is the Hello World help! Just say Hi" response = {"outputSpeech": {"type":output_type,"text":output_speech},'shouldEndSession':False} return response else: return launch_request(session, user, request) ##Just do the same thing as launch request
def test_get_states(self): """ Test Python API get_state_entity_ids. """ self.assertEqual( remote.get_states(self.api), self.hass.states.all())
def _states(self): return remote.get_states(self.api)