-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Wink sensors #21
Closed
Closed
Wink sensors #21
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce9de1b
Add initial support for wink sensors.
kangaroo 7fbd36c
Add a new sensor component
kangaroo 454bea4
Reduce the timer interval to make sensors useful
kangaroo 80c2d81
Fire the sensor timer more often, and fix a typo
kangaroo 78f7c3c
Implement state for the sensor device
kangaroo 1501268
Remove switch state delay
kangaroo a731568
Reduce the threshold on the sensor changes, and rename it for easier …
kangaroo a287bb5
Updates to resolve flake8 errors
kangaroo abfaca3
Update sensor icon for now
kangaroo 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 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 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 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,89 @@ | ||
""" | ||
homeassistant.components.sensor | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Component to interface with various sensors that can be monitored. | ||
""" | ||
import logging | ||
from datetime import timedelta | ||
|
||
from homeassistant.loader import get_component | ||
import homeassistant.util as util | ||
from homeassistant.const import ( | ||
STATE_OPEN) | ||
from homeassistant.helpers import ( | ||
platform_devices_from_config) | ||
from homeassistant.components import group, discovery, wink | ||
|
||
DOMAIN = 'sensor' | ||
DEPENDENCIES = [] | ||
|
||
GROUP_NAME_ALL_SENSORS = 'all_sensors' | ||
ENTITY_ID_ALL_SENSORS = group.ENTITY_ID_FORMAT.format( | ||
GROUP_NAME_ALL_SENSORS) | ||
|
||
ENTITY_ID_FORMAT = DOMAIN + '.{}' | ||
|
||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=1) | ||
|
||
# Maps discovered services to their platforms | ||
DISCOVERY_PLATFORMS = { | ||
wink.DISCOVER_SENSORS: 'wink', | ||
} | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def is_on(hass, entity_id=None): | ||
""" Returns if the sensor is open based on the statemachine. """ | ||
entity_id = entity_id or ENTITY_ID_ALL_SENSORS | ||
|
||
return hass.states.is_state(entity_id, STATE_OPEN) | ||
|
||
|
||
def setup(hass, config): | ||
""" Track states and offer events for sensors. """ | ||
logger = logging.getLogger(__name__) | ||
|
||
sensors = platform_devices_from_config( | ||
config, DOMAIN, hass, ENTITY_ID_FORMAT, logger) | ||
|
||
# pylint: disable=unused-argument | ||
@util.Throttle(MIN_TIME_BETWEEN_SCANS) | ||
def update_sensor_states(now): | ||
""" Update states of all sensors. """ | ||
if sensors: | ||
logger.info("Updating sensor states") | ||
|
||
for sensor in sensors.values(): | ||
sensor.update_ha_state(hass, True) | ||
|
||
update_sensor_states(None) | ||
|
||
# Track all sensors in a group | ||
sensor_group = group.Group( | ||
hass, GROUP_NAME_ALL_SENSORS, sensors.keys(), False) | ||
|
||
def sensor_discovered(service, info): | ||
""" Called when a sensor is discovered. """ | ||
platform = get_component("{}.{}".format( | ||
DOMAIN, DISCOVERY_PLATFORMS[service])) | ||
|
||
discovered = platform.devices_discovered(hass, config, info) | ||
|
||
for sensor in discovered: | ||
if sensor is not None and sensor not in sensors.values(): | ||
sensor.entity_id = util.ensure_unique_string( | ||
ENTITY_ID_FORMAT.format(util.slugify(sensor.name)), | ||
sensors.keys()) | ||
|
||
sensors[sensor.entity_id] = sensor | ||
|
||
sensor.update_ha_state(hass) | ||
|
||
sensor_group.update_tracked_entity_ids(sensors.keys()) | ||
|
||
discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), sensor_discovered) | ||
|
||
hass.track_time_change(update_sensor_states) | ||
|
||
return True |
This file contains 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,35 @@ | ||
""" Support for Wink sensors. """ | ||
import logging | ||
|
||
# pylint: disable=no-name-in-module, import-error | ||
import homeassistant.external.wink.pywink as pywink | ||
|
||
from homeassistant.components.wink import WinkSensorDevice | ||
from homeassistant.const import CONF_ACCESS_TOKEN | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def get_devices(hass, config): | ||
""" Find and return Wink sensors. """ | ||
token = config.get(CONF_ACCESS_TOKEN) | ||
|
||
if token is None: | ||
logging.getLogger(__name__).error( | ||
"Missing wink access_token - " | ||
"get one at https://winkbearertoken.appspot.com/") | ||
return [] | ||
|
||
pywink.set_bearer_token(token) | ||
|
||
return get_sensors() | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def devices_discovered(hass, config, info): | ||
""" Called when a device is discovered. """ | ||
return get_sensors() | ||
|
||
|
||
def get_sensors(): | ||
""" Returns the Wink sensors. """ | ||
return [WinkSensorDevice(sensor) for sensor in pywink.get_sensors()] |
This file contains 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this one is dangerous, there are a bunch of components that listen to each time changed events to update their state. I'll make sure I'll update them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is basically hinged on the shitty Wink API. I can't find a way to get pushed, and I want to trigger lights on door entry. Chicken? Egg?
If we could get push from wink it would be easy, but waiting 10s to go to the bathroom at 2am sucks.