Skip to content

Commit

Permalink
v0.12.0 Release Candidate (#152)
Browse files Browse the repository at this point in the history
* Fix 2024.05 YAML defect
* Fix test to not unload any more
  • Loading branch information
vasqued2 authored May 18, 2024
1 parent e08492e commit e7918b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
50 changes: 33 additions & 17 deletions custom_components/teamtracker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ async def async_setup_platform(

if DOMAIN not in hass.data.keys():
hass.data.setdefault(DOMAIN, {})
config.entry_id = slugify(f"{config.get(CONF_TEAM_ID)}")
config.data = config
else:
config.entry_id = slugify(f"{config.get(CONF_TEAM_ID)}")
config.data = config
# config.entry_id = slugify(f"{config.get(CONF_TEAM_ID)}")
# config.data = config
# else:
# config.entry_id = slugify(f"{config.get(CONF_TEAM_ID)}")
# config.data = config
entry_id = slugify(f"{config.get(CONF_TEAM_ID)}")

# Setup the data coordinator
coordinator = TeamTrackerDataUpdateCoordinator(
Expand All @@ -127,10 +128,10 @@ async def async_setup_platform(
# Fetch initial data so we have data when entities subscribe
await coordinator.async_refresh()

hass.data[DOMAIN][config.entry_id] = {
hass.data[DOMAIN][entry_id] = {
COORDINATOR: coordinator,
}
async_add_entities([TeamTrackerScoresSensor(hass, config)], True)
async_add_entities([TeamTrackerScoresSensor(hass, None, config)], True)


async def async_setup_entry(
Expand All @@ -152,34 +153,49 @@ async def async_setup_entry(
if entry.options:
config.update(entry.options)

async_add_entities([TeamTrackerScoresSensor(hass, entry)], True)
async_add_entities([TeamTrackerScoresSensor(hass, entry, None)], True)


class TeamTrackerScoresSensor(CoordinatorEntity):
"""Representation of a Sensor."""

def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
def __init__(self, hass: HomeAssistant, entry: ConfigEntry, config: ConfigType) -> None:
"""Initialize the sensor."""
super().__init__(hass.data[DOMAIN][entry.entry_id][COORDINATOR])

sport_path = entry.data.get(CONF_SPORT_PATH, DEFAULT_SPORT_PATH)
if entry is not None: # GUI setup
entry_id = entry.entry_id
sensor_coordinator = hass.data[DOMAIN][entry_id][COORDINATOR]
super().__init__(sensor_coordinator)
sport_path = entry.data.get(CONF_SPORT_PATH, DEFAULT_SPORT_PATH)
sensor_name = entry.data[CONF_NAME]

else: # YAML setup
entry_id = slugify(f"{config.get(CONF_TEAM_ID)}")
sensor_coordinator = hass.data[DOMAIN][entry_id][COORDINATOR]
super().__init__(sensor_coordinator)
try:
sport_path = config[CONF_SPORT_PATH]
except:
sport_path = DEFAULT_SPORT_PATH
sensor_name = config[CONF_NAME]

if sport_path == DEFAULT_SPORT_PATH:
_LOGGER.debug(
"%s: Initializing sensor values. SPORT_PATH not set.",
entry.data[CONF_NAME],
sensor_name,
)

icon = SPORT_ICON_MAP.get(sport_path, DEFAULT_ICON)
if icon == DEFAULT_ICON:
_LOGGER.debug(
"%s: Initializing sensor values. Sport icon not found for sport '%s'",
entry.data[CONF_NAME],
sensor_name,
sport_path,
)

self.coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR]
self._config = entry
self._name = entry.data[CONF_NAME]
self.coordinator = sensor_coordinator
self._entry_id = entry_id
self._name = sensor_name
self._icon = icon
self._state = "PRE"

Expand Down Expand Up @@ -251,7 +267,7 @@ def unique_id(self) -> str:
"""
Return a unique, Home Assistant friendly identifier for this entity.
"""
return f"{slugify(self._name)}_{self._config.entry_id}"
return f"{slugify(self._name)}_{self._entry_id}"

@property
def name(self) -> str:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ async def test_options_flow_init(
assert {CONF_API_LANGUAGE: "en"} == result["data"]

# Unload
assert await entry.async_unload(hass)

# No need to unload any more

# assert await entry.async_unload(hass)
await hass.async_block_till_done()


Expand Down

0 comments on commit e7918b2

Please sign in to comment.