Skip to content

Instantly share code, notes, and snippets.

@cbpowell
Last active March 18, 2022 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbpowell/cb9cf9c34f68530374eb2cfa777b597f to your computer and use it in GitHub Desktop.
Save cbpowell/cb9cf9c34f68530374eb2cfa777b597f to your computer and use it in GitHub Desktop.
sources:
- mutable:
plugs:
- TesSense:
alias: "Tesla Charger"
mac: 50:c7:bf:f6:4f:39 # matches value in TesSenseLink.py line 245
power: 0 # will be updated with live values
@cbpowell
Copy link
Author

cbpowell commented Mar 12, 2022

Comments I added to supply some extra info are prefixed by #/c, just to make them identifiable.

See this revision (between Rev 2 and Rev 3) for the major changes. The Rev 1 to Rev 2 change is just to indent the majority of the code once, so that adding the async function wrapper doesn't just make it one huge singular change block. Rev 1 is the original TesSense.py code.

@israndy
Copy link

israndy commented Mar 14, 2022

Thanks, I downloaded this and since this code is calling other code I went to the full SenseLink folder and grabbed all the other files there. I had to change the TesSenseLink and config.yml files to have my already used MAC address. I ran the script and fixed a bunch of errors in modules having a period in front of their name (perhaps they should be in another folder?

This seemed to get the app running but I get an error trying to assign the current watts to the plug:

  File "/Users/israndy/Documents/Xcode/Python3/SenseApps/SenseLink/tessenselink.py", line 145, in run_tes_sense
    mutable_plug.power = charge_power
AttributeError: can't set attribute

@cbpowell
Copy link
Author

cbpowell commented Mar 14, 2022

@israndy Sorry that was a mistake on my part, that I didn't catch due to what I'd simplified for testing. Check the newest revision, essentially the call needs to be mutable_plug.data_source.power = …

@israndy
Copy link

israndy commented Mar 14, 2022

Thx, trying it now

@israndy
Copy link

israndy commented Mar 16, 2022

OK, so I love the ability to send this info to Sense, all is working great, but I had to download your entire SenseLink directory to get it working. I have now gotten SenseLink from PyPi installed and want to use that copy so I don't need all the supporting files around the TesSenseLink.py file but I am getting an error on the line "from SenseLink import *" that there is no module named SenseLink.

requests-oauthlib  1.3.0
sense-energy       0.10.2
SenseLink          2.0.1
setuptools         49.2.1

I thought I got it working with the sense_energy module, but it failed at the create instance call and I looked at the code and it looks like that's not in the sense_energy's SenseLink code.

@cbpowell
Copy link
Author

Give the code in the pull request a shot - I got it all working there, and having it on a branch from your TesSense code should help merge in any changes you've made since that published version. Specifically, here's the full working file: https://github.com/israndy/TesSense/blob/3d636a8d7b9d7e9c90c247d61a33616f2f8d4152/TesSense.py

The import statement when using the PyPI-installed package should be: from senselink import SenseLink. I get your annoyance with the Python import statements though, I can't ever get them right.

The sense_energy module does not use the same code. He forked that from my original SenseLink a while back, and it's more aimed at working hand in hand with HomeAssistant than as a standalone (in my impression).

@israndy
Copy link

israndy commented Mar 16, 2022 via email

@cbpowell
Copy link
Author

Good stuff!

Yeah I can see how the broad config option is less useful in your case. While I might eventually change this up to make it a little more streamlined, you can replace the config file by doing something the following - using my module_usage_exampe.py code as an example below, but it's highly similar to your latest TesSense code:

Original:

# pip install
from senselink import SenseLink
...
async def main():
    # Get config
    config = open('config.yml', 'r')
    # Create controller, with config
    controller = SenseLink(config)
    # Create instances
    controller.create_instances()

    # Get Mutable controller object, and create task to update it
    mutable_plug = controller.plug_for_mac("50:c7:bf:f6:4f:39")

Change to:

from senselink import SenseLink
from senselink.plug_instance import PlugInstance
from senselink.data_source import MutableSource
...
async def main():
    # Create controller, with NO config
    controller =  SenseLink(None)
    
    # Create a PlugInstance, setting at least the identifier and MAC
    mutable_plug = PlugInstance("mutable",mac="50:c7:bf:f6:4f:39")
    # Create and assign a Mutable Data Source to that plug
    mutable_data_source = MutableSource("mutable", None)
    mutable_plug.data_source = mutable_data_source
    
    # Add that plug to the controller
    controller.add_instances({mutable_plug.identifier: mutable_plug})

    # Note: you can then use the mutable_plug variable directly instead of needing a controller.plug_for_mac() call.
    # They are the same object.

Just a heads up though, I see some areas for improvement around this, so it could change in future releases of SenseLink!

@israndy
Copy link

israndy commented Mar 17, 2022 via email

@cbpowell
Copy link
Author

Yeah sure thing! It's definitely helped me make some improvements to SenseLink.

As for it waking up the car, there is a way to query the Tesla API in such a way that it doesn't wake up the car, but I think it might have to be via the streaming API. Unfortunately it seems like the Teslapy docs on streaming are a little light/nonexistent. I gather that if you use the streaming API it doesn't wake the car up, but you can tell if it is awake you can then query it like normal. I have telsamate set up for myself, which I can verify doesn't wake the car up.

@israndy
Copy link

israndy commented Mar 18, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment