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

LaCrosse View new endpoint #137284

Draft
wants to merge 2 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
31 changes: 21 additions & 10 deletions homeassistant/components/lacrosse_view/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class LaCrosseUpdateCoordinator(DataUpdateCoordinator[list[Sensor]]):
name: str
id: str
hass: HomeAssistant
devices: list[Sensor] | None = None

def __init__(
self,
Expand Down Expand Up @@ -60,24 +61,34 @@ async def _async_update_data(self) -> list[Sensor]:
except LoginError as error:
raise ConfigEntryAuthFailed from error

if self.devices is None:
_LOGGER.debug("Getting devices")
try:
self.devices = await self.api.get_devices(
location=Location(id=self.id, name=self.name),
)
except HTTPError as error:
raise ConfigEntryNotReady from error

try:
# Fetch last hour of data
sensors = await self.api.get_sensors(
location=Location(id=self.id, name=self.name),
tz=self.hass.config.time_zone,
start=str(now - 3600),
end=str(now),
)
for sensor in self.devices:
sensor.data = (
await self.api.get_sensor_status(
sensor=sensor,
tz=self.hass.config.time_zone,
)
)["data"]["current"]
_LOGGER.debug("Got data: %s", sensor.data)

except HTTPError as error:
raise ConfigEntryNotReady from error

_LOGGER.debug("Got data: %s", sensors)

# Verify that we have permission to read the sensors
for sensor in sensors:
for sensor in self.devices:
if not sensor.permissions.get("read", False):
raise ConfigEntryAuthFailed(
f"This account does not have permission to read {sensor.name}"
)

return sensors
return self.devices
2 changes: 1 addition & 1 deletion homeassistant/components/lacrosse_view/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"documentation": "https://www.home-assistant.io/integrations/lacrosse_view",
"iot_class": "cloud_polling",
"loggers": ["lacrosse_view"],
"requirements": ["lacrosse-view==1.0.4"]
"requirements": ["lacrosse-view==1.1.1"]
}
11 changes: 9 additions & 2 deletions homeassistant/components/lacrosse_view/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@

def get_value(sensor: Sensor, field: str) -> float | int | str | None:
"""Get the value of a sensor field."""
if sensor.data is None:
return None

Check warning on line 49 in homeassistant/components/lacrosse_view/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/lacrosse_view/sensor.py#L49

Added line #L49 was not covered by tests
field_data = sensor.data.get(field)
if field_data is None:
return None
value = field_data["values"][-1]["s"]
value = field_data["spot"]["value"]
try:
value = float(value)
except ValueError:
Expand Down Expand Up @@ -177,6 +179,9 @@
_LOGGER.warning(message)
continue

if sensor.data is None:
continue

Check warning on line 183 in homeassistant/components/lacrosse_view/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/lacrosse_view/sensor.py#L183

Added line #L183 was not covered by tests

# if the API returns a different unit of measurement from the description, update it
if sensor.data.get(field) is not None:
native_unit_of_measurement = UNIT_OF_MEASUREMENT_MAP.get(
Expand Down Expand Up @@ -240,7 +245,9 @@
@property
def available(self) -> bool:
"""Return True if entity is available."""
data = self.coordinator.data[self.index].data
return (
super().available
and self.entity_description.key in self.coordinator.data[self.index].data
and data is not None
and self.entity_description.key in data
)
2 changes: 1 addition & 1 deletion requirements_all.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion requirements_test_all.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 57 additions & 10 deletions tests/components/lacrosse_view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
sensor_id="2",
sensor_field_names=["Temperature"],
location=Location(id="1", name="Test"),
data={"Temperature": {"values": [{"s": "2"}], "unit": "degrees_celsius"}},
data={
"data": {
"current": {
"Temperature": {"spot": {"value": "2"}, "unit": "degrees_celsius"}
}
}
},
permissions={"read": True},
model="Test",
)
Expand All @@ -26,7 +32,13 @@
sensor_id="2",
sensor_field_names=["Temperature"],
location=Location(id="1", name="Test"),
data={"Temperature": {"values": [{"s": "2"}], "unit": "degrees_celsius"}},
data={
"data": {
"current": {
"Temperature": {"spot": {"value": "2"}, "unit": "degrees_celsius"}
}
}
},
permissions={"read": False},
model="Test",
)
Expand All @@ -37,7 +49,16 @@
sensor_id="2",
sensor_field_names=["SomeUnsupportedField"],
location=Location(id="1", name="Test"),
data={"SomeUnsupportedField": {"values": [{"s": "2"}], "unit": "degrees_celsius"}},
data={
"data": {
"current": {
"SomeUnsupportedField": {
"spot": {"value": "2"},
"unit": "degrees_celsius",
}
}
}
},
permissions={"read": True},
model="Test",
)
Expand All @@ -48,7 +69,13 @@
sensor_id="2",
sensor_field_names=["Temperature"],
location=Location(id="1", name="Test"),
data={"Temperature": {"values": [{"s": "2.3"}], "unit": "degrees_celsius"}},
data={
"data": {
"current": {
"Temperature": {"spot": {"value": "2.3"}, "unit": "degrees_celsius"}
}
}
},
permissions={"read": True},
model="Test",
)
Expand All @@ -59,7 +86,9 @@
sensor_id="2",
sensor_field_names=["WetDry"],
location=Location(id="1", name="Test"),
data={"WetDry": {"values": [{"s": "dry"}], "unit": "wet_dry"}},
data={
"data": {"current": {"WetDry": {"spot": {"value": "dry"}, "unit": "wet_dry"}}}
},
permissions={"read": True},
model="Test",
)
Expand All @@ -70,7 +99,13 @@
sensor_id="2",
sensor_field_names=["HeatIndex"],
location=Location(id="1", name="Test"),
data={"HeatIndex": {"values": [{"s": 2.3}], "unit": "degrees_fahrenheit"}},
data={
"data": {
"current": {
"HeatIndex": {"spot": {"value": 2.3}, "unit": "degrees_fahrenheit"}
}
}
},
permissions={"read": True},
model="Test",
)
Expand All @@ -81,7 +116,13 @@
sensor_id="2",
sensor_field_names=["WindSpeed"],
location=Location(id="1", name="Test"),
data={"WindSpeed": {"values": [{"s": 2}], "unit": "kilometers_per_hour"}},
data={
"data": {
"current": {
"WindSpeed": {"spot": {"value": 2}, "unit": "kilometers_per_hour"}
}
}
},
permissions={"read": True},
model="Test",
)
Expand All @@ -92,7 +133,7 @@
sensor_id="2",
sensor_field_names=["Temperature"],
location=Location(id="1", name="Test"),
data={},
data={"data": {"current": {}}},
permissions={"read": True},
model="Test",
)
Expand All @@ -103,7 +144,7 @@
sensor_id="2",
sensor_field_names=["Temperature"],
location=Location(id="1", name="Test"),
data={"Temperature": None},
data={"data": {"current": {"Temperature": None}}},
permissions={"read": True},
model="Test",
)
Expand All @@ -114,7 +155,13 @@
sensor_id="2",
sensor_field_names=["Temperature"],
location=Location(id="1", name="Test"),
data={"Temperature": {"values": [{"s": "2.1"}], "unit": "degrees_fahrenheit"}},
data={
"data": {
"current": {
"Temperature": {"spot": {"value": "2.1"}, "unit": "degrees_fahrenheit"}
}
}
},
permissions={"read": True},
model="Test",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'coordinator_data': list([
dict({
'__type': "<class 'lacrosse_view.Sensor'>",
'repr': "Sensor(name='Test', device_id='1', type='Test', sensor_id='2', sensor_field_names=['Temperature'], location=Location(id='1', name='Test'), permissions={'read': True}, model='Test', data={'Temperature': {'values': [{'s': '2'}], 'unit': 'degrees_celsius'}})",
'repr': "Sensor(name='Test', device_id='1', type='Test', sensor_id='2', sensor_field_names=['Temperature'], location=Location(id='1', name='Test'), permissions={'read': True}, model='Test', data={'Temperature': {'spot': {'value': '2'}, 'unit': 'degrees_celsius'}})",
}),
]),
'entry': dict({
Expand Down
7 changes: 6 additions & 1 deletion tests/components/lacrosse_view/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ async def test_entry_diagnostics(
)
config_entry.add_to_hass(hass)

sensor = TEST_SENSOR.model_copy()
status = sensor.data
sensor.data = None

with (
patch("lacrosse_view.LaCrosse.login", return_value=True),
patch("lacrosse_view.LaCrosse.get_sensors", return_value=[TEST_SENSOR]),
patch("lacrosse_view.LaCrosse.get_devices", return_value=[sensor]),
patch("lacrosse_view.LaCrosse.get_sensor_status", return_value=status),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
Expand Down
31 changes: 23 additions & 8 deletions tests/components/lacrosse_view/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_ENTRY_DATA)
config_entry.add_to_hass(hass)

sensor = TEST_SENSOR.model_copy()
status = sensor.data
sensor.data = None

with (
patch("lacrosse_view.LaCrosse.login", return_value=True),
patch(
"lacrosse_view.LaCrosse.get_sensors",
return_value=[TEST_SENSOR],
"lacrosse_view.LaCrosse.get_devices",
return_value=[sensor],
),
patch("lacrosse_view.LaCrosse.get_sensor_status", return_value=status),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
Expand Down Expand Up @@ -68,7 +73,7 @@ async def test_http_error(hass: HomeAssistant) -> None:

with (
patch("lacrosse_view.LaCrosse.login", return_value=True),
patch("lacrosse_view.LaCrosse.get_sensors", side_effect=HTTPError),
patch("lacrosse_view.LaCrosse.get_devices", side_effect=HTTPError),
):
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
Expand All @@ -84,12 +89,17 @@ async def test_new_token(hass: HomeAssistant, freezer: FrozenDateTimeFactory) ->
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_ENTRY_DATA)
config_entry.add_to_hass(hass)

sensor = TEST_SENSOR.model_copy()
status = sensor.data
sensor.data = None

with (
patch("lacrosse_view.LaCrosse.login", return_value=True) as login,
patch(
"lacrosse_view.LaCrosse.get_sensors",
return_value=[TEST_SENSOR],
"lacrosse_view.LaCrosse.get_devices",
return_value=[sensor],
),
patch("lacrosse_view.LaCrosse.get_sensor_status", return_value=status),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
Expand All @@ -103,7 +113,7 @@ async def test_new_token(hass: HomeAssistant, freezer: FrozenDateTimeFactory) ->
with (
patch("lacrosse_view.LaCrosse.login", return_value=True) as login,
patch(
"lacrosse_view.LaCrosse.get_sensors",
"lacrosse_view.LaCrosse.get_devices",
return_value=[TEST_SENSOR],
),
):
Expand All @@ -121,12 +131,17 @@ async def test_failed_token(
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_ENTRY_DATA)
config_entry.add_to_hass(hass)

sensor = TEST_SENSOR.model_copy()
status = sensor.data
sensor.data = None

with (
patch("lacrosse_view.LaCrosse.login", return_value=True) as login,
patch(
"lacrosse_view.LaCrosse.get_sensors",
return_value=[TEST_SENSOR],
"lacrosse_view.LaCrosse.get_devices",
return_value=[sensor],
),
patch("lacrosse_view.LaCrosse.get_sensor_status", return_value=status),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
Expand Down
Loading
Loading