Skip to content

Commit

Permalink
Coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
kloemi committed Dec 9, 2024
1 parent e426e32 commit ba8b627
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
5 changes: 3 additions & 2 deletions custom_components/samsvolleyball/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The sams-volleyball integration."""

from __future__ import annotations

import asyncio
Expand Down Expand Up @@ -192,5 +193,5 @@ async def game_active(self) -> bool:
return True
return False

def has_listener(self) -> bool:
return len(self._listeners) > 0
def has_listener(self) -> tuple:
return len(self._listeners) > 0, len(self._listeners)
13 changes: 7 additions & 6 deletions custom_components/samsvolleyball/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config flow for samsvolleyball integration."""

from __future__ import annotations

import logging
Expand Down Expand Up @@ -75,7 +76,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]):
raise InvalidData

leagues = SamsUtils.get_leaguelist(data)
if 0 == len(leagues):
if len(leagues) == 0:
raise InvalidData

# Return info that you want to store in the config entry.
Expand Down Expand Up @@ -148,17 +149,17 @@ async def async_step_league(
self.cfg_data[CONF_LEAGUE_NAME] = SamsUtils.get_league_data(
self.data, league_id, "name"
)
if 0 == len(self.teams):
if len(self.teams) == 0:
errors["base"] = "no_teams"
else:
self.cfg_data[CONF_LEAGUE] = user_input[CONF_LEAGUE]
return await self.async_step_team()

leagues_filter = SamsUtils.get_leaguelist(self.data, self.cfg_data[CONF_GENDER])
league_select = []
for league in leagues_filter:
league_select.append({"label": league["name"], "value": league["id"]})

league_select = [
{"label": league["name"], "value": league["id"]}
for league in leagues_filter
]
step_league_schema = vol.Schema(
{
vol.Required(CONF_LEAGUE): selector.SelectSelector(
Expand Down
20 changes: 12 additions & 8 deletions custom_components/samsvolleyball/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The sams volleyball sensor platform."""

from __future__ import annotations

import locale
Expand Down Expand Up @@ -97,8 +98,12 @@ async def async_added_to_hass(self) -> None:
except Exception: # pylint: disable=broad-except
lang, _ = locale.getlocale()
self._lang = lang or "en_US"
_, num_listener = self._coordinator.has_listener()
_LOGGER.debug(
f"Added entity {self.name} with coordinator {self._coordinator.name} listener {len(self._coordinator._listeners)}"
"Added entity %s with coordinator %s listener %d",
self.name,
self._coordinator.name,
num_listener,
)

def _update_overview(self, data):
Expand All @@ -108,7 +113,7 @@ def _update_overview(self, data):
uuid_list = SamsUtils.get_uuids_by_name(data, self._name, self._league_name)
if len(uuid_list) == 0:
_LOGGER.warning(
f"No team data found for {self._name} - {self._league_name}"
"No team data found for %s - %s", self._name, self._league_name
)
return
matches = []
Expand All @@ -130,7 +135,7 @@ def get_active_state(self):
# check if we are nearby (2 hours before / 3 hours behind)
if self._state == STATES_IN:
return IN_GAME
elif self._match and "date" in self._attr:
if self._match and "date" in self._attr:
date = self._attr["date"]
duration = (dt_util.now() - date).total_seconds()
if (-2 * 60 * 60) < duration < (3 * 60 * 60):
Expand Down Expand Up @@ -183,11 +188,10 @@ def extra_state_attributes(self) -> dict[str, Any]:
self._attr = SamsUtils.fill_match_attributes(
self._attr, self._ticker_data, self._match, self._team, self._lang
)
else:
if self._team:
self._attr = SamsUtils.fill_team_attributes(
self._attr, self._ticker_data, self._team, self._state
)
elif self._team:
self._attr = SamsUtils.fill_team_attributes(
self._attr, self._ticker_data, self._team, self._state
)
if self._match_data:
self._attr = SamsUtils.update_match_attributes(
self._attr, self._match_data
Expand Down
21 changes: 11 additions & 10 deletions custom_components/samsvolleyball/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ def get_match_state(data: dict, match_id: str):
if SamsUtils.is_overview(data):
if match_id in data[MATCHSTATES]:
return data[MATCHSTATES][match_id]
return None

@staticmethod
def state_from_match_state(match_state: dict):
state = STATES_NOT_FOUND
if match_state:
if match_state[FINISHED]:
state = STATES_POST
elif match_state[STARTED]:
state = STATES_IN
else:
if match_state[STARTED]:
state = STATES_IN
else:
state = STATES_PRE
state = STATES_PRE
else:
state = STATES_PRE
return state
Expand All @@ -160,12 +160,12 @@ def select_match(data: dict, matches: list):
for match in matches:
state = SamsUtils.state_from_match(data, match)
# prefer active matches
if STATES_IN == state:
if state == STATES_IN:
return match

for match in matches:
state = SamsUtils.state_from_match(data, match)
if STATES_POST == state:
if state == STATES_POST:
duration = (
dt_util.now() - SamsUtils.date_from_match(match)
).total_seconds()
Expand All @@ -177,7 +177,7 @@ def select_match(data: dict, matches: list):

for match in matches:
state = SamsUtils.state_from_match(data, match)
if STATES_PRE == state:
if state == STATES_PRE:
# select the next
time_to_start = (
SamsUtils.date_from_match(match) - dt_util.now()
Expand Down Expand Up @@ -225,7 +225,7 @@ def fill_match_attrs(
]
)

if STATES_POST == state:
if state == STATES_POST:
attrs["team_score"] = match_state["setPoints"][team_num]
attrs["opponent_score"] = match_state["setPoints"][opponent_num]

Expand All @@ -237,7 +237,7 @@ def fill_match_attrs(
match_state, team_num, opponent_num, 0
)

if STATES_IN == state:
if state == STATES_IN:
attrs["team_score"] = match_state["matchSets"][-1]["setScore"][team_num]
attrs["opponent_score"] = match_state["matchSets"][-1]["setScore"][
opponent_num
Expand All @@ -255,6 +255,7 @@ def _get_ranking(league: dict, team_id: str):
for rank in league["rankings"]["fullRankings"]:
if rank[TEAM][ID] == team_id:
return rank
return None

@staticmethod
def fill_team_attributes(attrs: dict, data: dict, team: dict, state: str):
Expand All @@ -270,7 +271,7 @@ def fill_team_attributes(attrs: dict, data: dict, team: dict, state: str):
attrs["last_update"] = dt_util.as_local(dt_util.now())

attrs["team_name"] = team[NAME]
if STATES_NOT_FOUND == state:
if state == STATES_NOT_FOUND:
attrs["team_abbr"] = team[NAME]
else:
attrs["team_abbr"] = (
Expand Down

0 comments on commit ba8b627

Please sign in to comment.