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

Show new errors from the MotionMount #137006

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions homeassistant/components/motionmount/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Support for MotionMount sensors."""

import motionmount
from motionmount import MotionMountSystemError

from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.core import HomeAssistant
Expand All @@ -9,6 +10,14 @@
from . import MotionMountConfigEntry
from .entity import MotionMountEntity

error_messages = {
Copy link
Member

Choose a reason for hiding this comment

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

Module-level consts should be all-caps (and optionally denoted as Final):

Suggested change
error_messages = {
from typing import Final
ERROR_MESSAGES: Final = {

MotionMountSystemError.MotorError: "motor",
MotionMountSystemError.ObstructionDetected: "obstruction",
MotionMountSystemError.TVWidthConstraintError: "tv_width_constraint",
MotionMountSystemError.HDMICECError: "hdmi_cec",
MotionMountSystemError.InternalError: "internal",
}


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -25,7 +34,14 @@ class MotionMountErrorStatusSensor(MotionMountEntity, SensorEntity):
"""The error status sensor of a MotionMount."""

_attr_device_class = SensorDeviceClass.ENUM
_attr_options = ["none", "motor", "internal"]
_attr_options = [
"none",
"motor",
"hdmi_cec",
"obstruction",
"tv_width_constraint",
"internal",
]
_attr_translation_key = "motionmount_error_status"

def __init__(
Expand All @@ -38,13 +54,10 @@ def __init__(
@property
def native_value(self) -> str:
"""Return error status."""
errors = self.mm.error_status or 0

if errors & (1 << 31):
# Only when but 31 is set are there any errors active at this moment
if errors & (1 << 10):
return "motor"
status = self.mm.system_status

return "internal"
for error, message in error_messages.items():
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for error, message in error_messages.items():
for error, message in ERROR_MESSAGES.items():

if error in status:
return message

return "none"
3 changes: 3 additions & 0 deletions homeassistant/components/motionmount/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
"state": {
"none": "None",
"motor": "Motor",
"hdmi_cec": "HDMI CEC",
"obstruction": "Obstruction",
"tv_width_constraint": "TV width constraint",
"internal": "Internal"
}
}
Expand Down
49 changes: 49 additions & 0 deletions tests/components/motionmount/test_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Tests for the MotionMount Sensor platform."""

from unittest.mock import patch

from motionmount import MotionMountSystemError
import pytest

from homeassistant.core import HomeAssistant

from . import ZEROCONF_NAME

from tests.common import MockConfigEntry

MAC = bytes.fromhex("c4dd57f8a55f")


@pytest.mark.parametrize(
"system_status",
Copy link
Member

Choose a reason for hiding this comment

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

Parameterize supports multiple arguments and will split the tuple into the args for you automatically:

Suggested change
"system_status",
("system", "status"),

[
(None, "none"),
(MotionMountSystemError.MotorError, "motor"),
(MotionMountSystemError.ObstructionDetected, "obstruction"),
(MotionMountSystemError.TVWidthConstraintError, "tv_width_constraint"),
(MotionMountSystemError.HDMICECError, "hdmi_cec"),
(MotionMountSystemError.InternalError, "internal"),
],
)
async def test_error_status_sensor_states(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
system_status: (MotionMountSystemError, str),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
system_status: (MotionMountSystemError, str),
status: MotionMountSystemError,
state: str,

) -> None:
"""Tests the state attributes."""
(status, state) = system_status
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(status, state) = system_status


with patch(
"homeassistant.components.motionmount.motionmount.MotionMount",
autospec=True,
) as motionmount_mock:
motionmount_mock.return_value.name = ZEROCONF_NAME
motionmount_mock.return_value.mac = MAC
motionmount_mock.return_value.is_authenticated = True
motionmount_mock.return_value.system_status = [status]

mock_config_entry.add_to_hass(hass)

assert await hass.config_entries.async_setup(mock_config_entry.entry_id)

assert hass.states.get("sensor.my_motionmount_error_status").state == state