Problem

During the repair of a new apartment, I returned to the apartment where I had not lived for 10 years and decided to practice the discipline “Smart Home” in order to equip a new apartment with knowledge of the matter. The equipment in this apartment is also not the smartest and not the newest, because it was produced ~ 10 years ago. Most of all in the apartment I was bothered by the abundance of infrared remotes, they were lost and loaded the brain with the selection of the right one. Until recently, it was decided to make a huge number of unused buttons on the remote controls. And the consoles are neither aesthetic nor hygienic, some are angular, others with rounded edges, the buttons turn yellow, and the remnants of the inhabitants' life accumulate on them, which form a “sub-button biocenosis”. As a result, a dozen remotes are scattered around the house with a bunch of unnecessary buttons, for simple operations I had to look for two or three pieces, their look scratches my eyes,I wanted a comfortable tactile and fashionable voice control.
, . . xiaomi Xiaomi universal IR remotecontroller Xiaomi Mi Smart Home Magic Cube, Xiaomi smart home gateway 2

, , IR , — ZigBee . , , .
, , xiaomi, , Mi Home “”. , , . , . , , ( ) Mi Home 10 5 , 30 10 , , , 1 , . , ...
The trouble from China does not come alone, except for the brakes to rub has become the inability to screw a smart cube to the sound control.
The cube is controlled by gestures:- To shake
- Move on a plane
- Rotate 90/180 degrees around the horizontal axis
- Rotate around a vertical axis
- And etc...

— , , , , , 90 — 10 , 45 — 5 . , — ZigBee, - , 3 .
Home Assistant, , , Xiaomi , ZigBee . Security token IR , Mi Home . xiaomi gateway , UART RX/TX Home Assistant.
The Home Assistant itself needs to be launched somewhere, Synology DS718 + came up under this task, which was used as a DLNA media center and torrent download. This device is able to run Docker containers, and a container with Home Assistant is easily wound up in it. Home Assistant is cool, but unlike Mi Home, you need to write all the smartness of a house in it with pens in yaml. Having mastered the DSL understood Home Assistant and spitting from another programming tool that does not require knowledge of the PL, I managed to cook up a crutch of the desired shape.
Automation Code:- id: volume_up
alias: Volume up
trigger:
- event_data:
action_type: rotate
entity_id: binary_sensor.cube_0000000103ec74
event_type: xiaomi_aqara.cube_action
platform: event
condition:
condition: template
value_template: "{{trigger.event.data.action_value> 0}}"
action:
- service_template: "script.bbk_vol_up _ {{(trigger.event.data.action_value / 3) | int}}"
This automation catches the clockwise rotation event and calls a script in whose name the number of sound increase signals that the infrared emitter should send is substituted. As you probably guessed, each corner of the rotation requires its own script, copy-paste was called to create them, and it gave me 20 scripts like these:
bbk_vol_up_10:
sequence:
- data:
command:
- bbk_vol_up
delay_secs: 0.04
num_repeats: 10
entity_id: remote.xiaomi_miio_192_168_2_62
service: remote.send_command
The configuration of the emitter with intercepted signals from the speaker panel looks like this:remote:
- platform: xiaomi_miio
host: 192.168.2.62
token: 392cbed325e862ffff983c4575b7a6f2
commands:
bbk_power:
command: raw: nE5m0wlk0msxmEsmszmEsm0wAHKaABkAoQAcAL1MZtOQEFmM3ARMCHwCymM2m4ENgXEAioHlgy + AwoL3gvYAh4L0gGGEBYLCAL0BR4CHhU4A + wQ8TC
bbk_vol_up:
command: raw: nMwmUwlk0mk1mEsms3ADKbABlM5hLJtMAD2ALKazkAdgAymM3AYMBApjNgKTmgGLABuBD4IaAEKA1YJrgyuBDwJvgZeEx4C / geHMZsA4YCDJxoAxgxojxgco
bbk_vol_down:
command: raw: nE5m0wlk0msxmEsmszmEsm0wAHKaABkAoQAcAL1MZtOQEFmM3ARMCHwIfAdQBFZuAQYFfAfuBCoHngveEx4C / gL + Cj4R2ANfLJqAYQUChU3MAA
Hooray! The sound controls the rotation of the cube around the vertical axis, and the TV and speakers are turned on by turning the cube 90 degrees around the horizontal axis.Home Assistant , , Google Assistant Home Assistant, . GA , . , . Ethernet , , IP , IP — , . custom_component Home Assistant:
import logging
from homeassistant.helpers.entity import Entity
from pythonping import ping
logger = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
entities = []
for name, ip in config['hosts'].items():
entities.append(IPSensor(name, ip))
add_entities(entities)
class IPSensor(Entity):
def __init__(self, name, ip):
self._name = name
self._ip = ip
self._state = None
self._is_on = None
@property
def name(self):
return self._name
@property
def state(self):
return self._state
@property
def state_attributes(self):
return {'ip_address': self._ip}
@property
def device_class(self):
return 'connectivity'
@property
def is_on(self):
return self._is_on
def update(self):
result = ping(self._ip, count=1, timeout=0.1)
self._is_on = result.success()
self._state = 'on' if result.success() else ‘off'
Then connect the infrared emitter with the sensor to the switch like this:switch:
- platform: template
switches:
toshiba_tv:
value_template: "{{is_state ('binary_sensor.toshiba_tv', 'on')}}"
turn_on:
service: script.toshiba_power
turn_off:
service: script.toshiba_power
And such a switch began to be displayed in Google Home and controlled from voice.PS Exercise with a smart home was successful, went to buy new devices!PPS A ZigBee hub now for some reason illuminates the hallway ...It was

Has become
