New service calls and sensors for shopping list component


#1

The built-in shopping list is one of the most useful feature for me and yet I rarely use it because it only works via conversation component at the moment and my primary mode of interaction with HA is via Telegram, Alexa and Zanzito.

I wish I can access to my shopping list via other components which allows me to add, remove and retrieve items from my shopping list.

In order to do that, I hope the shopping list component will introduce new service calls and sensor. For example, to add new item, I can use this service…

action:
    - service: shopping_list.add
      data_template:
        item: "milk"

…or to remove an item…

action:
    - service: shopping_list.remove
      data_template:
        item: "milk"

Then to view items in the list, there will be a sensor with number of items in the list as state and the items listed in the attributes.


#2

Totally agree, I was expecting to be able to do something like:

trigger:
  platform: zone
  zone: zone.shop
  event: enter
action:
  service: notify.telegram
  data_template:
    message: '{{states.shopping_list.state}}'

#3

Agree. As I didn’t use let’s encrypt. So I don’t have ssl. Myself, cannot use google assistant and some feature that HA provide like shopping list. Although I have microphone attached but no ssl, cannot use.


#4

I’m sorry but I’m wondering is anyone working on this?


#5

i’d love to see this too. and it would be great if it were a general component that’s not relegated to a panel so we can easily make multiple lists, etc.


#6

:+1:

I want to be able to get a message playing on Sonos (TTS):

  • “Milk is added to your shopping list”.

But that seems impossible with the current API


#7

That can be easily done using automation IF my feature request here comes true.


#8

I’m trying to add items to the shopping list via telegram (text) and zanzito (voice).

Using automation, the message feedback can be added as additional service call under the action part of the automation. Will share with you the example later when I have access to my PC.

Anyway, please vote for this feature request. Thanks.


#9

Seems I’m late to the party, but I found this thread while looking if it would be possible to integrate the 2D barcode scanner I impulse bought on an Amazon Lightning Deal.

I’d like to have a component that would allow us to check household inventory if we make a random grocery stop, or to check when the last time we bought some random spice.


#10

I submitted a Pull Request which got released with version 0.71 of Home Assistant.

There are now two services which allow adding and removing items from the shopping list!


#11

Cool! Half of my request is fulfilled. Now waiting for a sensor that will list down all items in the shopping list.


#12

I want to support this request because I like the way the shopping list works and how it looks. However, I am struggling with working with the shopping list at the moment.
My intention is to send the filled shopping list via email and start a new one afterwards. At the moment, I am able to send it via email (using an external script reading the .shopping_list.json) but I have no idea how to clear the complete list to start a new one.

In the future, I also want to add voice control by snips. I guess the new services should help with that, so I already appreciate what has been done so far :slight_smile:


#13

great idea. appreciate if you can share on how you do that. thanks in advance.


#14

Sure. It is just a little quick & dirty python code that I added as a shell command to homeassistant:

#!/usr/bin/python
# coding: utf8
import json, smtplib, datetime

sender='XXX'
reciever='YYY'
account='ZZZ'
password='BBB'

with open('PATHTOHOMEASSISTANT/.shopping_list.json') as data_file:
    shoppingListData = json.load(data_file)
   
# build message
content = u"Shopping list:\n"
for entry in shoppingListData:
    if not entry['complete']:
        content += u"- %s\n" % entry['name']

content += u"\nThank you for shopping!"

mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(account,password)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (sender, reciever, "Shopping list", content)
mail.sendmail(sender,reciever,message.encode('utf-8'))
mail.close()

The code was tested using gmail. However it should work with other email providers by replacing the SMTP server accordingly. You need to enter the sender email address, the receiver email address and your mail account information that are hard coded (there certainly is room for improvements :wink:)


#15

Why not make an command line sensor? This is working in docker also.

sensor:    
  - platform: command_line
    name: shopping_list
    command: /config/shopping_list.py

shopping_list.py:
#!/usr/local/bin/python
# coding: utf8
import json

with open('/config/.shopping_list.json') as data_file:
    shoppingListData = json.load(data_file)

content = u"Boodschappen:\n"
for entry in shoppingListData:
    if not entry['complete']:
        content += u"- %s\n" % entry['name']

content += u"\n"

print(content)

Then this is possible:

trigger:
  platform: zone
  zone: zone.shop
  event: enter
action:
  service: notify.telegram
  data_template:
    message: '{{states.sensor.shopping_list.state}}'

#16

I’m wondering if someone is working on recognize 2D Barcode (specific hardware device or using image processing ?).
There is an API for matching barcode and a specific product. It could be cool to scan a barcode to automatically add the product to the shopping list.


#17

Thanks. Command line sensor works perfectly for me.


#18

This is great. Thanks for the simple python script. At first the sensor reported a status of unknown for me. I had to set the sensor up slightly differently in Hassio by prefacing the command with python3.

  - platform: command_line
    name: shopping_list
    command: python3 /config/shopping_list.py

#19

Hello All and Flovie in particular.
New to Homeassistant and Linux
Im trying to use your python script above but i dont know how to execute it.
I have created a folder under the homeassistant folder called “python_scripts” and created a send.py file there with your script , ofcourse edited with my e-mail address and path
home/homeassistant/.homeassistant/python_scripts/shopping_list.json
I dont know how to activate or run the script, can i create a button “send email” to run this script or can i controll it with a simple voice command?
Any help appreciated.

#!/usr/bin/python
# coding: utf8
import json, smtplib, datetime

sender='[email protected]'
reciever='[email protected]'
account='[email protected]'
password='xxx'

with open(' home/homeassistant/.homeassistant/python_scripts/shopping_list.json') as data_file:
    shoppingListData = json.load(data_file)
   
# build message
content = u"Shopping list:\n"
for entry in shoppingListData:
    if not entry['complete']:
        content += u"- %s\n" % entry['name']

content += u"\nThank you for shopping!"

mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(account,password)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (sender, reciever, "Shopping list", content)
mail.sendmail(sender,reciever,message.encode('utf-8'))
mail.close()

#20

Hi,

if you are using homeassistant in an environment where you are free to place and run python scripts. Than you can go the way I went:

  1. create a file with the pyhton code inside (for example sendshoppinglist.py)

  2. create a shell_command component in your config.yaml. For example:

    shell_command:
    sendshoppinglist: /sendshoppinglist.py

I didn’t get the correct indentation working in this example, so be careful with copy&paste. This code will expose the python code as a service to your home assistant.
3. You are free to call this service how you like. May it be voice command or an automation.

If you are in a more restricted environment, then you should take a look at the code by Mark_Meijer and use a notification component of your choice.

I hope this helped you out and pointed in the right direction.