找回密码
 立即注册

微信扫码登录

搜索
查看: 7342|回复: 29

[经验分享] 修改大佬xiao_miot,使儿童手表定位不在偏移

[复制链接]

14

主题

1578

回帖

5180

积分

论坛元老

积分
5180
金钱
3588
HASS币
0
发表于 2023-3-26 08:53:52 | 显示全部楼层 |阅读模式
修改了device_tracter.py,添加了坐标系转换,使儿童手表不在偏移。小米儿童手表6X测试通过,代码如下:
"""Support for Xiaomi device tracker."""
import logging
import time,math
from datetime import timedelta

from homeassistant.const import *  # noqa: F401
from homeassistant.components.device_tracker import (
    DOMAIN as ENTITY_DOMAIN,
)
from homeassistant.components.device_tracker.const import SOURCE_TYPE_GPS
from homeassistant.components.device_tracker.config_entry import TrackerEntity

from . import (
    DOMAIN,
    CONF_MODEL,
    XIAOMI_CONFIG_SCHEMA as PLATFORM_SCHEMA,  # noqa: F401
    MiotEntity,
    async_setup_config_entry,
    bind_services_to_entries,
)
from .core.miot_spec import (
    MiotSpec,
    MiotService,
)

_LOGGER = logging.getLogger(__name__)
DATA_KEY = f'{ENTITY_DOMAIN}.{DOMAIN}'
SCAN_INTERVAL = timedelta(seconds=60)

SERVICE_TO_METHOD = {}
def GCJ2WGS(lon,lat):
    a = 6378245.0 # 克拉索夫斯基椭球参数长半轴a
    ee = 0.00669342162296594323 #克拉索夫斯基椭球参数第一偏心率平方
    PI = 3.14159265358979324 # 圆周率

    x = lon - 105.0
    y = lat - 35.0

    dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x));
    dLon += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0;
    dLon += (20.0 * math.sin(x * PI) + 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
    dLon += (150.0 * math.sin(x / 12.0 * PI) + 300.0 * math.sin(x / 30.0 * PI)) * 2.0 / 3.0;

    dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x));
    dLat += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0;
    dLat += (20.0 * math.sin(y * PI) + 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
    dLat += (160.0 * math.sin(y / 12.0 * PI) + 320 * math.sin(y * PI / 30.0)) * 2.0 / 3.0;
    radLat = lat / 180.0 * PI
    magic = math.sin(radLat)
    magic = 1 - ee * magic * magic
    sqrtMagic = math.sqrt(magic)
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
    dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * PI);
    wgsLon = lon - dLon
    wgsLat = lat - dLat
    return [wgsLon,wgsLat]

async def async_setup_entry(hass, config_entry, async_add_entities):
    await async_setup_config_entry(hass, config_entry, async_setup_platform, async_add_entities, ENTITY_DOMAIN)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    hass.data.setdefault(DATA_KEY, {})
    hass.data[DOMAIN]['add_entities'][ENTITY_DOMAIN] = async_add_entities
    config['hass'] = hass
    model = str(config.get(CONF_MODEL) or '')
    spec = hass.data[DOMAIN]['miot_specs'].get(model)
    entities = []
    if isinstance(spec, MiotSpec):
        for srv in spec.get_services('watch', 'rearview_mirror', 'head_up_display'):
            if 'xiaoxun.watch.' in model:
                entities.append(XiaoxunWatchTrackerEntity(config, srv))
            elif srv.get_property('latitude', 'longitude'):
                entities.append(MiotTrackerEntity(config, srv))
    if not entities and ('xiaoxun.watch.' in model or 'xiaoxun.tracker.' in model):
        # xiaoxun.watch.sw763
        # xiaoxun.tracker.v1
        entities.append(XiaoxunWatchTrackerEntity(config))
    for entity in entities:
        hass.data[DOMAIN]['entities'][entity.unique_id] = entity
    async_add_entities(entities, update_before_add=True)
    bind_services_to_entries(hass, SERVICE_TO_METHOD)


class MiotTrackerEntity(MiotEntity, TrackerEntity):
    _attr_latitude = None
    _attr_longitude = None
    _attr_location_name = None
    _attr_location_accuracy = 0
    _disable_location_name = False

    def __init__(self, config, miot_service: MiotService = None):
        super().__init__(miot_service, config=config, logger=_LOGGER)

    async def async_added_to_hass(self):
        await super().async_added_to_hass()
        self._disable_location_name = self.custom_config_bool('disable_location_name')

    async def async_update(self):
        await super().async_update()
        if not self._available or not self._miot_service:
            return

        if prop := self._miot_service.get_property('latitude'):
            self._attr_latitude = prop.from_dict(self._state_attrs)
        if prop := self._miot_service.get_property('longitude'):
            self._attr_longitude = prop.from_dict(self._state_attrs)
        if prop := self._miot_service.get_property('current_address'):
            self._attr_location_name = prop.from_dict(self._state_attrs)

        for p in self._miot_service.get_properties('driving_status'):
            self._update_sub_entities(p, None, 'binary_sensor')

    @property
    def should_poll(self):
        """No polling for entities that have location pushed."""
        return True

    @property
    def source_type(self):
        """Return the source type, eg gps or router, of the device."""
        return SOURCE_TYPE_GPS

    @property
    def latitude(self):
        """Return latitude value of the device."""
        return self._attr_latitude

    @property
    def longitude(self):
        """Return longitude value of the device."""
        return self._attr_longitude

    @property
    def location_name(self):
        """Return a location name for the current location of the device."""
        if self._disable_location_name:
            return None
        return self._attr_location_name

    @property
    def location_accuracy(self):
        """Return the location accuracy of the device.
        Value in meters.
        """
        return self._attr_location_accuracy

    @property
    def battery_level(self):
        """Return the battery level of the device."""
        if not self._miot_service:
            return None
        sls = [self._miot_service, *self._miot_service.spec.get_services('battery')]
        for srv in sls:
            prop = srv.get_property('battery_level')
            if prop:
                return prop.from_dict(self._state_attrs)
        return None


class XiaoxunWatchTrackerEntity(MiotTrackerEntity):
    def __init__(self, config, miot_service: MiotService = None, miot_spec: MiotSpec = None):
        self._miot_spec = miot_spec
        super().__init__(config=config, miot_service=miot_service)

    @property
    def device_eid(self):
        did = f'{self.miot_did}'
        return did.replace('xiaoxun.', '')

    async def async_update(self):
        await super().async_update()
        await self.update_location()

    async def update_location(self):
        did = f'{self.miot_did}'
        mic = self.xiaomi_cloud
        if not did or not mic:
            return
        pms = {
            'app_id': '10025',
            'dids': [did],
            'params': {
                'CID': 50031,
                'model': self._model,
                'SN': int(time.time() / 1000),
                'PL': {
                    'Size': 1,
                    'Key': '78999898989898998',
                    'EID': self.device_eid,
                },
            },
        }
        rdt = await mic.async_request_api('third/api', pms) or {}
        loc = {}
        for v in (rdt.get('result') or {}).get('PL', {}).get('List', {}).values():
            loc = v.get('result', {})
            break
        if not loc:
            self.logger.warning('%s: Got xiaoxun watch location faild: %s', self.name_model, rdt)
            return
        self.logger.debug('%s: Got xiaoxun watch location: %s', self.name_model, rdt)
        gps = f"{loc.get('location', '')},".split(',')
        gps=GCJ2WGS(float(gps[0]),float(gps[1]))  #修改为wgs84坐标
        self._attr_latitude = float(gps[1])
        self._attr_longitude = float(gps[0])
        self._attr_location_name = loc.get('desc')
        self._attr_location_accuracy = int(loc.get('radius') or 0)
        tim = loc.get('timestamp', '')
        self.update_attrs({
            'timestamp': f'{tim[0:4]}-{tim[4:6]}-{tim[6:8]} {tim[8:10]}:{tim[10:12]}:{tim[12:14]}',
        })



评分

参与人数 4金钱 +30 收起 理由
咸味土豆 + 12 论坛有你更精彩!
Xi11 + 8 论坛有你更精彩!
anlong + 5 论坛有你更精彩!
ysst4 + 5 大神666!

查看全部评分

回复

使用道具 举报

4

主题

131

回帖

874

积分

高级会员

积分
874
金钱
739
HASS币
0
发表于 2024-7-20 09:48:01 | 显示全部楼层
本帖最后由 snowing 于 2024-7-20 09:59 编辑

找到了https://github.com/al-one/hass-xiaomi-miot/issues/367,5X需要将coord_type自定义为bd09
回复

使用道具 举报

4

主题

131

回帖

874

积分

高级会员

积分
874
金钱
739
HASS币
0
发表于 2024-7-20 09:19:51 | 显示全部楼层
隔壁的王叔叔 发表于 2024-7-15 08:19
现在不用看了,大佬已经整合了,直接可以用。

我是5X怎么选择坐标系呢?
回复

使用道具 举报

14

主题

1578

回帖

5180

积分

论坛元老

积分
5180
金钱
3588
HASS币
0
 楼主| 发表于 2024-7-15 08:19:24 | 显示全部楼层
xieahui 发表于 2024-7-15 07:20
新买儿童手表 做个记号回头来看

现在不用看了,大佬已经整合了,直接可以用。
回复

使用道具 举报

8

主题

2080

回帖

6167

积分

论坛元老

流水无味

积分
6167
金钱
4079
HASS币
145

灌水之王

发表于 2024-7-15 07:20:04 | 显示全部楼层
新买儿童手表 做个记号回头来看
回复

使用道具 举报

14

主题

1578

回帖

5180

积分

论坛元老

积分
5180
金钱
3588
HASS币
0
 楼主| 发表于 2023-5-22 09:06:11 | 显示全部楼层
zyling 发表于 2023-5-21 23:40
另外,
我的其他device_tracker 设备,能输出具体地址,还有另一个“定位”的实体,输出 home 或 not_home  ...

你可以用模板增加一个。或者通过wifi判断
回复

使用道具 举报

26

主题

241

回帖

1491

积分

金牌会员

积分
1491
金钱
1224
HASS币
0
发表于 2023-5-21 23:40:16 | 显示全部楼层
另外,
我的其他device_tracker 设备,能输出具体地址,还有另一个“定位”的实体,输出 home 或 not_home 这样的信息。
但是我的这个手表,接入ha出来的只有一个实体:device_tracker.xiaoxun_v5_3dfc_watch
这个实体输出是具体的地址;要怎么让它也能输出home 或 not_home  呢?
回复

使用道具 举报

14

主题

1578

回帖

5180

积分

论坛元老

积分
5180
金钱
3588
HASS币
0
 楼主| 发表于 2023-5-19 16:13:48 | 显示全部楼层
zyling 发表于 2023-5-19 15:19
真的没有,用了很久了,只有位置信息

那得问作者了
我的有电量
回复

使用道具 举报

26

主题

241

回帖

1491

积分

金牌会员

积分
1491
金钱
1224
HASS币
0
发表于 2023-5-19 15:19:02 | 显示全部楼层
隔壁的王叔叔 发表于 2023-5-19 14:32
电量也有吧,本来就有啊

真的没有,用了很久了,只有位置信息
回复

使用道具 举报

14

主题

1578

回帖

5180

积分

论坛元老

积分
5180
金钱
3588
HASS币
0
 楼主| 发表于 2023-5-19 14:32:12 | 显示全部楼层
zyling 发表于 2023-5-19 14:02
我的小寻儿童电话手表S1 使用这个插件可以接入HA了,但是只有一个位置实体可用 device_tracker.xiaoxun_v5_ ...

电量也有吧,本来就有啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-7-5 12:03 , Processed in 0.073816 second(s), 9 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表