MotionBlinds use device_name helper (#72438)

* use device_name helper

* fix typo

* fix import

* fix isort

* add gateway_test

* implement gateway test

* correct test blind mac
pull/72986/head
starkillerOG 2022-06-03 15:33:43 +02:00 committed by GitHub
parent beab6e2e5f
commit 6cadd4f665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 19 deletions

View File

@ -35,6 +35,7 @@ from .const import (
SERVICE_SET_ABSOLUTE_POSITION,
UPDATE_INTERVAL_MOVING,
)
from .gateway import device_name
_LOGGER = logging.getLogger(__name__)
@ -193,13 +194,12 @@ class MotionPositionDevice(CoordinatorEntity, CoverEntity):
if blind.device_type in DEVICE_TYPES_WIFI:
via_device = ()
connections = {(dr.CONNECTION_NETWORK_MAC, blind.mac)}
name = blind.blind_type
else:
via_device = (DOMAIN, blind._gateway.mac)
connections = {}
name = f"{blind.blind_type} {blind.mac[12:]}"
sw_version = None
name = device_name(blind)
self._attr_device_class = device_class
self._attr_name = name
self._attr_unique_id = blind.mac
@ -422,7 +422,7 @@ class MotionTDBUDevice(MotionPositionDevice):
super().__init__(coordinator, blind, device_class, sw_version)
self._motor = motor
self._motor_key = motor[0]
self._attr_name = f"{blind.blind_type} {blind.mac[12:]} {motor}"
self._attr_name = f"{device_name(blind)} {motor}"
self._attr_unique_id = f"{blind.mac}-{motor}"
if self._motor not in ["Bottom", "Top", "Combined"]:

View File

@ -3,7 +3,7 @@ import contextlib
import logging
import socket
from motionblinds import AsyncMotionMulticast, MotionGateway
from motionblinds import DEVICE_TYPES_WIFI, AsyncMotionMulticast, MotionGateway
from homeassistant.components import network
@ -12,6 +12,13 @@ from .const import DEFAULT_INTERFACE
_LOGGER = logging.getLogger(__name__)
def device_name(blind):
"""Construct common name part of a device."""
if blind.device_type in DEVICE_TYPES_WIFI:
return blind.blind_type
return f"{blind.blind_type} {blind.mac[12:]}"
class ConnectMotionGateway:
"""Class to async connect to a Motion Gateway."""

View File

@ -10,6 +10,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ATTR_AVAILABLE, DOMAIN, KEY_COORDINATOR, KEY_GATEWAY
from .gateway import device_name
ATTR_BATTERY_VOLTAGE = "battery_voltage"
TYPE_BLIND = "blind"
@ -54,14 +55,9 @@ class MotionBatterySensor(CoordinatorEntity, SensorEntity):
"""Initialize the Motion Battery Sensor."""
super().__init__(coordinator)
if blind.device_type in DEVICE_TYPES_WIFI:
name = f"{blind.blind_type} battery"
else:
name = f"{blind.blind_type} {blind.mac[12:]} battery"
self._blind = blind
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, blind.mac)})
self._attr_name = name
self._attr_name = f"{device_name(blind)} battery"
self._attr_unique_id = f"{blind.mac}-battery"
@property
@ -103,14 +99,9 @@ class MotionTDBUBatterySensor(MotionBatterySensor):
"""Initialize the Motion Battery Sensor."""
super().__init__(coordinator, blind)
if blind.device_type in DEVICE_TYPES_WIFI:
name = f"{blind.blind_type} {motor} battery"
else:
name = f"{blind.blind_type} {blind.mac[12:]} {motor} battery"
self._motor = motor
self._attr_unique_id = f"{blind.mac}-{motor}-battery"
self._attr_name = name
self._attr_name = f"{device_name(blind)} {motor} battery"
@property
def native_value(self):
@ -144,10 +135,8 @@ class MotionSignalStrengthSensor(CoordinatorEntity, SensorEntity):
if device_type == TYPE_GATEWAY:
name = "Motion gateway signal strength"
elif device.device_type in DEVICE_TYPES_WIFI:
name = f"{device.blind_type} signal strength"
else:
name = f"{device.blind_type} {device.mac[12:]} signal strength"
name = f"{device_name(device)} signal strength"
self._device = device
self._device_type = device_type

View File

@ -0,0 +1,19 @@
"""Test the Motion Blinds config flow."""
from unittest.mock import Mock
from motionblinds import DEVICE_TYPES_WIFI, BlindType
from homeassistant.components.motion_blinds.gateway import device_name
TEST_BLIND_MAC = "abcdefghujkl0001"
async def test_device_name(hass):
"""test_device_name."""
blind = Mock()
blind.blind_type = BlindType.RollerBlind.name
blind.mac = TEST_BLIND_MAC
assert device_name(blind) == "RollerBlind 0001"
blind.device_type = DEVICE_TYPES_WIFI[0]
assert device_name(blind) == "RollerBlind"