Local Deployment for SureFlap / SurePetCare Connect using only local MQTT Broker

So I finally got around to getting this setup. Here’s a real dumb question. Should things automatically show up in HA? I don’t see any new MQTT devices…

edit: I see I have red blinking ears too, so I’m not doing something right.

Red blinking ears is bad. Means that the hub hasn’t connected to the web service. So you probably haven’t got the docker compose working correctly and listening on port 443 or that you haven’t updated your internal DNS to point to the correct IP of the docker stack. If you did you should see a request come in on the console to “web” for your serial number.
Green blinking ears means it has the credentials file and now unable to connect to the MQTT endpoint.

The URL resolves to the local address. Should there be a website that I can see on 80/443?

I Get

You should be sending a POST request!

When I connect

You should be able to do a curl to the IP with the serial number and mac address values:

sn=H0xx-0xxxxxx
mac=0000xxxxxxxxxxxx
d=$(date +%Y%m%d-%H%H%S)
fw=2.43
curl -v -k -d "serial_number=$sn&mac_address=$mac&product_id=1&firmware_version=$fw" -H "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" -H "Content-Type: application/x-www-form-urlencoded" -X POST -o $sn-$mac-$fw-$d.bin https://internalhost:443/api/credentials

Where internalhost points to your instance and you have updated your serial and mac address

It may be my Docker ignorance here then. Is there something I need to do in Docker to add another IP, or is setting BINDIP enough?

The BINDIP will only work if you have added the IP to the physical host. So you will need to add a secondary IP, if you are using ubuntu/debian you can use netplan, otherwise bind to your main host IP as long as 443 and 8883 are free.

cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.1.240/24
        - 192.168.1.241/24
      gateway4: 192.168.1.254
      nameservers:
        addresses: [192.168.1.254]

And then you won’t be able to use DHCP.
Then using “ip a” will show you have a secondary IP… time to update the documentation with that too :slight_smile:

Finally found time to commit a lot of the changes I have made, moved the code around a lot to make it more sensible to read, especially around the message generation. Still needs some work on the cat flap, but oddities where I was incorrectly calculating timestamps has been fixed.

5 Likes

Great work, the Non Matched Messages seem to have gone. Unfortunately the catflap curfewmode now seems to cause problems for pethub.

When the database is created it populates the curfew state as “0” despite it existing in the start.json I have changed it in the database to 1 for off and pethub no longer stops.

I have ordered a pet chip that should be here any day, I figured it could come in handy if you need to see the result of certain settings and stop the cats getting grumpy with me shoving them in and out!

Just getting back to trying this again. I started over fresh and got this error:

pethub    | Traceback (most recent call last):
pethub    |   File "pethubmqtt.py", line 476, in <module>
pethub    |     curfewstate = {'State':CurfewOnOff(device.curfewenabled).name, 'Lock time':str(device.lock_time),'Unlock time':str(device.unlock_time)}
pethub    |   File "/usr/local/lib/python3.8/enum.py", line 339, in __call__
pethub    |     return cls.__new__(cls, value)
pethub    |   File "/usr/local/lib/python3.8/enum.py", line 663, in __new__
pethub    |     raise ve_exc
pethub    | ValueError: 0 is not a valid CurfewOnOff
pethub exited with code 1

Also had an interesting error.

mosquitto          | 1621520190: auto-EAD5F931-1AEE-37A9-65F2-A9A776090C66 1 pethublocal/#
mosquitto          | 1621520190: auto-EAD5F931-1AEE-37A9-65F2-A9A776090C66 0 homeassistant/+/pethub/+/state
mosquitto          | 1621520190: auto-EAD5F931-1AEE-37A9-65F2-A9A776090C66 0 homeassistant/+/pethub/+/set
mosquitto          | 1621520190: 6thximooON8pAD0oT4Nev6 0 homeassistant/switch/pethub/device_D98122FEFFEC1054_curfew/state
mosquitto          | 1621520190: 6thximooON8pAD0oT4Nev6 homeassistant/switch/pethub/device_D98122FEFFEC1054_curfew/status
mosquitto          | 1621520314: auto-8E263FDB-1E19-7976-CE2D-18D717819FC1 1 pethublocal/#
mosquitto          | 1621520314: auto-8E263FDB-1E19-7976-CE2D-18D717819FC1 0 homeassistant/+/pethub/+/state
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a679a6 0610 132 208 33 3 8c 0f 80
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"OP": "Battery", "Battery": "5.2575", "Time": "15:128"}, {"OP": ["Battery"]}], "timestamp": "2021-05-20 15:00:54", "operation": "Status"}
pethub             | HA message T=homeassistant/sensor/pethub/device_D98122FEFFEC1054_battery/state QoS=0 Msg=b'5.2575'
pethub             | Hub Parsed: {"device": "Home hub", "mac_address": "0000801F121D6719", "message": [{"OP": "Uptime", "Uptime": "3540", "TS": "20-15:03:47", "Reconnect": "1"}, {"OP": ["Uptime"]}], "timestamp": "2021-05-20 14:18:34", "operation": "Status"}
pethub             | Hub    Raw: pethublocal/messages 1 60a67a53 0620 10 00003540 20 15 03 47 60a67a53 1
pethub             | Hub Parsed: {"device": "Home hub", "mac_address": "0000801F121D6719", "message": [{"OP": "Other", "Other": "Other"}, {"OP": "Other"}], "timestamp": "2021-05-20 14:18:34", "operation": "Status"}
pethub             | Hub    Raw: pethublocal/messages 1 60a67a54 0630 132 209 27 1 d4
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a67a95 0640 132 210 33 3 8c 0f 84
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"OP": "Battery", "Battery": "5.2575", "Time": "15:132"}, {"OP": ["Battery"]}], "timestamp": "2021-05-20 15:04:53", "operation": "Status"}
pethub             | HA message T=homeassistant/sensor/pethub/device_D98122FEFFEC1054_battery/state QoS=0 Msg=b'5.2575'
pethub             | Hub Parsed: {"device": "Home hub", "mac_address": "0000801F121D6719", "message": [{"OP": "Boot", "Boot": "Boot"}, {"OP": "Boot"}], "timestamp": "2021-05-20 14:18:34", "operation": "Status"}
pethub             | Hub    Raw: pethublocal/messages 1 60a67a95 0650 132 211 55 6 54 47 f0 be c8 17
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a67bad 0660 8 20 0f 08 b2 00 00 00 00 00 00 29 01 3a
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"Msg": "60a67bad 0660 8 20 0f 08 b2 00 00 00 00 00 00 29 01 3a"}, {"OP": ["8"]}], "timestamp": "2021-05-20 15:09:33", "operation": "Status"}
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a67bae 0670 132 212 621 9 0f 08 b2 00 00 00 00 00 00
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"OP": "PetMovement", "PetOffset": "621", "Animal": "Unknown Pet", "Direction": "Outside", "State": "OFF"}, {"OP": ["PetMovement"]}], "timestamp": "2021-05-20 15:09:34", "operation": "Status"}
pethub             | HA message T=homeassistant/sensor/pethub/pet_unknown pet/state QoS=0 Msg=b'Outside'
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a67d6c 0680 8 20 0f 10 b2 00 00 00 00 00 00 2a 01 9f
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"Msg": "60a67d6c 0680 8 20 0f 10 b2 00 00 00 00 00 00 2a 01 9f"}, {"OP": ["8"]}], "timestamp": "2021-05-20 15:17:00", "operation": "Status"}
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a67d6c 0690 132 213 621 9 0f 10 b2 00 00 00 00 00 00
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"OP": "PetMovement", "PetOffset": "621", "Animal": "Unknown Pet", "Direction": "Outside", "State": "OFF"}, {"OP": ["PetMovement"]}], "timestamp": "2021-05-20 15:17:00", "operation": "Status"}
pethub             | HA message T=homeassistant/sensor/pethub/pet_unknown pet/state QoS=0 Msg=b'Outside'
pethub             | Door    Raw: pethublocal/messages/D98122FEFFEC1054 1 60a681eb 06a0 8 01 0f 23 62 00 08 2b 44 03 2b 2b 01 a1
pethub             | Door Parsed: {"device": "Kitchen", "mac_address": "D98122FEFFEC1054", "message": [{"Msg": "60a681eb 06a0 8 01 0f 23 62 00 08 2b 44 03 2b 2b 01 a1"}, {"OP": ["8"]}], "timestamp": "2021-05-20 15:36:11", "operation": "Status"}
pethub             | Traceback (most recent call last):
pethub             |   File "pethubmqtt.py", line 653, in <module>
pethub             |     mc.loop_forever()
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
pethub             |     rc = self.loop(timeout, max_packets)
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 1181, in loop
pethub             |     rc = self.loop_read(max_packets)
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 1572, in loop_read
pethub             |     rc = self._packet_read()
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
pethub             |     rc = self._packet_handle()
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 2936, in _packet_handle
pethub             |     return self._handle_publish()
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 3220, in _handle_publish
pethub             |     self._handle_on_message(message)
pethub             |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 3429, in _handle_on_message
pethub             |     callback(self, self._userdata, message)
pethub             |   File "pethubmqtt.py", line 128, in on_petdoor_hub_message
pethub             |     pethub=phlp.decodehubmqtt(msg.topic,msg.payload.decode("utf-8"))
pethub             |   File "/code/pethubpacket.py", line 708, in decodehubmqtt
pethub             |     response.message = parsedoorframe(mac_address, int(msgsplit[4]),"".join(msgsplit[5:]))
pethub             |   File "/code/pethubpacket.py", line 588, in parsedoorframe
pethub             |     updatedbtag('petstate',tag,device,'timestamp', localtimestampfromnow()) #Update timestamp
pethub             | NameError: name 'tag' is not defined
pethub exited with code 1

Fixed some typos and added support in the Enum to stop it crashing… should be a whole lot more stable now.

1 Like

It’s still not happy with the catflap. There is a warning when generating the database about four columns with only three being used.

Then I ran ran docker-compose and after some time pethub exits

msgs      | 1621804185.990600584pethublocal/messages/D3D100CFF9D5B37060aab68a 1000 127 07 00 0d 00 6d 52 6f 55 00 00 00 00 05
msgs      | 1621804185.990886556pethublocal/messages/D3D100CFF9D5B37060aab68a 1000 127 01 00 0e 00 6d 52 6f 55 0c 00
msgs      | 1621804194.384943447pethublocal/messages/D3D100CFF9D5B37060aac4a2 03b0 126 0b 00 00 0d 00 6d 52 6f 55 07 00 00 0b 00 00 0e 00 6d 52 6f 55 01 00 00 18 0c 00 67 00 6d 52 6f 55 d3 16 00 00 d8 0c 00 00 21 01 00 00 00 00 00 00
pethub    | Cat Flap    Raw: pethublocal/messages/D3D100CFF9D5B370 1 60aac4a2 03b0 126 0b 00 00 0d 00 6d 52 6f 55 07 00 00 0b 00 00 0e 00 6d 52 6f 55 01 00 00 18 0c 00 67 00 6d 52 6f 55 d3 16 00 00 d8 0c 00 00 21 01 00 00 00 00 00 00
pethub    | Cat Flap Parsed: {"device": "Flap", "mac_address": "D3D100CFF9D5B370", "message": [{"framets": "2021-05-23 21:09:45", "data": {"msg": "00", "counter": "13"}, "OP": "Ack", "MSG": "07"}, {"framets": "2021-05-23 21:09:45", "data": {"msg": "00", "counter": "14"}, "OP": "Ack", "MSG": "01"}, {"framets": "2021-05-23 21:09:45", "data": {"msg": "0c", "counter": "103"}, "OP": "Battery", "Battery": "5.843"}, {"OP": ["Ack", "Ack", "Battery"]}], "timestamp": "2021-05-23 21:09:54", "operation": "Status"}
pethub    | Traceback (most recent call last):
pethub    |   File "/usr/local/lib/python3.8/site-packages/box/box.py", line 461, in __getitem__
pethub    |     return super().__getitem__(item)
pethub    | KeyError: 'topic'
pethub    | 
pethub    | The above exception was the direct cause of the following exception:
pethub    | 
pethub    | Traceback (most recent call last):
pethub    |   File "pethubmqtt.py", line 612, in <module>
pethub    |     mc.loop_forever()
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
pethub    |     rc = self.loop(timeout, max_packets)
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 1181, in loop
pethub    |     rc = self.loop_read(max_packets)
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 1572, in loop_read
pethub    |     rc = self._packet_read()
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
pethub    |     rc = self._packet_handle()
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 2936, in _packet_handle
pethub    |     return self._handle_publish()
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 3220, in _handle_publish
pethub    |     self._handle_on_message(message)
pethub    |   File "/usr/local/lib/python3.8/site-packages/paho/mqtt/client.py", line 3429, in _handle_on_message
pethub    |     callback(self, self._userdata, message)
pethub    |   File "pethubmqtt.py", line 274, in on_catflap_hub_message
pethub    |     hubpub(ackmsg['topic'],ackmsg['msg'])
pethub    |   File "/usr/local/lib/python3.8/site-packages/box/box.py", line 482, in __getitem__
pethub    |     raise BoxKeyError(str(err)) from _exception_cause(err)
pethub    | box.exceptions.BoxKeyError: "'topic'"
pethub exited with code 1

Hmm my hub seems to be stuck with red ears and I just see these logs repeating:

pethub-web         | Post payload : {"serial_number": "H004-0088731", "mac_address": "0000801F121D6719", "product_id": "1", "firmware_version": "2.43"}
pethub-web         | Firmware already downloaded /web/creds/H004-0088731-1.177-00.bin
pethub-web         | 192.168.241.236 - - [24/May/2021 07:48:08] "POST /api/credentials HTTP/1.1" 200 -
pethub-web         | Post payload : {"serial_number": "H004-0088731", "mac_address": "0000801F121D6719", "product_id": "1", "firmware_version": "2.43"}
pethub-web         | Firmware already downloaded /web/creds/H004-0088731-1.177-00.bin
pethub-web         | 192.168.241.236 - - [24/May/2021 07:50:07] "POST /api/credentials HTTP/1.1" 200 -
pethub-web         | Post payload : {"serial_number": "H004-0088731", "mac_address": "0000801F121D6719", "product_id": "1", "firmware_version": "2.43"}
pethub-web         | Firmware already downloaded /web/creds/H004-0088731-1.177-00.bin
pethub-web         | 192.168.241.236 - - [24/May/2021 07:52:06] "POST /api/credentials HTTP/1.1" 200 -
pethub-web         | Post payload : {"serial_number": "H004-0088731", "mac_address": "0000801F121D6719", "product_id": "1", "firmware_version": "2.43"}
pethub-web         | Firmware already downloaded /web/creds/H004-0088731-1.177-00.bin
pethub-web         | 192.168.241.236 - - [24/May/2021 07:54:05] "POST /api/credentials HTTP/1.1" 200 -
pethub-web         | Post payload : {"serial_number": "H004-0088731", "mac_address": "0000801F121D6719", "product_id": "1", "firmware_version": "2.43"}
pethub-web         | Firmware already downloaded /web/creds/H004-0088731-1.177-00.bin
pethub-web         | 192.168.241.236 - - [24/May/2021 07:56:04] "POST /api/credentials HTTP/1.1" 200 -

If it is flashing red then the credentials file must be busted. Check docker/output/web and there should be a H0xx…2.43.bin file, the 2.43.original.bin is the one downloaded from Surepet and the one without original is the modified one.
If the hub is failing to download it then I would try removing the credentials .bin file and see if it downloads it again from Surepet, and works again. But that does seem odd to me.
Just trying to fix up the cat flap issues, but ran out of time tonight so will try and finish it tomorrow night.

Sweet. I think everything is working now. I see the MQTT entries. Using this, is there a way for me to set the pets home/away via HA now?

edit: I see that I can do that by using mqtt.publish. Sorry, slowly figuring out MQTT. What would be the best way to do this? Create an input dropdown and some automation rules?

So it seems I’m wrong. How can update the pet’s status from HA?

Ok turned out wiping the bin files made no difference.
Ended up deleting the database and recreating it and restarting everything. Then it all started working again.

I imagine there need to be an overwrite part added to the pethub docker, that allows you to send an overwrite mqtt command and it will then update the local database and also the entity in HA.

Should be easy enough. Its a normal feature request I think.

Call it lets say, “Mark pet inside” and “Mark pet outside”.

I was thinking I could change the pet type from a sensor to a switch. How about if I have it as a config option in the ini so then when the entity is created it could be created as a switch vs sensor. Then on / off for inside / outside. The only problem I have found is when using HA on my phone sometimes I brush over the switch and it changes… is that the sort of thing you’re looking for?

1 Like