dvdgeisler / DirigeraClient

Dirigera Client API: Java written client API to interface IKEA's new smarthome hub DIRIGERA
MIT License
74 stars 9 forks source link

Another Motion Sensor #54

Open daveabbott007 opened 1 year ago

daveabbott007 commented 1 year ago

Hi all, in my dump file my motion sensor has a different productCode "E1525" to the listed one "productCode" : "E1745". can this one be added please.

many thanks

}, { "id" : "6c7ef29d-b9d4-4e65-b712-59086514330f_1", "type" : "sensor", "deviceType" : "motionSensor", "createdAt" : "2022-11-13T12:20:12.000Z", "isReachable" : true, "lastSeen" : "2022-11-27T20:32:26.000Z", "attributes" : { "customName" : "Toilet motion", "model" : "TRADFRI motion sensor", "manufacturer" : "IKEA of Sweden", "firmwareVersion" : "1.2.214", "hardwareVersion" : "1", "serialNumber" : "000B57FFFE96AC79", "productCode" : "E1525", "batteryPercentage" : 52, "isOn" : false, "permittingJoin" : false, "otaStatus" : "upToDate", "otaState" : "updateComplete", "otaProgress" : 0, "otaPolicy" : "autoUpdate", "otaScheduleStart" : "00:00", "otaScheduleEnd" : "00:00", "sensorConfig" : { "scheduleOn" : false, "onDuration" : 180, "schedule" : { "onCondition" : { "time" : "17:00" }, "offCondition" : { "time" : "09:00" } } } }, "capabilities" : { "canSend" : [ "isOn" ], "canReceive" : [ "customName" ] },

TheMrBooyah commented 1 year ago

The ProductCode is not used anywhere. It's merely a device identification. We use the deviceType in order to detect if anything is supported.

It seems to me that this motion sensor should already be accepted by the current client

daveabbott007 commented 1 year ago

This Motion Sensor does show up but does not change state, it did change state under homekit.

was kinda hoping the different productCode was the reason for it not working properly.

TheMrBooyah commented 1 year ago

Have you tried restarting the add on?

Do the logs indicate a state change?

daveabbott007 commented 1 year ago

addons was restarted at 18:30, here are the entries from the log 2022-11-29 18:31:15.718 DEBUG 63 --- [or-http-epoll-3] d.d.iot.dirigera.client.mqtt.MqttBridge : Create device: id=6c7ef29d-b9d4-4e65-b712-59086514330f_1, name=Toilet motion, category=SENSOR, type=MOTION_SENSOR 2022-11-29 18:31:15.720 DEBUG 63 --- [or-http-epoll-3] d.d.iot.dirigera.client.mqtt.MqttBridge : Publish message to MQTT: topic=homeassistant/binary_sensor/d705fc32-eea6-4dde-8f06-355b36306149_1/6c7ef29d-b9d4-4e65-b712-59086514330f_1/config, payload={"object_id":"6c7ef29d-b9d4-4e65-b712-59086514330f_1","unique_id":"6c7ef29d-b9d4-4e65-b712-59086514330f_1","name":"Toilet motion","device":{"identifiers":["000B57FFFE96AC79"],"manufacturer":"IKEA of Sweden","model":"TRADFRI motion sensor","hw_version":"1","sw_version":"1.2.214","name":"Toilet motion","via_device":"d705fc32-eea6-4dde-8f06-355b36306149_1"},"payload_on":"\"ON\"","payload_off":"\"OFF\"","state_topic":"homeassistant/binary_sensor/d705fc32-eea6-4dde-8f06-355b36306149_1/6c7ef29d-b9d4-4e65-b712-59086514330f_1/state","availability":{"topic":"homeassistant/binary_sensor/d705fc32-eea6-4dde-8f06-355b36306149_1/6c7ef29d-b9d4-4e65-b712-59086514330f_1/availability","payload_available":"\"ONLINE\"","payload_not_available":"\"OFFLINE\""}}, factory=MotionSensorEventHandler, deviceId=6c7ef29d-b9d4-4e65-b712-59086514330f_1, deviceType=MOTION_SENSOR, deviceCategory=SENSOR 2022-11-29 18:31:15.724 DEBUG 63 --- [or-http-epoll-3] d.d.iot.dirigera.client.mqtt.MqttBridge : Publish message to MQTT: topic=homeassistant/binary_sensor/d705fc32-eea6-4dde-8f06-355b36306149_1/6c7ef29d-b9d4-4e65-b712-59086514330f_1/state, payload="OFF", factory=MotionSensorEventHandler, deviceId=6c7ef29d-b9d4-4e65-b712-59086514330f_1, deviceType=MOTION_SENSOR, deviceCategory=SENSOR 2022-11-29 18:31:15.727 DEBUG 63 --- [or-http-epoll-3] d.d.iot.dirigera.client.mqtt.MqttBridge : Publish message to MQTT: topic=homeassistant/binary_sensor/d705fc32-eea6-4dde-8f06-355b36306149_1/6c7ef29d-b9d4-4e65-b712-59086514330f_1/availability, payload="ONLINE", factory=MotionSensorEventHandler, deviceId=6c7ef29d-b9d4-4e65-b712-59086514330f_1, deviceType=MOTION_SENSOR, deviceCategory=SENSOR

Monitoring with MQTT explorer and no changes when i active the motion sensor (except the toilet light coming on as setup in Dirigera hub) and no new lines in log for motion

TheMrBooyah commented 1 year ago

I'm noticing the same behaviour in my own installation.

I'll have a look

dvdgeisler commented 1 year ago

Unfortunately, the motion sensor does not work for me either... It has the attribute "isOn", whose status is reported to HA. However, for my sensor, it is always set to false. Also, no events are coming in over the websocket, indicating that the sensor has detected something.

TheMrBooyah commented 1 year ago

Yeah I noticed this as well. Currently in the process of capturing traffic between Hass and the gateway. Since homekit does work

TheMrBooyah commented 1 year ago

So I've been looking at it but it seems the device that can be queried from the rest API isn't updating for some reason. I've seen some traffic on another port (I believe 8000) but that's the homekit integration which does update.

I'll continue to look into it but atm I'm at a loss about why this sensor isn't updating

dvdgeisler commented 1 year ago

Is there any chance to write an client to operate the HomeKit interface?

TheMrBooyah commented 1 year ago

Well that's what home assistant is already doing with the 'Homekit controller' integration. It is however not a foolproof solution.

What I did notice is that the motion sensor is the only one that has canSend capabilities and I suspect that's what we should be looking into.

dvdgeisler commented 1 year ago

Controllers have that capabilities as well, e.g.:

{
  "id": "26d8025a-1b5b-4248-991b-f85b250d6199_1",
  "type": "controller",
  "deviceType": "lightController",
  "createdAt": "2022-11-02T21:15:16.000Z",
  "isReachable": true,
  "lastSeen": "2022-11-10T03:05:55.000Z",
  "attributes": {
    "customName": "Remote",
    "model": "Remote Control N2",
    "manufacturer": "IKEA of Sweden",
    "firmwareVersion": "1.0.024",
    "hardwareVersion": "1",
    "serialNumber": "84B4DBFFFE221943",
    "productCode": "E2001",
    "batteryPercentage": 100,
    "isOn": false,
    "lightLevel": 1,
    "permittingJoin": false,
    "otaStatus": "upToDate",
    "otaState": "readyToCheck",
    "otaProgress": 0,
    "otaPolicy": "autoUpdate",
    "otaScheduleStart": "00:00",
    "otaScheduleEnd": "00:00"
  },
  "capabilities": {
    "canSend": [
      "isOn",
      "lightLevel"
    ],
    "canReceive": [
      "customName"
    ]
  },
  "room": {
    "id": "300918c6-bd34-428c-8b86-c03e95ff981b",
    "name": "MyRoom",
    "color": "ikea_red_no_39",
    "icon": "none"
  },
  "deviceSet": [],
  "remoteLinks": [],
  "isHidden": false
}
TheMrBooyah commented 1 year ago

I did find a project that does some homekit stuff for Java here

However, I'm not sure if we want to go that route since that is also already in the home assistant core.

dvdgeisler commented 1 year ago

I am currently working on a Homekit interface. Even if Home Assistant already provides this feature. However, I myself don't use Home-Assistant (except to test stuff in this project). HAP-Java, was a good starting point. Unfortunately, it only offers the possibility to implement Homekit Accessories (server mode), not to connect one (client mode). Anyway, i found this gem: hap-client. It's a NodeJs-based implementation of a Homekit client. Yet, porting to Java is quite tricky. After a week of repeating my crypto lectures, i have successfully paired with the Dirigera using its Homekit interface. The demo is available at HomeKit.java. Be cautious, after pairing there is no code to unpair. Hence, I keep resetting the Dirigera from time to time, which leads to losing all settings on the Diregera, including paired devices.

TheMrBooyah commented 1 year ago

I asked the reddit community of the Tradfri if anyone knew why it is not being updated on the WS or Restful API and this is the response I got:

That sounds like a bug in the Dirigera, the sensor itself doesn’t expose an API (except of course ZigBee) and can’t really be queried as it’s a battery device. It’s up to the hub to cache the last reported state and present that via an API.

One advise, if you’re trying to use this programmatically ditch the Dirigera - their API sucks and isn’t really open or documented. Install ZigBee2MQTT with something like the Sonoff ZigBee 3.0 dongle and use MQTT to control it programmatically as MQTT is open and well documented protocol. Another option is to use the old hub, the Tradfri Hub, which uses COAP for it’s API. While COAP is an slow, ancient protocol at least it’s well documented. Using WS for controlling smart devices is very bad idea, the entire industrial IoT industry uses MQTT for a reason.

Resources:

https://aws.amazon.com/what-is/mqtt/

https://coap.technology/

https://www.hivemq.com/blog/understanding-the-differences-between-mqtt-and-websockets-for-iot/

https://www.hivemq.com/blog/mqtt-vs-coap-for-iot/

ETA: After explaining the goal of this project, it seems he hopes the bugs get resolved :)