I finally figured out a way to create sensors for my Smart Feed Automatic Dog and Cat Feeder, 2nd Generation pet feeder.
I used this PetSafe Smart Feed - Python API to create some curl commands to request the required token and device id, and then created some {{REST}} sensors. If you dig around in the devices.py and api.py you will see you can do a lot more than just create sensors if you want to.
To get started:
You first need to request a code from PetSafe. Just replace [email protected] with the email address you have registered email with PetSafe and request the following POST: (You may need to create a single-line curl command to get it to work, remove the \ and return.)
curl --request POST \
--url https://users-api.ps-smartfeed.cloud.petsafe.net/users/ \
--header 'Content-Type: application/json' \
--data '{ "email": "[email protected]" }'
You will very quickly receive an email from PetSafe with your code:
It should look something like this:
[email protected]
**400-632** is your code
Now replace [email protected] with your registered email address and replace the XXX-XXX with the code PetSafe sent:
curl --request POST \
--url https://users-api.ps-smartfeed.cloud.petsafe.net/users/tokens \
--header 'Content-Type: application/json' \
--data '{ "email": "[email protected]", "code": "XXX-XXX" }'
The response should look somethis like this:
{
âidentityIdâ: âxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ,
âaccessTokenâ: âxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ
ârefreshTokenâ: âxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ ,
âdeprecatedTokenâ: âxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxâ
}
Copy the âdeprecatedTokenâ and save it in Secrets.yaml as smartfeed_token: xxxxxxâŚ
Next you need to get the feeder id (PetSafe calls it thing_name) to work with the feeder. You can use another curl command or create a sensor with the information you have to retrive the âthing_nameâ.
Replace the yourtoken with the deprecatedToken.
curl --request GET \
--url https://api.ps-smartfeed.cloud.petsafe.net/api/v2/feeders \
--header 'Content-Type: application/json' \
--header 'Token: yourtoken'
Using the curl command, the response should look something like this:
[
{
"id": 29066,
"serial": null,
"thing_name": "**x-XXXXXXXXXXXX**",
"battery_voltage": "40",
"region": "",
"connection_status": 2,
"is_food_low": 0,
"is_adapter_installed": true,
"is_batteries_installed": false,
"firmware_version": "V2.2.0",
"product_name": "SmartFeed_2.0",
"revision_desired": 56,
"revision_reported": 56,
"food_sensor_current": 0,
"food_sensor_reference": 0,
"network_rssi": -63,
"network_snr": 24,
"connection_status_timestamp": "2021-03-14 21:39:19",
"created_at": "2020-01-05 20:47:35",
"deleted_at": null,
"settings": {
"friendly_name": "Smart Feed",
"paused": false,
"slow_feed": true,
"child_lock": false,
"notify_on_feed": true,
"notify_on_connection_state_change": true,
"notify_on_low_food": true,
"notify_on_empty_food": true,
"notify_on_error": true,
"notify_on_community_message": true,
"timezone": "America\/New_York",
"ssid": null,
"pet_type": "cat",
"updated_at": "2020-12-25 14:28:43"
},
"schedules": [
{
"time": "7:00",
"amount": 1,
"id": 501089,
"updated_at": "2021-03-14 07:17:35"
},
{
"time": "17:00",
"amount": 2,
"id": 501090,
"updated_at": "2021-03-14 07:17:35"
}
]
}
]
This includes the complete response. You need the âthing_nameâ to get information from an individual feeder.
Instead, If you want to you can set up a sensor to start using it in HA. In my configuration.yaml file I created the sensor âsmartfeed_feedersâ. In this example I only set up json_attributes for 3 items: thing_name, settings, and schedules. You could also include any of the other attributes you might want.
- platform: rest
name: smartfeed_feeders
method: GET
json_attributes:
- data
resource: https://api.ps-smartfeed.cloud.petsafe.net/api/v2/feeders/
headers:
Content-Type: application/json
token: !secret smartfeed_token
value_template: "{{ states.sensor.smartfeed_feeders.attributes['thing_name'] }}"
json_attributes:
- thing_name
- settings
- schedules
The sensor.smartfeed_feeders in the Development Tools - STATES looks like this:
thing_name: x-XXXXXXXXXXXX
settings:
friendly_name: Smart Feed
paused: false
slow_feed: true
child_lock: false
notify_on_feed: true
notify_on_connection_state_change: true
notify_on_low_food: true
notify_on_empty_food: true
notify_on_error: true
notify_on_community_message: true
timezone: America/New_York
ssid: null
pet_type: cat
updated_at: '2020-12-25 14:28:43'
schedules:
- time: '7:00'
amount: 1
id: 501089
updated_at: '2021-03-14 07:17:35'
- time: '17:00'
amount: 2
id: 501090
updated_at: '2021-03-14 07:17:35'
friendly_name: smartfeed_feeders
After getting the âthing_nameâ, I setup another sensor to retrieve data from the individual feeder. On the resource line replace x-XXXXXXXXXXXX with your retrieved âthing_nameâ.
- platform: rest
name: smartfeed_last_messages
method: GET
json_attributes:
- data
resource: https://api.ps-smartfeed.cloud.petsafe.net/api/v2/feeders/x-XXXXXXXXXXXX/messages?days=1
headers:
Content-Type: application/json
token: !secret smartfeed_token
value_template: "{{ states.sensor.smartfeed_last_messages.attributes['message_type'] }}"
json_attributes:
- "message_type"
- "created_at"
- "payload"
The sensor.smartfeed_last_messages will look like this in Development Tools - STATES:
message_type: FEED_DONE
created_at: '2021-03-15 11:00:06'
payload:
isFoodLow: 0
amount: 1
source: schedule
h: 11
m: 0
sensorReading1Infrared: 29537
sensorReading2Infrared: 29534
time: 1615806006
friendly_name: smartfeed_last_messages
The âcreated_atâ is UTC and the âtimeâ is EPOCH.
You will need to play around with the Developer Tools - TEMPLATE to work with the data, as an example:
{{ states.sensor.smartfeed_last_messages.attributes[âpayloadâ][âtimeâ] }} = 1615806006
{{ states.sensor.smartfeed_last_messages.attributes[âpayloadâ][âtimeâ] | timestamp_local}} = 2021-03-15 18:00:07
I just discovered the api a few days ago and just started working with it. I know an integration could probably be created by someone.