From a664f8c51492c2336887c4bcc90afeda17b5348c Mon Sep 17 00:00:00 2001 From: ccostan Date: Tue, 30 Jan 2018 22:58:36 -0500 Subject: [PATCH] #307 - Added in the minecraft stuff. - Notifications or some sort of automations will follow. Stay tuned. --- automation/Speech/door_opened.yaml | 2 +- custom_components/sensor/minecraft.py | 81 ++++++++++++++++++++++ dasher/dasher | 98 --------------------------- packages/fire_tablet.yaml | 24 ------- packages/minecraft.yaml | 29 ++++++++ 5 files changed, 111 insertions(+), 123 deletions(-) create mode 100755 custom_components/sensor/minecraft.py delete mode 100755 dasher/dasher create mode 100755 packages/minecraft.yaml diff --git a/automation/Speech/door_opened.yaml b/automation/Speech/door_opened.yaml index 8e7320db..165e818f 100755 --- a/automation/Speech/door_opened.yaml +++ b/automation/Speech/door_opened.yaml @@ -23,7 +23,7 @@ action: - service: script.notify_engine data_template: - who: "parents" + who: 'parents' value1: "The {{ trigger.to_state.attributes.friendly_name }} has been {{ (trigger.to_state.state)|replace('_', ' ') }}." - service: input_boolean.turn_on diff --git a/custom_components/sensor/minecraft.py b/custom_components/sensor/minecraft.py new file mode 100755 index 00000000..fa8e1724 --- /dev/null +++ b/custom_components/sensor/minecraft.py @@ -0,0 +1,81 @@ +""" +Sensor to check the status of a Minecraft server. + +""" +import logging +from homeassistant.helpers.entity import Entity +ATTR_PING = 'Ping' +ATTR_USERS = 'Users Online' +ATTR_MOTD = 'MOTD' +ATTR_VERSION = 'Version' +ICON = 'mdi:minecraft' +REQUIREMENTS = ['mcstatus==2.1'] + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the Minecraft server platform.""" + from mcstatus import MinecraftServer as mcserver + logger = logging.getLogger(__name__) + + server = config.get('server') + name = config.get('name') + + if server is None: + logger.error('No server specified') + return False + elif name is None: + logger.error('No name specified') + return False + + add_devices([ + MCServerSensor(server, name, mcserver) + ]) + + +class MCServerSensor(Entity): + """A class for the Minecraft server.""" + + # pylint: disable=abstract-method + def __init__(self, server, name, mcserver): + """Initialize the sensor.""" + self._mcserver = mcserver + self._server = server + self._name = name + self.update() + + @property + def name(self): + """Return the name of the server.""" + return self._name + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + # pylint: disable=no-member + def update(self): + """Update device state.""" + status = self._mcserver.lookup(self._server).status() + query = self._mcserver.lookup(self._server).query() + self._state = str(status.players.online) + '/' + str(status.players.max) + self._ping = status.latency + self._users = query.players.names + self._motd = query.motd + self._version = query.software.version + + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return { + ATTR_PING: self._ping, + ATTR_USERS: self._users, + ATTR_MOTD: self._motd, + ATTR_VERSION: self._version + } + + @property + def icon(self): + """Return the icon to use in the frontend.""" + return ICON diff --git a/dasher/dasher b/dasher/dasher deleted file mode 100755 index 5f28fb2f..00000000 --- a/dasher/dasher +++ /dev/null @@ -1,98 +0,0 @@ -#!/bin/sh -### BEGIN INIT INFO -# Provides: -# Required-Start: $remote_fs $syslog -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Start daemon at boot time -# Description: Enable service provided by daemon. -### END INIT INFO - -dir="/home/pi/dasher" -cmd="DEBUG=* node app.js" -user="root" - -name=`basename $0` -pid_file="/var/run/$name.pid" -stdout_log="/var/log/$name.log" -stderr_log="/var/log/$name.err" - -get_pid() { - cat "$pid_file" -} - -is_running() { - [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1 -} - -case "$1" in - start) - if is_running; then - echo "Already started" - else - echo "Starting $name" - cd "$dir" - if [ -z "$user" ]; then - sudo $cmd >> "$stdout_log" 2>> "$stderr_log" & - else - sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" & - fi - echo $! > "$pid_file" - if ! is_running; then - echo "Unable to start, see $stdout_log and $stderr_log" - exit 1 - fi - fi - ;; - stop) - if is_running; then - echo -n "Stopping $name.." - kill `get_pid` - for i in {1..10} - do - if ! is_running; then - break - fi - - echo -n "." - sleep 1 - done - echo - - if is_running; then - echo "Not stopped; may still be shutting down or shutdown may have failed" - exit 1 - else - echo "Stopped" - if [ -f "$pid_file" ]; then - rm "$pid_file" - fi - fi - else - echo "Not running" - fi - ;; - restart) - $0 stop - if is_running; then - echo "Unable to stop, will not attempt to start" - exit 1 - fi - $0 start - ;; - status) - if is_running; then - echo "Running" - else - echo "Stopped" - exit 1 - fi - ;; - *) - echo "Usage: $0 {start|stop|restart|status}" - exit 1 - ;; -esac - -exit 0 diff --git a/packages/fire_tablet.yaml b/packages/fire_tablet.yaml index 7b3e5d1d..073b6609 100755 --- a/packages/fire_tablet.yaml +++ b/packages/fire_tablet.yaml @@ -12,27 +12,3 @@ group: - binary_sensor.bedroom_plugged - binary_sensor.clock_motion - binary_sensor.clock_plugged - -# automation: -# -# - alias: 'Enable battery help!' -# trigger: -# - platform: state -# entity_id: -# - binary_sensor.clock_plugged -# - binary_sensor.bedroom_plugged -# to: 'off' -# from: 'on' -# -# - alias: 'Cry when the Battery is about to die. ' -# trigger: -# - platform: state -# entity_id: -# - input_boolean.clock_snooze -# to: 'on' -# from: 'off' -# action: -# - delay: -# minutes: 10 -# - service: input_boolean.turn_off -# entity_id: input_boolean.clock_snooze diff --git a/packages/minecraft.yaml b/packages/minecraft.yaml new file mode 100755 index 00000000..0b4d9199 --- /dev/null +++ b/packages/minecraft.yaml @@ -0,0 +1,29 @@ +#------------------------------------------- +# @CCOSTAN +# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig +# Neato Support for D7 Connected Botvac - control my [Neato Vacuum](http://amzn.to/2kqnnqu) with Home Assistant. +#------------------------------------------- +# homeassistant: +# customize_glob: +# "*.*_sleep_hours": +# unit_of_measurement: hours +# icon: mdi:sleep +# +# hidden: False +#------------------------------------------- +sensor: + platform: minecraft + name: Bear Stone + server: 192.168.10.10 +#------------------------------------------- +# group: +# finance: +# entities: +# - sensor.tesla +# - sensor.bitcoin + +############################################################################## +### Automations - Detect when things are not right. Like any Good Watchdog. +############################################################################## +#automation: +#Tweets pushed out to twitter.