mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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
This commit is contained in:
parent
beab6e2e5f
commit
6cadd4f665
@ -35,6 +35,7 @@ from .const import (
|
|||||||
SERVICE_SET_ABSOLUTE_POSITION,
|
SERVICE_SET_ABSOLUTE_POSITION,
|
||||||
UPDATE_INTERVAL_MOVING,
|
UPDATE_INTERVAL_MOVING,
|
||||||
)
|
)
|
||||||
|
from .gateway import device_name
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -193,13 +194,12 @@ class MotionPositionDevice(CoordinatorEntity, CoverEntity):
|
|||||||
if blind.device_type in DEVICE_TYPES_WIFI:
|
if blind.device_type in DEVICE_TYPES_WIFI:
|
||||||
via_device = ()
|
via_device = ()
|
||||||
connections = {(dr.CONNECTION_NETWORK_MAC, blind.mac)}
|
connections = {(dr.CONNECTION_NETWORK_MAC, blind.mac)}
|
||||||
name = blind.blind_type
|
|
||||||
else:
|
else:
|
||||||
via_device = (DOMAIN, blind._gateway.mac)
|
via_device = (DOMAIN, blind._gateway.mac)
|
||||||
connections = {}
|
connections = {}
|
||||||
name = f"{blind.blind_type} {blind.mac[12:]}"
|
|
||||||
sw_version = None
|
sw_version = None
|
||||||
|
|
||||||
|
name = device_name(blind)
|
||||||
self._attr_device_class = device_class
|
self._attr_device_class = device_class
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self._attr_unique_id = blind.mac
|
self._attr_unique_id = blind.mac
|
||||||
@ -422,7 +422,7 @@ class MotionTDBUDevice(MotionPositionDevice):
|
|||||||
super().__init__(coordinator, blind, device_class, sw_version)
|
super().__init__(coordinator, blind, device_class, sw_version)
|
||||||
self._motor = motor
|
self._motor = motor
|
||||||
self._motor_key = motor[0]
|
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}"
|
self._attr_unique_id = f"{blind.mac}-{motor}"
|
||||||
|
|
||||||
if self._motor not in ["Bottom", "Top", "Combined"]:
|
if self._motor not in ["Bottom", "Top", "Combined"]:
|
||||||
|
@ -3,7 +3,7 @@ import contextlib
|
|||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from motionblinds import AsyncMotionMulticast, MotionGateway
|
from motionblinds import DEVICE_TYPES_WIFI, AsyncMotionMulticast, MotionGateway
|
||||||
|
|
||||||
from homeassistant.components import network
|
from homeassistant.components import network
|
||||||
|
|
||||||
@ -12,6 +12,13 @@ from .const import DEFAULT_INTERFACE
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_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 ConnectMotionGateway:
|
||||||
"""Class to async connect to a Motion Gateway."""
|
"""Class to async connect to a Motion Gateway."""
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import ATTR_AVAILABLE, DOMAIN, KEY_COORDINATOR, KEY_GATEWAY
|
from .const import ATTR_AVAILABLE, DOMAIN, KEY_COORDINATOR, KEY_GATEWAY
|
||||||
|
from .gateway import device_name
|
||||||
|
|
||||||
ATTR_BATTERY_VOLTAGE = "battery_voltage"
|
ATTR_BATTERY_VOLTAGE = "battery_voltage"
|
||||||
TYPE_BLIND = "blind"
|
TYPE_BLIND = "blind"
|
||||||
@ -54,14 +55,9 @@ class MotionBatterySensor(CoordinatorEntity, SensorEntity):
|
|||||||
"""Initialize the Motion Battery Sensor."""
|
"""Initialize the Motion Battery Sensor."""
|
||||||
super().__init__(coordinator)
|
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._blind = blind
|
||||||
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, blind.mac)})
|
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"
|
self._attr_unique_id = f"{blind.mac}-battery"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -103,14 +99,9 @@ class MotionTDBUBatterySensor(MotionBatterySensor):
|
|||||||
"""Initialize the Motion Battery Sensor."""
|
"""Initialize the Motion Battery Sensor."""
|
||||||
super().__init__(coordinator, blind)
|
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._motor = motor
|
||||||
self._attr_unique_id = f"{blind.mac}-{motor}-battery"
|
self._attr_unique_id = f"{blind.mac}-{motor}-battery"
|
||||||
self._attr_name = name
|
self._attr_name = f"{device_name(blind)} {motor} battery"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
@ -144,10 +135,8 @@ class MotionSignalStrengthSensor(CoordinatorEntity, SensorEntity):
|
|||||||
|
|
||||||
if device_type == TYPE_GATEWAY:
|
if device_type == TYPE_GATEWAY:
|
||||||
name = "Motion gateway signal strength"
|
name = "Motion gateway signal strength"
|
||||||
elif device.device_type in DEVICE_TYPES_WIFI:
|
|
||||||
name = f"{device.blind_type} signal strength"
|
|
||||||
else:
|
else:
|
||||||
name = f"{device.blind_type} {device.mac[12:]} signal strength"
|
name = f"{device_name(device)} signal strength"
|
||||||
|
|
||||||
self._device = device
|
self._device = device
|
||||||
self._device_type = device_type
|
self._device_type = device_type
|
||||||
|
19
tests/components/motion_blinds/test_gateway.py
Normal file
19
tests/components/motion_blinds/test_gateway.py
Normal 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"
|
Loading…
x
Reference in New Issue
Block a user