-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Make sun solar_rising a binary_sensor
#140956
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Sensor platform for Sun integration.""" | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from homeassistant.components.binary_sensor import ( | ||
| DOMAIN as BINARY_SENSOR_DOMAIN, | ||
| BinarySensorEntity, | ||
| BinarySensorEntityDescription, | ||
| ) | ||
| from homeassistant.const import EntityCategory | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
|
|
||
| from .const import DOMAIN, SIGNAL_EVENTS_CHANGED | ||
| from .entity import Sun, SunConfigEntry | ||
|
|
||
| ENTITY_ID_BINARY_SENSOR_FORMAT = BINARY_SENSOR_DOMAIN + ".sun_{}" | ||
|
|
||
|
|
||
| @dataclass(kw_only=True, frozen=True) | ||
| class SunBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
| """Describes a Sun sensor entity.""" | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
|
|
||
| value_fn: Callable[[Sun], bool | None] | ||
| signal: str | ||
|
|
||
|
|
||
| BINARY_SENSOR_TYPES: tuple[SunBinarySensorEntityDescription, ...] = ( | ||
| SunBinarySensorEntityDescription( | ||
| key="solar_rising", | ||
| translation_key="solar_rising", | ||
| value_fn=lambda data: data.rising, | ||
| entity_registry_enabled_default=False, | ||
| signal=SIGNAL_EVENTS_CHANGED, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| entry: SunConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up Sun binary sensor platform.""" | ||
|
|
||
| sun = entry.runtime_data | ||
|
|
||
| async_add_entities( | ||
| [ | ||
| SunBinarySensor(sun, description, entry.entry_id) | ||
| for description in BINARY_SENSOR_TYPES | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| class SunBinarySensor(BinarySensorEntity): | ||
| """Representation of a Sun Sensor.""" | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
|
|
||
| _attr_has_entity_name = True | ||
| _attr_should_poll = False | ||
| _attr_entity_category = EntityCategory.DIAGNOSTIC | ||
| entity_description: SunBinarySensorEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| sun: Sun, | ||
| entity_description: SunBinarySensorEntityDescription, | ||
| entry_id: str, | ||
| ) -> None: | ||
| """Initiate Sun Binary Sensor.""" | ||
| self.entity_description = entity_description | ||
| self.entity_id = ENTITY_ID_BINARY_SENSOR_FORMAT.format(entity_description.key) | ||
| self._attr_unique_id = f"{entry_id}-binary-{entity_description.key}" | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
| self.sun = sun | ||
| self._attr_device_info = DeviceInfo( | ||
| name="Sun", | ||
| identifiers={(DOMAIN, entry_id)}, | ||
| entry_type=DeviceEntryType.SERVICE, | ||
| ) | ||
|
|
||
| @property | ||
| def is_on(self) -> bool | None: | ||
| """Return value of binary sensor.""" | ||
| return self.entity_description.value_fn(self.sun) | ||
|
|
||
| async def async_added_to_hass(self) -> None: | ||
| """Register signal listener when added to hass.""" | ||
| await super().async_added_to_hass() | ||
| self.async_on_remove( | ||
| async_dispatcher_connect( | ||
| self.hass, | ||
| self.entity_description.signal, | ||
| self.async_write_ha_state, | ||
| ) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """The tests for the Sun binary_sensor platform.""" | ||
|
|
||
| from datetime import datetime, timedelta | ||
|
|
||
| from freezegun.api import FrozenDateTimeFactory | ||
| import pytest | ||
|
|
||
| from homeassistant.components import sun | ||
| from homeassistant.const import EntityCategory | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import entity_registry as er | ||
| from homeassistant.setup import async_setup_component | ||
| from homeassistant.util import dt as dt_util | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
| async def test_setting_rising( | ||
| hass: HomeAssistant, | ||
| entity_registry: er.EntityRegistry, | ||
| freezer: FrozenDateTimeFactory, | ||
| ) -> None: | ||
| """Test retrieving sun setting and rising.""" | ||
| utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC) | ||
| freezer.move_to(utc_now) | ||
| await async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: {}}) | ||
| await hass.async_block_till_done() | ||
|
|
||
| assert hass.states.get("binary_sensor.sun_solar_rising").state == "on" | ||
|
|
||
| entry_ids = hass.config_entries.async_entries("sun") | ||
|
|
||
| freezer.tick(timedelta(hours=12)) | ||
| # Block once for Sun to update | ||
| await hass.async_block_till_done() | ||
| # Block another time for the sensors to update | ||
| await hass.async_block_till_done() | ||
|
|
||
| # Make sure all the signals work | ||
| assert hass.states.get("binary_sensor.sun_solar_rising").state == "off" | ||
|
|
||
| entity = entity_registry.async_get("binary_sensor.sun_solar_rising") | ||
| assert entity | ||
| assert entity.entity_category is EntityCategory.DIAGNOSTIC | ||
| assert entity.unique_id == f"{entry_ids[0].entry_id}-binary-solar_rising" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.