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

Two bugfix and one minor improvement on the VIAGGIATRENO integration #130377

Draft
wants to merge 5 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
2 changes: 2 additions & 0 deletions homeassistant/components/viaggiatreno/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"domain": "viaggiatreno",
"name": "Trenitalia ViaggiaTreno",
"codeowners": [],
"dependencies": ["http"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you specify the HTTP integration as a dependency?

Copy link
Author

@iaxexo iaxexo Nov 12, 2024

Choose a reason for hiding this comment

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

Please make sure CI is passing

I see, I'll have to build up the whole development environment locally :S

Why did you specify the HTTP integration as a dependency?

beacuse I am ignorant ad i saw from http import HTTPStatus at the beginning of the sensor.py file. i tried to do the update manifest task to the best of my knowledge

"documentation": "https://www.home-assistant.io/integrations/viaggiatreno",
"integration_type": "service",
"iot_class": "cloud_polling"
}
59 changes: 25 additions & 34 deletions homeassistant/components/viaggiatreno/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
return {"error": req.status}
json_response = await req.json()
except (TimeoutError, aiohttp.ClientError) as exc:
_LOGGER.error("Cannot connect to ViaggiaTreno API endpoint: %s", exc)
_LOGGER.error("Cannot connect to ViaggiaTreno API endpoint: %s", uri)
return None
except ValueError:
_LOGGER.error("Received non-JSON data from ViaggiaTreno API endpoint")
Expand All @@ -105,14 +105,11 @@
"""Initialize the sensor."""
self._state = None
self._attributes = {}
self._unit = ""
self._icon = ICON
self._train_id = train_id
self._station_id = station_id
self._name = name

self.uri = VIAGGIATRENO_ENDPOINT.format(
station_id=station_id, train_id=train_id, timestamp=int(time.time()) * 1000
)

@property
def name(self):
Expand All @@ -129,11 +126,6 @@
"""Icon to use in the frontend, if any."""
return self._icon

@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit

@property
def extra_state_attributes(self):
"""Return extra attributes."""
Expand Down Expand Up @@ -167,30 +159,29 @@

async def async_update(self) -> None:
"""Update state."""
uri = self.uri
uri = VIAGGIATRENO_ENDPOINT.format(
station_id=self._station_id, train_id=self._train_id, timestamp=int(time.time()) * 1000
)
res = await async_http_request(self.hass, uri)

Check failure on line 165 in homeassistant/components/viaggiatreno/sensor.py

View workflow job for this annotation

GitHub Actions / Check ruff

Ruff (E711)

homeassistant/components/viaggiatreno/sensor.py:165:20: E711 Comparison to `None` should be `cond is not None`
if res.get("error", ""):
if res["error"] == 204:
self._state = NO_INFORMATION_STRING
self._unit = ""
if (res != None):
if res.get("error", ""):
if res["error"] == 204:
self._state = NO_INFORMATION_STRING
else:
self._state = f"Error: {res['error']}"
else:
self._state = f"Error: {res['error']}"
self._unit = ""
for i in MONITORED_INFO:
self._attributes[i] = res[i]

if self.is_cancelled(res):
self._state = CANCELLED_STRING
self._icon = "mdi:cancel"
elif not self.has_departed(res):
self._state = NOT_DEPARTED_STRING
elif self.has_arrived(res):
self._state = ARRIVED_STRING
else:
self._state = res.get("ritardo")
self._icon = ICON
else:
for i in MONITORED_INFO:
self._attributes[i] = res[i]

if self.is_cancelled(res):
self._state = CANCELLED_STRING
self._icon = "mdi:cancel"
self._unit = ""
elif not self.has_departed(res):
self._state = NOT_DEPARTED_STRING
self._unit = ""
elif self.has_arrived(res):
self._state = ARRIVED_STRING
self._unit = ""
else:
self._state = res.get("ritardo")
self._unit = UnitOfTime.MINUTES
self._icon = ICON
self._state = f"Error"
Loading