def tplink_consumption(ip): plug = SmartPlug(ip) # read meter information und parse JSON pluginfo = json.loads(str(json.dumps(plug.get_emeter_realtime()))) # filter for total power consumption consumption = pluginfo['total'] # return consumption return consumption
def getDeviceStatus(self): ip = self.get_variable("ip") port = self.get_variable("port") p = SmartPlug(ip) emeter_info = p.get_emeter_realtime() self.set_variable('status', str(p.state)) try: self.set_variable('current', str(emeter_info['current'])) self.set_variable('voltage', str(emeter_info['voltage'])) self.set_variable('power', str(emeter_info['power'])) except: print("no energy measument modul") self.printDeviceStatus()
async def test_unload(hass, platform): """Test that the async_unload_entry works.""" # As we have currently no configuration, we just to pass the domain here. entry = MockConfigEntry(domain=tplink.DOMAIN) entry.add_to_hass(hass) with patch( "homeassistant.components.tplink.get_static_devices" ) as get_static_devices, patch( "homeassistant.components.tplink.common.SmartDevice._query_helper" ), patch( f"homeassistant.components.tplink.{platform}.async_setup_entry", return_value=mock_coro(True), ) as async_setup_entry: config = { tplink.DOMAIN: { platform: [{ CONF_HOST: "123.123.123.123" }], CONF_DISCOVERY: False, } } light = SmartBulb("123.123.123.123") switch = SmartPlug("321.321.321.321") switch.get_sysinfo = MagicMock( return_value=SMARTPLUG_HS110_DATA["sysinfo"]) switch.get_emeter_realtime = MagicMock( return_value=EmeterStatus(SMARTPLUG_HS110_DATA["realtime"])) if platform == "light": get_static_devices.return_value = SmartDevices([light], []) elif platform == "switch": get_static_devices.return_value = SmartDevices([], [switch]) assert await async_setup_component(hass, tplink.DOMAIN, config) await hass.async_block_till_done() assert len(async_setup_entry.mock_calls) == 1 assert tplink.DOMAIN in hass.data assert await tplink.async_unload_entry(hass, entry) assert not hass.data[tplink.DOMAIN]
output = os.environ.get('OUTPUT', '/data/out/out.csv') doutput = os.path.dirname(output) host = os.environ.get('PLUG', 'plug') titles = ['date', 'power', 'voltage', 'current', 'total'] rtitles = dict([(a, a) for a in titles]) if not os.path.exists(doutput): os.makedirs(doutput) try: content = open(output).read(1024) except Exception: content = '' if not content: write(rtitles) print('Hit control C to stop scanning') while True: now = datetime.datetime.now() try: sd = SmartPlug(host) state = sd.get_emeter_realtime() except pyHS100.smartdevice.SmartDeviceException: continue state["date"] = now.strftime('%Y-%m-%d %H:%M:%S') write(state, mode='a') time.sleep(1) # vim:set et sts=4 ts=4 tw=120:
class HS110: def __init__(self, ip): self.plug = SmartPlug(ip) self.name = self.plug.alias while True: self.today = datetime.date.today() try: self.write(self.read()) except Exception as e: print("Current Stats Exception: {}".format(e)) time.sleep(1) def read(self): year = self.today.year month = self.today.month realtime = self.plug.get_emeter_realtime() daily_avg = self.plug.get_emeter_daily(year, month) jsons = [] x = 0 for day in daily_avg: daytime = datetime.datetime(int(year), int(month), int(day), 0, 0, 0, 0) jsons.append({ "measurement": "power", "tags": { "name": self.name }, "fields": { "daily_average": daily_avg[day] }, "time": daytime }) x += 1 jsons.append({ "measurement": "power", "tags": { "name": self.name }, "fields": { "milliwatt": realtime["power_mw"], "milliampere": realtime["current_ma"], "millivolt": realtime["voltage_mv"], "watthours": realtime["total_wh"] }, "time": datetime.datetime.utcnow() }) return jsons def write(self, jsons): try: client.write_points(jsons, protocol="json", retention_policy=config.influx_retention_policy) except Exception as e: print("Write exception: {}".format(e))
emeterdaily = plug.get_emeter_daily(year=2018, month=10) for k, v in emeterdaily.items(): row["day%s" % k] = v hwinfo = plug.hw_info for k, v in hwinfo.items(): row["%s" % k] = v sysinfo = plug.get_sysinfo() for k, v in sysinfo.items(): row["%s" % k] = v timezone = plug.timezone for k, v in timezone.items(): row["%s" % k] = v emetermonthly = plug.get_emeter_monthly(year=2018) for k, v in emetermonthly.items(): row["month%s" % k] = v realtime = plug.get_emeter_realtime() for k, v in realtime.items(): row["%s" % k] = v row['alias'] = plug.alias row['time'] = plug.time.strftime('%m/%d/%Y %H:%M:%S') row['ledon'] = plug.led row['systemtime'] = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S') json_string = json.dumps(row) print(json_string)
client.switch_database("display_measurements") # tplink connection plug = SmartPlug("192.168.0.32") print("Tp-Link HS110 status: ", plug.state) amount = 200 while True: table = input("Measurement: ") if (table == 'q'): break for i in range(1, amount + 1): plug = SmartPlug("192.168.0.32") all_informations = str(plug.get_emeter_realtime()) now = datetime.now() current_time = now.strftime("%Y-%m-%d %H:%M:%S") splitted = all_informations.split(' ') voltage_mv = splitted[1][:-1] current_ma = splitted[3][:-1] power_mw = splitted[5][:-1] total_wh = splitted[7][:-1] print("Number: ", i) #print("Date : ", current_time.split(' ')[0]) print("Time : ", current_time.split(' ')[1])
#TP Link Smart Plug - Energy Monitoring for YuMi Work Cell #Author: Simran Nijjar from pyHS100 import Discover, SmartPlug from pprint import pformat as pf #Smart Plug Discovery for dev in Discover.discover().values(): print(dev) #Connect to Smart Plug (IP address determined by Discover) plug = SmartPlug("10.0.2.36") #Get Plug Information ##print("Hardware: %s" % pf(plug.hw_info)) ##print("System Information: %s" % pf(plug.get_sysinfo())) #Get Plug Status print("Current State: %s" % plug.state) #Turn Plug Off plug.turn_off() #Turn Plug On ######plug.turn_on() #Energy Monitoring Data print("Current Consumption: %s" % plug.get_emeter_realtime()) print("Per Day: %s" % plug.get_emeter_daily(year=2018, month=9)) print("Per Month: %s" % plug.get_emeter_monthly(year=2018))
async def test_platforms_are_initialized(hass: HomeAssistant): """Test that platforms are initialized per configuration array.""" config = { tplink.DOMAIN: { CONF_DISCOVERY: False, CONF_LIGHT: [{ CONF_HOST: "123.123.123.123" }], CONF_SWITCH: [{ CONF_HOST: "321.321.321.321" }], } } with patch( "homeassistant.components.tplink.common.Discover.discover" ), patch( "homeassistant.components.tplink.get_static_devices" ) as get_static_devices, patch( "homeassistant.components.tplink.common.SmartDevice._query_helper" ), patch( "homeassistant.components.tplink.light.async_setup_entry", return_value=mock_coro(True), ), patch( "homeassistant.components.tplink.common.SmartPlug.is_dimmable", False, ): light = SmartBulb("123.123.123.123") switch = SmartPlug("321.321.321.321") switch.get_sysinfo = MagicMock( return_value=SMARTPLUG_HS110_DATA["sysinfo"]) switch.get_emeter_realtime = MagicMock( return_value=EmeterStatus(SMARTPLUG_HS110_DATA["realtime"])) switch.get_emeter_daily = MagicMock( return_value={int(time.strftime("%e")): 1.123}) get_static_devices.return_value = SmartDevices([light], [switch]) # patching is_dimmable is necessray to avoid misdetection as light. await async_setup_component(hass, tplink.DOMAIN, config) await hass.async_block_till_done() state = hass.states.get(f"switch.{switch.alias}") assert state assert state.name == switch.alias for description in ENERGY_SENSORS: state = hass.states.get( f"sensor.{switch.alias}_{slugify(description.name)}") assert state assert state.state is not None assert state.name == f"{switch.alias} {description.name}" device_registry = dr.async_get(hass) assert len(device_registry.devices) == 1 device = next(iter(device_registry.devices.values())) assert device.name == switch.alias assert device.model == switch.model assert device.connections == {(dr.CONNECTION_NETWORK_MAC, switch.mac.lower())} assert device.sw_version == switch.sys_info[CONF_SW_VERSION]
class TestSmartPlug(TestCase): # these schemas should go to the mainlib as # they can be useful when adding support for new features/devices # as well as to check that faked devices are operating properly. sysinfo_schema = Schema({ 'active_mode': check_mode, 'alias': basestring, 'dev_name': basestring, 'deviceId': basestring, 'feature': basestring, 'fwId': basestring, 'hwId': basestring, 'hw_ver': basestring, 'icon_hash': basestring, 'latitude': All(float, Range(min=-90, max=90)), 'led_off': check_int_bool, 'longitude': All(float, Range(min=-180, max=180)), 'mac': check_mac, 'model': basestring, 'oemId': basestring, 'on_time': int, 'relay_state': int, 'rssi': All(int, Range(max=0)), 'sw_ver': basestring, 'type': basestring, 'updating': check_int_bool, }) current_consumption_schema = Schema({ 'voltage': All(float, Range(min=0, max=300)), 'power': All(float, Range(min=0)), 'total': All(float, Range(min=0)), 'current': All(float, Range(min=0)), }) tz_schema = Schema({ 'zone_str': basestring, 'dst_offset': int, 'index': All(int, Range(min=0)), 'tz_str': basestring, }) def setUp(self): self.plug = SmartPlug(PLUG_IP, protocol=FakeTransportProtocol(sysinfo_hs110)) def tearDown(self): self.plug = None def test_initialize(self): self.assertIsNotNone(self.plug.sys_info) self.sysinfo_schema(self.plug.sys_info) def test_initialize_invalid_connection(self): plug = SmartPlug('127.0.0.1', protocol=FakeTransportProtocol(sysinfo_hs110, invalid=True)) with self.assertRaises(SmartPlugException): plug.sys_info['model'] def test_query_helper(self): with self.assertRaises(SmartPlugException): self.plug._query_helper("test", "testcmd", {}) # TODO check for unwrapping? @skipIf(SKIP_STATE_TESTS, "SKIP_STATE_TESTS is True, skipping") def test_state(self): def set_invalid(x): self.plug.state = x set_invalid_int = partial(set_invalid, 1234) self.assertRaises(ValueError, set_invalid_int) set_invalid_str = partial(set_invalid, "1234") self.assertRaises(ValueError, set_invalid_str) set_invalid_bool = partial(set_invalid, True) self.assertRaises(ValueError, set_invalid_bool) orig_state = self.plug.state if orig_state == SmartPlug.SWITCH_STATE_OFF: self.plug.state = "ON" self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_ON) self.plug.state = "OFF" self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_OFF) elif orig_state == SmartPlug.SWITCH_STATE_ON: self.plug.state = "OFF" self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_OFF) self.plug.state = "ON" self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_ON) elif orig_state == SmartPlug.SWITCH_STATE_UNKNOWN: self.fail("can't test for unknown state") def test_get_sysinfo(self): # initialize checks for this already, but just to be sure self.sysinfo_schema(self.plug.get_sysinfo()) @skipIf(SKIP_STATE_TESTS, "SKIP_STATE_TESTS is True, skipping") def test_turns_and_isses(self): orig_state = self.plug.is_on if orig_state: self.plug.turn_off() self.assertFalse(self.plug.is_on) self.assertTrue(self.plug.is_off) self.plug.turn_on() self.assertTrue(self.plug.is_on) else: self.plug.turn_on() self.assertFalse(self.plug.is_off) self.assertTrue(self.plug.is_on) self.plug.turn_off() self.assertTrue(self.plug.is_off) def test_has_emeter(self): # a not so nice way for checking for emeter availability.. if "110" in self.plug.sys_info["model"]: self.assertTrue(self.plug.has_emeter) else: self.assertFalse(self.plug.has_emeter) def test_get_emeter_realtime(self): self.current_consumption_schema((self.plug.get_emeter_realtime())) def test_get_emeter_daily(self): self.assertEqual(self.plug.get_emeter_daily(year=1900, month=1), {}) k, v = self.plug.get_emeter_daily().popitem() self.assertTrue(isinstance(k, int)) self.assertTrue(isinstance(v, float)) def test_get_emeter_monthly(self): self.assertEqual(self.plug.get_emeter_monthly(year=1900), {}) d = self.plug.get_emeter_monthly() k, v = d.popitem() self.assertTrue(isinstance(k, int)) self.assertTrue(isinstance(v, float)) @skip("not clearing your stats..") def test_erase_emeter_stats(self): self.fail() def test_current_consumption(self): x = self.plug.current_consumption() self.assertTrue(isinstance(x, float)) self.assertTrue(x >= 0.0) def test_identify(self): ident = self.plug.identify() self.assertTrue(isinstance(ident, tuple)) self.assertTrue(len(ident) == 3) def test_alias(self): test_alias = "TEST1234" original = self.plug.alias self.assertTrue(isinstance(original, basestring)) self.plug.alias = test_alias self.assertEqual(self.plug.alias, test_alias) self.plug.alias = original self.assertEqual(self.plug.alias, original) def test_led(self): original = self.plug.led self.plug.led = False self.assertFalse(self.plug.led) self.plug.led = True self.assertTrue(self.plug.led) self.plug.led = original def test_icon(self): self.assertEqual(set(self.plug.icon.keys()), {'icon', 'hash'}) def test_time(self): self.assertTrue(isinstance(self.plug.time, datetime.datetime)) # TODO check setting? def test_timezone(self): self.tz_schema(self.plug.timezone) def test_hw_info(self): self.sysinfo_schema(self.plug.hw_info) def test_on_since(self): self.assertTrue(isinstance(self.plug.on_since, datetime.datetime)) def test_location(self): self.sysinfo_schema(self.plug.location) def test_rssi(self): self.sysinfo_schema({'rssi': self.plug.rssi}) # wrapping for vol def test_mac(self): self.sysinfo_schema({'mac': self.plug.mac}) # wrapping for val
def get_device_power(): plug = SmartPlug("192.168.43.204") print("Current state: %s" % plug.state) print("Current consumption: %s" % plug.get_emeter_realtime())
import sys import logging from pprint import pformat as pf from pyHS100 import SmartPlug logging.basicConfig(level=logging.DEBUG) if len(sys.argv) < 2: print("%s <ip>" % sys.argv[0]) sys.exit(1) hs = SmartPlug(sys.argv[1]) logging.info("Identify: %s", hs.identify()) logging.info("Sysinfo: %s", pf(hs.get_sysinfo())) has_emeter = hs.has_emeter if has_emeter: logging.info("== Emeter ==") logging.info("- Current: %s", hs.get_emeter_realtime()) logging.info("== Monthly ==") logging.info(hs.get_emeter_monthly()) logging.info("== Daily ==") logging.info(hs.get_emeter_daily(month=11, year=2016))
print("to graph the data, use: gnuplot draw.gnuplot") print() print("to stop data recording, just hit Ctrl+C") # open the file to write into it with open(filename, 'a') as f: # write headers f.write("year;month;day;hour;minute;seconds;timestamp;") f.write("power;current;voltage;total\n") f.flush() os.fsync(f) # loop forever while True: consumption = plug.get_emeter_realtime() now = datetime.datetime.now() f.write("%i;%i;%i;" % (now.year, now.month, now.day)) f.write("%i;%i;%i;" % (now.hour, now.minute, now.second)) f.write("%f;" % now.timestamp()) f.write("%f;" % consumption['power']) f.write("%f;" % consumption['current']) f.write("%f;" % consumption['voltage']) f.write("%f\n" % consumption['total']) f.flush() # wait a bit try: