Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitceae01b

Browse files
authored
Merge pull request#4 from zacs/dev
pushing latest updates:* fix bug where config-based sensors clone themselves on reboot* added initial bye week logic* removed interval from the UI setup* fixed tests
2 parents9bc5728 +337bf4d commitceae01b

File tree

11 files changed

+42
-55
lines changed

11 files changed

+42
-55
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You'll need to know your team ID, which is a 2- or 3-letter acronym (eg. "SEA" f
3030

3131
###Via the "Configuration->Integrations" section of the Home Assistant UI
3232

33-
Look for the integration labeled "NFL" and enter your team's acronym in the UI prompt. Thereare alsofields for refresh interval and timeout, which are safe to keep as-is.
33+
Look for the integration labeled "NFL" and enter your team's acronym in the UI prompt. Thereis alsoa field for timeout, which are safe to keep as-is.
3434

3535
###Manually in your configuration.yaml file
3636

‎custom_components/nfl/__init__.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717

1818
from .constimport (
1919
API_ENDPOINT,
20-
CONF_INTERVAL,
2120
CONF_TIMEOUT,
2221
CONF_TEAM_ID,
2322
COORDINATOR,
24-
DEFAULT_INTERVAL,
2523
DEFAULT_TIMEOUT,
2624
DOMAIN,
2725
ISSUE_URL,
@@ -54,8 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5452
coordinator=AlertsDataUpdateCoordinator(
5553
hass,
5654
entry.data,
57-
entry.data.get(CONF_TIMEOUT),
58-
entry.data.get(CONF_INTERVAL),
55+
entry.data.get(CONF_TIMEOUT)
5956
)
6057

6158
# Fetch initial data so we have data when entities subscribe
@@ -85,36 +82,32 @@ async def update_listener(hass, entry):
8582
awaithass.config_entries.async_forward_entry_unload(entry,"sensor")
8683
hass.async_add_job(hass.config_entries.async_forward_entry_setup(entry,"sensor"))
8784

88-
8985
asyncdefasync_migrate_entry(hass,config_entry):
90-
"""Migrate an old config entry."""
91-
version=config_entry.version
92-
93-
# 1-> 2: Migration format
94-
ifversion==1:
95-
_LOGGER.debug("Migrating from version %s",version)
96-
updated_config=config_entry.data.copy()
86+
"""Migrate an old config entry."""
87+
version=config_entry.version
9788

98-
ifCONF_INTERVALnotinupdated_config.keys():
99-
updated_config[CONF_INTERVAL]=DEFAULT_INTERVAL
100-
ifCONF_TIMEOUTnotinupdated_config.keys():
101-
updated_config[CONF_TIMEOUT]=DEFAULT_TIMEOUT
89+
# 1-> 2: Migration format
90+
ifversion==1:
91+
_LOGGER.debug("Migrating from version %s",version)
92+
updated_config=config_entry.data.copy()
10293

103-
ifupdated_config!=config_entry.data:
104-
hass.config_entries.async_update_entry(config_entry,data=updated_config)
94+
ifCONF_TIMEOUTnotinupdated_config.keys():
95+
updated_config[CONF_TIMEOUT]=DEFAULT_TIMEOUT
10596

106-
config_entry.version=2
107-
_LOGGER.debug("Migration to version %s complete",config_entry.version)
97+
ifupdated_config!=config_entry.data:
98+
hass.config_entries.async_update_entry(config_entry,data=updated_config)
10899

109-
returnTrue
100+
config_entry.version=2
101+
_LOGGER.debug("Migration to version %s complete",config_entry.version)
110102

103+
returnTrue
111104

112105
classAlertsDataUpdateCoordinator(DataUpdateCoordinator):
113106
"""Class to manage fetching NFL data."""
114107

115-
def__init__(self,hass,config,the_timeout:int,interval:int):
108+
def__init__(self,hass,config,the_timeout:int):
116109
"""Initialize."""
117-
self.interval=timedelta(minutes=interval)
110+
self.interval=timedelta(minutes=10)
118111
self.name=config[CONF_NAME]
119112
self.timeout=the_timeout
120113
self.config=config
@@ -128,7 +121,7 @@ async def _async_update_data(self):
128121
"""Fetch data"""
129122
asyncwithtimeout(self.timeout):
130123
try:
131-
data=awaitupdate_alerts(self.config)
124+
data=awaitupdate_game(self.config)
132125
# update the interval based on flag
133126
ifdata["private_fast_refresh"]==True:
134127
self.update_interval=timedelta(seconds=5)
@@ -140,7 +133,7 @@ async def _async_update_data(self):
140133

141134

142135

143-
asyncdefupdate_alerts(config)->dict:
136+
asyncdefupdate_game(config)->dict:
144137
"""Fetch new state data for the sensor.
145138
This is the only method that should fetch new data for Home Assistant.
146139
"""
@@ -162,11 +155,13 @@ async def async_get_state(config) -> dict:
162155
ifr.status==200:
163156
data=awaitr.json()
164157

158+
found_team=False
165159
ifdataisnotNone:
166160
foreventindata["events"]:
167161
#_LOGGER.debug("Looking at this event: %s" % event)
168-
_LOGGER.debug("Found event; parsing data.")
169162
ifteam_idinevent["shortName"]:
163+
_LOGGER.debug("Found event; parsing data.")
164+
found_team=True
170165
team_index=0ifevent["competitions"][0]["competitors"][0]["team"]["abbreviation"]==team_idelse1
171166
oppo_index=abs((team_index-1))
172167
values["state"]=event["status"]["type"]["state"].upper()
@@ -241,14 +236,19 @@ async def async_get_state(config) -> dict:
241236
values["opponent_score"]=event["competitions"][0]["competitors"][oppo_index]["score"]
242237
values["last_update"]=arrow.now().format(arrow.FORMAT_W3C)
243238
values["private_fast_refresh"]=False
239+
240+
# Never found the team. Either a bye or a post-season condition
241+
ifnotfound_team:
242+
_LOGGER.debug("Did not find a game with for the configured team. Is it a bye week?")
243+
values["state"]='BYE'
244244

245245
ifvalues["state"]=='PRE'and ((arrow.get(values["date"])-arrow.now()).total_seconds()<1200):
246246
_LOGGER.debug("Event is within 20 minutes, setting refresh rate to 5 seconds.")
247247
values["private_fast_refresh"]=True
248248
elifvalues["state"]=='IN':
249249
_LOGGER.debug("Event in progress, setting refresh rate to 5 seconds.")
250250
values["private_fast_refresh"]=True
251-
elifvalues["state"]=='POST':# and if the refresh is set to fast
251+
elifvalues["state"]in ['POST','BYE']:
252252
_LOGGER.debug("Event is over, setting refresh back to 10 minutes.")
253253
values["private_fast_refresh"]=False
254254

‎custom_components/nfl/config_flow.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313

1414
from .constimport (
1515
API_ENDPOINT,
16-
CONF_INTERVAL,
1716
CONF_TIMEOUT,
1817
CONF_TEAM_ID,
19-
DEFAULT_INTERVAL,
2018
DEFAULT_NAME,
2119
DEFAULT_TIMEOUT,
2220
DOMAIN,
@@ -43,7 +41,6 @@ def _get_default(key):
4341
{
4442
vol.Required(CONF_TEAM_ID,default=_get_default(CONF_TEAM_ID)):str,
4543
vol.Optional(CONF_NAME,default=_get_default(CONF_NAME)):str,
46-
vol.Optional(CONF_INTERVAL,default=_get_default(CONF_INTERVAL)):int,
4744
vol.Optional(CONF_TIMEOUT,default=_get_default(CONF_TIMEOUT)):int,
4845
}
4946
)
@@ -128,7 +125,6 @@ async def _show_config_form(self, user_input):
128125
# Defaults
129126
defaults= {
130127
CONF_NAME:DEFAULT_NAME,
131-
CONF_INTERVAL:DEFAULT_INTERVAL,
132128
CONF_TIMEOUT:DEFAULT_TIMEOUT,
133129
CONF_TEAM_ID:self._team_list,
134130
}

‎custom_components/nfl/const.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
# Config
66
CONF_TIMEOUT="timeout"
7-
CONF_INTERVAL="interval"
87
CONF_TEAM_ID="team_id"
98

109
# Defaults
1110
DEFAULT_ICON="mdi:football"
1211
DEFAULT_NAME="NFL"
13-
DEFAULT_INTERVAL=1
1412
DEFAULT_TIMEOUT=120
1513

1614
# Misc

‎custom_components/nfl/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"domain":"nfl",
3-
"name":"NFL Scores",
3+
"name":"NFL",
44
"version":"0.1",
55
"documentation":"https://github.com/zacs/ha_nfl",
66
"issue_tracker":"https://github.com/zacs/ha_nfl/issues",

‎custom_components/nfl/sensor.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313

1414
from .constimport (
1515
ATTRIBUTION,
16-
CONF_INTERVAL,
1716
CONF_TIMEOUT,
1817
CONF_TEAM_ID,
1918
COORDINATOR,
2019
DEFAULT_ICON,
21-
DEFAULT_INTERVAL,
2220
DEFAULT_NAME,
2321
DEFAULT_TIMEOUT,
2422
DOMAIN,
@@ -30,7 +28,6 @@
3028
{
3129
vol.Required(CONF_TEAM_ID):cv.string,
3230
vol.Optional(CONF_NAME,default=DEFAULT_NAME):cv.string,
33-
vol.Optional(CONF_INTERVAL,default=DEFAULT_INTERVAL):int,
3431
vol.Optional(CONF_TIMEOUT,default=DEFAULT_TIMEOUT):int,
3532
}
3633
)
@@ -40,18 +37,17 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
4037
"""Configuration from yaml"""
4138
ifDOMAINnotinhass.data.keys():
4239
hass.data.setdefault(DOMAIN, {})
43-
config.entry_id=uuid.uuid4().hex
40+
config.entry_id=slugify(f"{config.get(CONF_TEAM_ID)}")
4441
config.data=config
4542
else:
46-
config.entry_id=uuid.uuid4().hex
43+
config.entry_id=slugify(f"{config.get(CONF_TEAM_ID)}")
4744
config.data=config
4845

4946
# Setup the data coordinator
5047
coordinator=AlertsDataUpdateCoordinator(
5148
hass,
5249
config,
5350
config[CONF_TIMEOUT],
54-
config[CONF_INTERVAL],
5551
)
5652

5753
# Fetch initial data so we have data when entities subscribe

‎custom_components/nfl/translations/en.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"data": {
66
"name":"Friendly Name",
77
"team_id":"Team Acronym",
8-
"interval":"Update Interval (in minutes)",
98
"timeout":"Update Timeout (in seconds)"
109
},
1110
"description":"You can find your 2 or 3-letter acronym on the ESPN NFL page's banner, at the top score strip.",
@@ -19,7 +18,6 @@
1918
"data": {
2019
"name":"Friendly Name",
2120
"team_id":"Team Acronym",
22-
"interval":"Update Interval (in minutes)",
2321
"timeout":"Update Timeout (in seconds)"
2422
},
2523
"description":"You can find your 2 or 3-letter acronym on the ESPN NFL page's banner, at the top score strip.",

‎requirements_test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ black
22
isort
33
pytest
44
pytest-cov
5-
pytest-homeassistant-custom-component
5+
pytest-homeassistant-custom-component
6+
arrow

‎tests/test_config_flow.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
[
1515
(
1616
{
17-
"name":"TestingScores",
17+
"name":"TestingState",
1818
"team_id":"SEA",
19-
"interval":5,
2019
"timeout":120,
2120
},
2221
"user",
23-
"TestingAlerts",
22+
"TestingState",
2423
{
25-
"name":"TestingScores",
24+
"name":"TestingState",
2625
"team_id":"SEA",
27-
"interval":5,
2826
"timeout":120,
2927
},
3028
),
@@ -68,7 +66,7 @@ async def test_form(
6866
# [
6967
# {
7068
# DOMAIN: {
71-
# CONF_NAME: "NFL Scores",
69+
# CONF_NAME: "NFL",
7270
# CONF_TEAM_ID: "SEA",
7371
# },
7472
# },

‎tests/test_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async def test_setup_entry(
1818
"""Test settting up entities."""
1919
entry=MockConfigEntry(
2020
domain=DOMAIN,
21-
title="NFL Scores",
21+
title="NFL",
2222
data=CONFIG_DATA,
2323
)
2424

@@ -35,7 +35,7 @@ async def test_unload_entry(hass):
3535
"""Test unloading entities."""
3636
entry=MockConfigEntry(
3737
domain=DOMAIN,
38-
title="NFL Scores",
38+
title="NFL",
3939
data=CONFIG_DATA,
4040
)
4141

@@ -61,7 +61,7 @@ async def test_unload_entry(hass):
6161
# """Test importing a config."""
6262
# entry = MockConfigEntry(
6363
# domain=DOMAIN,
64-
# title="NFL Scores",
64+
# title="NFL",
6565
# data=CONFIG_DATA,
6666
# )
6767
# await async_setup_component(hass, "persistent_notification", {})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp