diff --git a/homeassistant/components/motion_blinds/cover.py b/homeassistant/components/motion_blinds/cover.py index 7bac3a5fb20..23e7e3d834a 100644 --- a/homeassistant/components/motion_blinds/cover.py +++ b/homeassistant/components/motion_blinds/cover.py @@ -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"]: diff --git a/homeassistant/components/motion_blinds/gateway.py b/homeassistant/components/motion_blinds/gateway.py index 218da9f625c..96a85246666 100644 --- a/homeassistant/components/motion_blinds/gateway.py +++ b/homeassistant/components/motion_blinds/gateway.py @@ -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.""" diff --git a/homeassistant/components/motion_blinds/sensor.py b/homeassistant/components/motion_blinds/sensor.py index ebaed95bcbf..3a6f775092e 100644 --- a/homeassistant/components/motion_blinds/sensor.py +++ b/homeassistant/components/motion_blinds/sensor.py @@ -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 diff --git a/tests/components/motion_blinds/test_gateway.py b/tests/components/motion_blinds/test_gateway.py new file mode 100644 index 00000000000..66f42c3c444 --- /dev/null +++ b/tests/components/motion_blinds/test_gateway.py @@ -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"