本帖最后由 printusing163 于 2019-5-23 22:43 编辑
背景
由于Owntracks iOS端支持iBeacon协议,所以利用安卓机发送iBeacon信号,可以用旧苹果手机反向定位安卓机,不过目前的Owntracks插件并没有支持这个功能、没法定位多台安卓iBeacon,而且Owntracks插件被内嵌入到webhook中,很难写插件替代,所以就直接针对HA源码做补丁。
直接替换文件,
或者打补丁在home_assistant/components/owntracks/device_tracker.py 最后面的async_handle_message 函数改为以下代码,安卓机会被现实为devicetracker.beacon<android_id>
不知道大家有没有更好的方案,现在主要问题是没法重用HA中的校验机制。
def _custom_location_info(hass, context, message):
regions = message.get('inregions', [])
for region in regions:
device = "beacon_{}".format(region)
hass.async_create_task(context.async_see(
dev_id=device,
location_name=STATE_HOME,
source_type=SOURCE_TYPE_BLUETOOTH_LE,
))
def _custom_transition(hass, context, message):
device = "beacon_{}".format(message.get('desc', '').lower())
event = message.get('event', '')
event2loc = {
'enter': STATE_HOME,
'leave': 'not_home',
}
if event not in event2loc:
return
hass.async_create_task(context.async_see(
dev_id=device,
location_name=event2loc[event],
source_type=SOURCE_TYPE_BLUETOOTH_LE,
))
def _custom_hook(hass, context, message):
msgtype = message.get('_type')
if msgtype == 'location':
_custom_location_info(hass, context, message)
elif msgtype == 'transition':
_custom_transition(hass, context, message)
else:
return
async def async_handle_message(hass, context, message):
"""Handle an OwnTracks message."""
msgtype = message.get('_type')
_LOGGER.debug("Received %s", message)
handler = HANDLERS.get(msgtype, async_handle_unsupported_msg)
await handler(hass, context, message)
_custom_hook(hass, context, message)
|