Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion homeassistant/components/broadlink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
sensors = [
BroadlinkSensor(device, monitored_condition)
for monitored_condition in sensor_data
if sensor_data[monitored_condition] or device.api.type == "A1"
if sensor_data[monitored_condition] != 0 or device.api.type == "A1"
]
async_add_entities(sensors)

Expand Down
18 changes: 16 additions & 2 deletions homeassistant/components/broadlink/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def async_update(self):
try:
data = await self.async_fetch_data()

except (BroadlinkException, OSError) as err:
except (BroadlinkException, ValueError, OSError) as err:
if self.available and (
dt.utcnow() - self.last_update > self.SCAN_INTERVAL * 3
or isinstance(err, (AuthorizationError, OSError))
Expand Down Expand Up @@ -117,11 +117,25 @@ async def async_fetch_data(self):
device = self.device

if hasattr(device.api, "check_sensors"):
return await device.async_request(device.api.check_sensors)
data = await device.async_request(device.api.check_sensors)
return self.normalize(data, self.coordinator.data)

await device.async_request(device.api.update)
return {}

@staticmethod
def normalize(data, previous_data):
"""Fix firmware issue.

See https://github.com/home-assistant/core/issues/42100.
"""
if data["temperature"] == -7:
if previous_data is None or previous_data["temperature"] is None:
data["temperature"] = None
elif previous_data["temperature"] - data["temperature"] > 3:
data["temperature"] = previous_data["temperature"]
return data


class BroadlinkSP1UpdateManager(BroadlinkUpdateManager):
"""Manages updates for Broadlink SP1 devices."""
Expand Down
5 changes: 4 additions & 1 deletion tests/components/broadlink/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ async def test_device_setup_update_authorization_error(hass):
"""Test we handle an authorization error in the update step."""
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.check_sensors.side_effect = (blke.AuthorizationError(), None)
mock_api.check_sensors.side_effect = (
Comment thread
felipediel marked this conversation as resolved.
blke.AuthorizationError(),
{"temperature": 30},
)

with patch.object(
hass.config_entries, "async_forward_entry_setup"
Expand Down
32 changes: 32 additions & 0 deletions tests/components/broadlink/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ async def test_rm_pro_sensor_update(hass):
assert sensors_and_states == {(f"{device.name} Temperature", "25.8")}


async def test_rm_pro_filter_crazy_temperature(hass):
"""Test we filter a crazy temperature variation.

Firmware issue. See https://github.com/home-assistant/core/issues/42100.
"""
device = get_device("Office")
mock_api = device.get_mock_api()
mock_api.check_sensors.return_value = {"temperature": 22.9}

device_registry = mock_device_registry(hass)
entity_registry = mock_registry(hass)

mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)

device_entry = device_registry.async_get_device({(DOMAIN, mock_entry.unique_id)})
entries = async_entries_for_device(entity_registry, device_entry.id)
sensors = {entry for entry in entries if entry.domain == SENSOR_DOMAIN}
assert len(sensors) == 1

mock_api.check_sensors.return_value = {"temperature": -7}
await hass.helpers.entity_component.async_update_entity(
next(iter(sensors)).entity_id
)
assert mock_api.check_sensors.call_count == 2

sensors_and_states = {
(sensor.original_name, hass.states.get(sensor.entity_id).state)
for sensor in sensors
}
assert sensors_and_states == {(f"{device.name} Temperature", "22.9")}


async def test_rm_mini3_no_sensor(hass):
"""Test we do not set up sensors for RM mini 3."""
device = get_device("Entrance")
Expand Down