Skip to content

Commit

Permalink
🛎️ add service xiaoai_weakup
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Dec 3, 2021
1 parent b7fa635 commit 018b189
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,19 @@ data:
```yaml
service: xiaomi_miot.intelligent_speaker
data:
entity_id: media_player.xiaomi_lx04_xxxx
entity_id: media_player.xiaoai_lx04_xxxx
text: Turn on the light
execute: true # Execute text directive.
silent: true # Silent execution.
```

#### [`xiaomi_miot.xiaoai_weakup`](https://my.home-assistant.io/redirect/developer_call_service/?service=xiaomi_miot.xiaoai_weakup)
```yaml
service: xiaomi_miot.xiaoai_weakup
data:
entity_id: media_player.xiaoai_lx04_xxxx
```

> [More services](https://github.com/al-one/hass-xiaomi-miot/blob/master/custom_components/xiaomi_miot/services.yaml)


Expand Down
9 changes: 8 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,19 @@ data:
```yaml
service: xiaomi_miot.intelligent_speaker
data:
entity_id: media_player.xiaomi_lx04_xxxx
entity_id: media_player.xiaoai_lx04_xxxx
text: Turn on the light
execute: true # Execute text directive.
silent: true # Silent execution.
```

#### [`xiaomi_miot.xiaoai_weakup`](https://my.home-assistant.io/redirect/developer_call_service/?service=xiaomi_miot.xiaoai_weakup)
```yaml
service: xiaomi_miot.xiaoai_weakup
data:
entity_id: media_player.xiaoai_lx04_xxxx
```

> 查看[更多服务](https://github.com/al-one/hass-xiaomi-miot/blob/master/custom_components/xiaomi_miot/services.yaml)


Expand Down
30 changes: 28 additions & 2 deletions custom_components/xiaomi_miot/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
},
),
},
'xiaoai_weakup': {
'method': 'async_xiaoai_weakup',
'schema': XIAOMI_MIIO_SERVICE_SCHEMA.extend(
{
vol.Optional('text', default=None): cv.string,
vol.Optional('throw', default=False): cv.boolean,
},
),
},
}


Expand Down Expand Up @@ -318,20 +327,37 @@ def intelligent_speaker(self, text, execute=False, silent=False, **kwargs):
pms.append(sil)
return self.miot_action(srv.iid, act.iid, pms, **kwargs)
else:
_LOGGER.warning('%s have no action: %s', self.name, anm)
_LOGGER.warning('%s does not have action: %s', self.name, anm)
elif self._message_router:
act = self._message_router.get_action('post')
if act and execute:
return self.call_action(act, [text], **kwargs)
else:
_LOGGER.error('%s have no service: %s', self.name, 'intelligent_speaker/message_router')
_LOGGER.error('%s does not have service: %s', self.name, 'intelligent_speaker/message_router')
return False

async def async_intelligent_speaker(self, text, execute=False, silent=False, **kwargs):
return await self.hass.async_add_executor_job(
partial(self.intelligent_speaker, text, execute, silent, **kwargs)
)

def xiaoai_weakup(self, text=None, **kwargs):
if srv := self._intelligent_speaker:
if act := srv.get_action('wake_up'):
pms = [text or ''] if act.ins else []
return self.miot_action(srv.iid, act.iid, pms, **kwargs)
else:
_LOGGER.warning('%s does not have action: %s', self.name, 'wake_up')
else:
_LOGGER.error('%s does not have service: %s', self.name, 'intelligent_speaker')
return False

async def async_xiaoai_weakup(self, text=None, **kwargs):
return await self.hass.async_add_executor_job(
partial(self.xiaoai_weakup, text, **kwargs)
)



class MitvMediaPlayerEntity(MiotMediaPlayerEntity):
def __init__(self, config: dict, miot_service: MiotService):
Expand Down
25 changes: 24 additions & 1 deletion custom_components/xiaomi_miot/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ intelligent_speaker:
domain: media_player
text:
description: Text content.
example: "Turn on light"
example: Turn on light
required: true
selector:
text:
Expand All @@ -184,6 +184,29 @@ intelligent_speaker:
selector:
boolean:

xiaoai_weakup:
description: Weak up the xiaoai speaker.
fields:
entity_id:
description: ID of the xiaoai entity.
example: media_player.xiaoai_x08c
required: true
selector:
entity:
integration: xiaomi_miot
domain: media_player
text:
description: Text content for weak up.
example: Livingroom light
selector:
text:
throw:
description: Throw result.
default: false
example: false
selector:
boolean:

get_device_data:
description: Get xiaomi device data from cloud.
fields:
Expand Down

1 comment on commit 018b189

@al-one
Copy link
Owner Author

@al-one al-one commented on 018b189 Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# scripts.yaml
do_you_want_to_turn_on_the_light:
  alias: 让小爱询问我是否开灯
  mode: single
  sequence:
    - service: xiaomi_miot.intelligent_speaker
      data:
        entity_id: media_player.xiaomi_lx04_xxxx_play_control # 你的小爱音箱实体ID
        text: 欢迎回家,需要打开客厅灯吗?
        execute: false
    - delay:
        seconds: 3
    - service: xiaomi_miot.xiaoai_wakeup
      data:
        entity_id: media_player.xiaomi_lx04_xxxx_play_control
    - delay:
        milliseconds: 50
    - service: xiaomi_miot.intelligent_speaker
      data:
        entity_id: media_player.xiaomi_lx04_xxxx_play_control
        text: 客厅灯
        execute: true
        silent: true

创建并执行该脚本,现在,你只需要回答小爱:打开

Typos fixed in: 3b34bd0

Please sign in to comment.