I am working on a driver for LG TV devices. Communication with the device is done via websockets, but maybe this is not really relevant for this question.
When the TV is off, its network stack seems to be off, and there is no way to determine when the TV is turned on using the remote (no webhooks can be triggered when the TV boots up). To detect when this happens, I added a 1 minute ping:
void updated() {
...
// Ping every 1 minute to detect when TV is turned on using the remote
unschedule()
schedule '0 0/1 * ? * * *', 'pingDevice'
}
void pingDevice() {
if (!ipAddr || "${device.currentValue('networkStatus', true)}" == 'online') return
log_debug "Pinging ${ipAddr} ..."
if (NetworkUtils.ping(ipAddr, 1)?.packetsReceived == 1) connect()
}
This works fine, but it causes high values in the Logs/Device stats page, probably because ping()
is synchronous and the driver remains "active" while ping()
executes.
Since "Livingroon TV" was on almost all day today, a lot less ping()
were executed, hence the 34.0% vs 64.6%.
Despite the Device stats high values, I don't see any increase in the hub CPU usage. But it bothers me nevertheless
Questions:
-
Is there any asynchronous version of
ping()
available? I could not find anything in the "Developer Documentation".
I chose ping cause I though it will be less intensive on the hub resources, rather than trying to establish a websocket connection every 1 minute, but I might be wrong. -
Is there a more efficient way of detecting when an IP comes alive? I would like to keep the frequent checks, so that Hubitat is synced with the device as soon as it becomes alive.
Thank you!