Skip to content

Commit

Permalink
Add name option
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Oct 24, 2023
1 parent 3f12ee7 commit 4003b63
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions custom_components/sun2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def __init__(
loc_params: LocParams | None,
namespace: str | None,
at_time: str | time,
name: str,
) -> None:
"""Initialize sensor."""
assert isinstance(at_time, time)
Expand All @@ -232,8 +233,7 @@ def __init__(
native_unit_of_measurement=DEGREE,
state_class=SensorStateClass.MEASUREMENT,
)
# TODO: name
super().__init__(loc_params, namespace, entity_description)
super().__init__(loc_params, namespace, entity_description, name=name)
self._event = "solar_elevation"

def _update(self, cur_dttm: datetime) -> None:
Expand Down Expand Up @@ -1083,6 +1083,15 @@ def _tae_defaults(config: ConfigType) -> ConfigType:
return config


def _eat_defaults(config: ConfigType) -> ConfigType:
"""Fill in defaults."""

if not config.get(CONF_NAME):
config[CONF_NAME] = f"Elevation at {config[CONF_ELEVATION_AT_TIME]}"

return config


TIME_AT_ELEVATION_SCHEMA = vol.All(
vol.Schema(
{
Expand All @@ -1097,14 +1106,18 @@ def _tae_defaults(config: ConfigType) -> ConfigType:
_tae_defaults,
)

ELEVATION_AT_TIME_SCHEMA = vol.Schema(
{
vol.Required(CONF_ELEVATION_AT_TIME): vol.Any(
vol.All(cv.string, cv.entity_domain("input_datetime")),
cv.time,
msg="Expected input_datetime entity ID or time string",
),
}
ELEVATION_AT_TIME_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_ELEVATION_AT_TIME): vol.Any(
vol.All(cv.string, cv.entity_domain("input_datetime")),
cv.time,
msg="Expected input_datetime entity ID or time string",
),
vol.Optional(CONF_NAME): cv.string,
}
),
_eat_defaults,
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
Expand Down Expand Up @@ -1152,6 +1165,7 @@ async def async_setup_platform(
loc_params,
namespace,
sensor[CONF_ELEVATION_AT_TIME],
sensor[CONF_NAME],
)
)

Expand Down

0 comments on commit 4003b63

Please sign in to comment.