Skip to content

Instantly share code, notes, and snippets.

@unixcharles
unixcharles / TH1123ZB-G2.md
Last active March 20, 2024 18:38
Sinope TH1123ZB-G2 and Home Assistant + zigbee2mqtt

Keep information on the TH1123ZB-G2 screen up to date with Home Assistant and z2m.

Should work with other sinope thermostat like the TH1124ZB.

  • You need to manually push outdoor temperature.
  • Time generally work without this but it will go blank randomly or out of sync at some point (won't change for DST for example).
@unixcharles
unixcharles / gilded_rose.rb
Last active June 23, 2021 14:46
guilded_rose_kata
class ItemUpdater
class AgedBrie < ItemUpdater
def value_change
-super
end
end
class BackstagePass < ItemUpdater
def value_change
case sell_in
@unixcharles
unixcharles / hash_object.rb
Created November 15, 2018 20:41
Hash Driven Development
class HashObject
def []=(method_name, implementation)
define_singleton_method(method_name, &implementation)
end
def [](method_name)
if method(method_name).arity == 0
method(method_name).call
else
method(method_name).curry
@unixcharles
unixcharles / rate_limiter.rb
Created May 2, 2017 19:29
30 lines rate limited
# USAGE:
# rate_limiter = RateLimiter.new('throttle_key', limit: 5, ttl: 1.hour)
# rate_limiter.throttled { not_too_often }
#
class RateLimiter
attr_reader :topic, :limit, :ttl, :cache
def initialize(topic:, limit:, ttl:, cache: default_cache)
@topic, @limit, @ttl, @cache = topic, limit, ttl, cache
end
@unixcharles
unixcharles / wtf.rb
Created March 11, 2014 21:36 — forked from byroot/wtf.rb
class Foo
def self.define_attribute(name)
class_eval %{
def #{name}(*args)
attribute(#{name.inspect}, *args)
end
}
end
@unixcharles
unixcharles / problem.rb
Created October 29, 2012 11:33
DelegateClass and "superclass mismatch for class" errors
require 'delegate'
class Klass < DelegateClass(String)
# ...
end
class Klass < DelegateClass(String)
# Re-open the class
end
# => TypeError: superclass mismatch for class Klass
@unixcharles
unixcharles / heroku_env.rb
Created September 12, 2012 11:29
Import heroku env
#!/usr/bin/env ruby -rubygems
reject_keys = %w(PATH STACK URL APP_NAME LOG_LEVEL)
reject_keys += %w(RACK_ENV RAILS_ENV NODE_ENV)
reject_keys += %w(COMMIT_HASH CONSOLE_AUTH LANG LAST_GIT_BY)
reject_keys += %w(REDISTOGO NEW_RELIC DATABASE MONGOHQ MEMCACHE SENDGRID)
reject_keys += (ENV['REJECT_ENV_KEYS'].nil? ? [] : ENV['REJECT_ENV_KEYS'].split(','))
keys_regexp = %r/#{reject_keys.join('|')}/i
configuration = `heroku config`.split("\n").inject({}) do |hash, line|
@unixcharles
unixcharles / redis_clean.rb
Created August 23, 2012 14:21
Clean up redis queue
queue_name = 'queue:queue_name'
len = Resque.redis.llen(queue_name)
deletion = Resque.redis.lrange(queue_name,0, len).select do |entry|
JSON.parse(entry)['args'][0] == "some_criterial" rescue nil
end
deletion.each do |entry|
puts entry
puts Resque.redis.lrem queue_name, -1, entry
@unixcharles
unixcharles / creditcard.coffee
Created May 11, 2012 13:41 — forked from masylum/creditcard.coffee
Credit Card number validation in Coffee Script
# Ported from https://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js
creditcard = (value)->
# accept only spaces, digits and dashes
return unless /[^0-9 \-]+/.test(value)
nCheck = nDigit = 0
bEven = false
checkDigit = (n) ->
@unixcharles
unixcharles / upload.rb
Created February 3, 2012 20:38
Batch upload to teambox.com
#!/usr/bin/env ruby -rubygems
@api_token = api_token
@project_id = integer_value_or_permalink
@paths = [ array_of_file_path ]
require 'faraday' # >= 0.8
require 'faraday_middleware'
@conn = Faraday.new(:url => 'https://beta.teambox.com/') do |builder|