Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get example to execute #10

Open
ghost opened this issue May 23, 2023 · 3 comments
Open

Can't get example to execute #10

ghost opened this issue May 23, 2023 · 3 comments

Comments

@ghost
Copy link

ghost commented May 23, 2023

When I put "import hass from 'homeassistant-ws';" into my NodeJS app i get

"SyntaxError: Cannot use import statement outside a module"

normally I experience something like with the home-assistant library, It looks like this:

   HomeAssistant = require('homeassistant'),
    hass = new HomeAssistant({
        host: 'http://127.0.0.1',       // Home Assistant IP address
        port: 8123,                     // Home Assistant port number
        token: 'my-token',
        ignoreCert: true                // above is Home Assistant long lived token 
    });

But your library have to use Import? why its like that? is there a way to just use require like in my example?

Anyway, i dont normally use "package.json" file for my project but I made this file and put this inside:

{
    "type": "module"
}

Ok so now seems to be going a step further, but now getting errors like :

  import hass from 'hass';
  ^^^^^^^^^^^
SyntaxError: Identifier 'hass' has already been declared

if i remove this line (import hass from 'hass';) and keep only the line (import hass from 'homeassistant-ws';) , then i get:

hass({
^
TypeError: hass is not a function

I used many libraries already including the NPM home-assistant library with no issues but I have no idea how to make this work. I love this minimalist approach and using websocket for HA.

Can you please provide a working example? And can you please share how to import the library using the "Const Require" way? So i dont have to use package.json file? I'd really appreciate it and I love to share the project I'm going to use this library for.

@ghost
Copy link
Author

ghost commented May 23, 2023

This is my example, is this incomplete? am i supposed to call function main()? is it supposed to be import hass from 'homeassistant-ws'; or import hass from 'hass'; or both? I dont get it.

if i dont call main() the app just ends immediately, but if i call main() i get error: TypeError: hass is not a function

import hass from 'homeassistant-ws';
process.setMaxListeners(0);
main();

async function main() {
  // Establishes a connection, and authenticates if necessary:
  const client = await hass({
     token: 'my-token', 
     host: '10.20.0.5',
     port: 8123,
    });

  // Get a list of all available states, panels or services:
  await client.getStates();
  await client.getServices();
  await client.getPanels();

  // Get hass configuration:
  await client.getConfig();

  // Get a Buffer containing the current thumbnail for the given media player
  await client.getMediaPlayerThumbnail('media_player.my_player');
  // { content_type: 'image/jpeg', content: Buffer<...>}

  // Get a Buffer containing a thumbnail for the given camera
  await client.getCameraThumbnail('camera.front_yard');
  // { content_type: 'image/jpeg', content: Buffer<...>}

  // Call a service, by its domain and name. The third argument is optional.
  await client.callService('lights', 'turn_on', {
    entity_id: 'light.my_light',
  });

  // Listen for all HASS events - the 'message' event is a homeassistant-ws event triggered for
  // all messages received through the websocket connection with HASS:
  //
  // See https://developers.home-assistant.io/docs/api/websocket/ for details on HASS events:
  client.on('message', (rawMessageData) => {
    console.log(rawMessageData);
  });

  // Listen only for state changes:
  client.on('state_changed', (stateChangedEvent) => {
    console.log(stateChangedEvent.data.new_state.state);
  });
}

@ghost ghost changed the title Can get example to execute Can't get example to execute May 23, 2023
@coricidin12
Copy link

did you ever get this to work? I'm having the same issue.

@ghost
Copy link
Author

ghost commented Oct 25, 2023

did you ever get this to work? I'm having the same issue.

No, i think this is not a functioning api. Im using twocolors esphome-native-api and its working great.

https://github.com/twocolors/esphome-native-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant