Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking Change: Add friendly BT name next to the mac address if we have it #2697

Merged
merged 2 commits into from Jul 22, 2022

Conversation

dshokouhi
Copy link
Member

Summary

Fixes: #2696

This is a breaking change because the friendly name will now be added to the Mac address if we have it, otherwise it will print the address again.

New format will be: MAC (Name)

Also cleaned up some warnings and removed a commented line

Screenshots

image

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

Any other notes

Copy link
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

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

Works correctly, and users can make it a non-breaking change by splitting the sensor attribute strings on spaces (" ") and using the first value

@JBassett JBassett merged commit d072b90 into home-assistant:master Jul 22, 2022
@Arquiteto
Copy link

Works correctly, and users can make it a non-breaking change by splitting the sensor attribute strings on spaces (" ") and using the first value

Can you elaborate on that? I'll give you an example of what was working for me earlier:

I had a sensor that calculated distance traveled by my cars, based on me and my wifes phone connecting to its BT beginning like this:

            {% set bt_mac =  '12:34:56:78:9A:BC'  %}                                    
            {% set bt_connection_sensor_1 = 'sensor.my_phone_bluetooth_connection' %}   
            {% set phone_owner_id_1 = "My phone" %}                                         
            {% set bt_connection_sensor_2 = 'sensor.wifes_phone_bluetooth_connection' %}     
            {% set phone_owner_id_2 = "Wife's phone" %}                                         
            {% set distance_sensor_id = 'sensor.car_traveled distance' %}               


            {# Check which phone changed location and is connected to cars BT #}
            {% if ((bt_mac in state_attr(bt_connection_sensor_1, 'connected_paired_devices')) and (trigger.id == phone_owner_id_1)) 
               or ((bt_mac in state_attr(bt_connection_sensor_2, 'connected_paired_devices')) and not (bt_mac in state_attr(bt_connection_sensor_1, 'connected_paired_devices')) and (trigger.id == phone_owner_id_2))  %}

Now, for my phone the friendly name is available, but it's not for my wife, so I can't just change the "bt_mac" variable to include friendly name. Connected paired devices attribute returns a list, so I can't just use .split(" ") on it (at least it's not working for me in template dev tools). And when in my car I always have more then 1 device connected, as I use a smartwatch.

So what I'm really looking for is to find if there is a device with mac, for example '12:34:56:78:9A:BC' in a list looking like ['12:34:56:78:9A:BC (CAR RADIO)', 'DE:F1:23:45:67:89 (SMARTWACH (MODEL))']

The only way I currently came up with is (and that's for just 1 phone)

{% set bt_mac = "12:34:56:78:9A:BC" %}
{% set myphone_bt_devices = state_attr('sensor.my_phone_bluetooth_connection', 'connected_paired_devices') %}

{% set myphone_in_car = namespace(found=false) %}
{% for bt_device in myphone_bt_devices  %}
  {% if bt_mac in bt_device.split(" ") %}
    {% set myphone_in_car.found = true %}
  {% endif %}
{% endfor %}

{% if myphone_in_car.found %}

Which is waaay more complicated than what was working before:


{% set bt_mac = "12:34:56:78:9A:BC" %}
{% set bt_connection_sensor_1 = 'sensor.my_phone_bluetooth_connection' %}

{% if bt_mac in state_attr(bt_connection_sensor_1, 'connected_paired_devices') %}

Do you have a simplier way of checking, did I miss something?

And I also have some automations based on phone connecting with car, so I have to rewrite those too...

@Arquiteto
Copy link

Arquiteto commented Aug 11, 2022

Okay, I managed to find a simplier fix with the help of my FB group. It just requires converting the list to a string. I leave it here for someone who might encounter simillar problem

            {% set bt_mac =  '12:34:56:78:9A:BC'  %}                                    
            {% set bt_connection_sensor_1 = 'sensor.my_phone_bluetooth_connection' %}   
     
            {# Check which phone changed location and is connected to cars BT #}
            {% if bt_mac in state_attr(bt_connection_sensor_1, 'connected_paired_devices')  | string  %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sensor: "bluetooth_connection" attributes - Capture Bluetooth friendly device names
5 participants